You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1017B

  1. require 'twitter'
  2. require 'twitter-text'
  3. class CrossPost
  4. class Twitter
  5. def initialize(config)
  6. config = {
  7. consumer_key: config['twitter.consumer.key'],
  8. consumer_secret: config['twitter.consumer.secret'],
  9. access_token: config['twitter.access.token'],
  10. access_token_secret: config['twitter.access.secret']
  11. }
  12. @client = ::Twitter::REST::Client.new config
  13. @stream = ::Twitter::Streaming::Client.new config
  14. end
  15. def post(status)
  16. content = Sanitize.clean status.content
  17. last = nil
  18. parts = split content
  19. parts.each { |p| last = @client.update p, in_reply_to_status: last }
  20. end
  21. private
  22. def split(text)
  23. parts = []
  24. part = ''
  25. words = text.split ' '
  26. words.each do |word|
  27. old_part = part
  28. part += ' ' unless part == ''
  29. part += word
  30. invalid = ::Twitter::Validation.tweet_invalid? part
  31. if invalid
  32. parts << old_part
  33. part = word
  34. end
  35. end
  36. parts << part unless part == ''
  37. parts
  38. end
  39. end
  40. end