Class: CrossPost::Twitter

Inherits:
Object
  • Object
show all
Defined in:
lib/cross-post/twitter.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Twitter

Returns a new instance of Twitter.



8
9
10
11
12
13
14
15
16
17
# File 'lib/cross-post/twitter.rb', line 8

def initialize(config)
  config  = {
      consumer_key:        config['twitter.consumer.key'],
      consumer_secret:     config['twitter.consumer.secret'],
      access_token:        config['twitter.access.token'],
      access_token_secret: config['twitter.access.secret']
  }
  @client = ::Twitter::REST::Client.new config
  @stream = ::Twitter::Streaming::Client.new config
end

Instance Method Details

#post(content, media = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cross-post/twitter.rb', line 19

def post(content, media = [])
  media = media.collect { |f| @client.upload f }

  parts = split content
  last  = nil

  unless media.empty?
    first, *parts = parts
    last          = @client.update first, media_ids: media.join(',')
  end
  parts.each { |p| last = @client.update p, in_reply_to_status: last }
end

#post_status(status) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cross-post/twitter.rb', line 32

def post_status(status)
  content = Sanitize.clean status.content
  content = CGI.unescape_html content
  media   = status.media_attachments.collect { |f| open f.url }

  LOGGER.info { 'Sending to twitter' }
  LOGGER.debug { "  Content: #{content}" }
  LOGGER.debug { "  Attachments: #{media.size}" }

  self.post content, media

  media.each do |f|
    f.close
    f.unlink
  end
end