Module: Twog::TwitterHandler

Included in:
Twog
Defined in:
lib/twog/twitter_handler.rb

Instance Method Summary collapse

Instance Method Details

#ensure_text_is_of_length(length, title, link) ⇒ Object



32
33
34
35
36
# File 'lib/twog/twitter_handler.rb', line 32

def ensure_text_is_of_length(length, title, link)
  blogged = 'blogged:'
  title = title[0, (length - ((' ' * 2).length + blogged.length + link.length))]
  [blogged, title, link].join(' ')
end

#tweet(posts, conf, bitly) ⇒ Object

Raises:

  • (StandardError)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/twog/twitter_handler.rb', line 5

def tweet(posts, conf, bitly)
  return unless posts&.length&.positive?
  raise StandardError, 'OAuth Consumer Key missing' unless conf['consumer_key']
  raise StandardError, 'OAuth Consumer Secret missing' unless conf['consumer_secret']
  raise StandardError, 'OAuth Access Token missing' unless conf['access_token']
  raise StandardError, 'OAuth Access Secret missing' unless conf['access_secret']

  posts.sort.each do |post|
    link = bitly ? bitly.shorten(post.link).short_url : item.link
    use_twitter_oauth(post, link, conf)
    update_config_file_with_latest_tweet_date(post.date.to_s, conf)
  end
end

#update_config_file_with_latest_tweet_date(last_blog_post_tweeted, conf) ⇒ Object



38
39
40
41
# File 'lib/twog/twitter_handler.rb', line 38

def update_config_file_with_latest_tweet_date(last_blog_post_tweeted, conf)
  conf['last_blog_post_tweeted'] = last_blog_post_tweeted
  File.open("#{ENV['HOME']}/.twog/conf.yaml", 'w') { |out| out.write(conf.to_yaml) }
end

#use_twitter_oauth(post, link, conf) ⇒ Object

Raises:

  • (StandardError)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twog/twitter_handler.rb', line 19

def use_twitter_oauth(post, link, conf)
  client = TwitterOAuth::Client.new(
    consumer_key: conf['consumer_key'],
    consumer_secret: conf['consumer_secret'],
    token: conf['access_token'],
    secret: conf['access_secret']
  )
  raise StandardError, 'TwitterOAuth unauthorized' unless client.authorized?

  text = ensure_text_is_of_length(140, post.title, link)
  client.update(text)
end