Module: TwitterNotification

Defined in:
lib/twitter_notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
# File 'lib/twitter_notification.rb', line 3

def self.included(base)
  base.class_eval {
    after_save :notify_twitter
  }
end

Instance Method Details

#absolute_urlObject



29
30
31
32
33
34
35
36
37
# File 'lib/twitter_notification.rb', line 29

def absolute_url
  if host = Radiant.config['site.host']
    if host =~ /^http/
      "#{host}#{self.url}"
    else
      "http://#{host}#{self.url}"
    end
  end
end

#notify_twitterObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/twitter_notification.rb', line 9

def notify_twitter
  if parent
    if published? && Radiant.configured? && parent.notify_twitter_of_children? && !self.twitter_id
      title_length = 138 - absolute_url.length
      message_title = title.length > title_length ? (title[0..title_length-4] + "...") : title
      message = "#{message_title}: #{absolute_url}"
      begin
        httpauth = Twitter::HTTPAuth.new(Radiant.config['twitter.username'], Radiant.config['twitter.password'])
        client = Twitter::Base.new(httpauth)
        status = client.update(message, :source => "radianttwitternotifier")
        # Don't trigger save callbacks
        self.class.update_all({:twitter_id => status.id}, :id => self.id)
      rescue Twitter::Error => e
        logger.error "Twitter Notification failure: #{e.inspect}"
      end
    end
  end
  true
end