Class: CrossPost::Twitter
- Inherits:
-
Object
- Object
- CrossPost::Twitter
- Defined in:
- lib/cross-post/twitter.rb
Constant Summary collapse
- WHITESPACE_TAGS =
{ 'br' => { before: "\n", after: '' }, 'div' => { before: "\n", after: "\n" }, 'p' => { before: "\n", after: "\n" } }.freeze
Instance Method Summary collapse
-
#initialize(config) ⇒ Twitter
constructor
A new instance of Twitter.
- #post(content, media = [], id:, reply_to:) ⇒ Object
- #post_status(status) ⇒ Object
Constructor Details
#initialize(config) ⇒ Twitter
Returns a new instance of Twitter.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cross-post/twitter.rb', line 11 def initialize(config) settings = config[:settings] @posts = config[:posts] @users = config[:users] config = { consumer_key: settings['twitter.consumer.key'], consumer_secret: settings['twitter.consumer.secret'], access_token: settings['twitter.access.token'], access_token_secret: settings['twitter.access.secret'] } @client = ::Twitter::REST::Client.new config @stream = ::Twitter::Streaming::Client.new config end |
Instance Method Details
#post(content, media = [], id:, reply_to:) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cross-post/twitter.rb', line 26 def post(content, media = [], id:, reply_to:) reply_to = OpenStruct.new id: reply_to unless reply_to.respond_to? :id media = media.collect { |f| @client.upload f } parts = split content unless media.empty? first, *parts = parts reply_to = @client.update first, media_ids: media.join(','), in_reply_to_status: reply_to end parts.each { |p| reply_to = @client.update p, in_reply_to_status: reply_to } reply_to = reply_to.id if reply_to.respond_to? :id @posts[id] = reply_to end |
#post_status(status) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cross-post/twitter.rb', line 47 def post_status(status) content = status.content content = Sanitize.clean(content, whitespace_elements: WHITESPACE_TAGS).strip content = CGI.unescape_html content @users.each do |mastodon, twitter| content = content.gsub /@\b#{mastodon}\b/, "@#{twitter}" end media = status..collect { |f| open f.url } LOGGER.info { 'Sending to twitter' } LOGGER.debug { " Content: #{content}" } LOGGER.debug { " Attachments: #{media.size}" } reply = status.in_reply_to_id reply_to = reply ? @posts[reply] : nil self.post content, media, id: status.id, reply_to: reply_to media.each do |f| f.close f.unlink end end |