Class: Squawk

Inherits:
Struct
  • Object
show all
Defined in:
lib/squawk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statusObject

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



3
4
5
# File 'lib/squawk.rb', line 3

def status
  @status
end

Class Method Details

.register_event(event_name, message_or_method) ⇒ Object

Registers an update event. For example:

Squawk.register_event :site_down, "Site just went down"
Squawk.register_event :user_signed_up, lambda { |user| "#{user.name} just signed up" }

provide you with:

Squawk.site_down!
Squawk.user_signed_up! @user


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/squawk.rb', line 14

def register_event(event_name, message_or_method)
  @@events ||= []
  
  event_name = event_name.to_sym
  
  return if @@events.include?(event_name)
  
  @@events << event_name.to_sym
  
  method_name = "#{event_name}!"

  (class << self; self; end).class_eval do # http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html
    # First, the easy case - string
    if message_or_method.is_a?(String)
      define_method method_name do
        update message_or_method
      end
    # Now, the harder case - lambda
    else
      define_method method_name do |*params|
        update(message_or_method.call(*params))
      end
    end
  end  
end

.test!Object



40
41
42
# File 'lib/squawk.rb', line 40

def test!
  update "The time is currently #{Time.now}"
end

Instance Method Details

#performObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/squawk.rb', line 58

def perform
  Twitter.configure do |config|
    config.consumer_key       = @@twitter_consumer_key
    config.consumer_secret    = @@twitter_consumer_secret
    config.oauth_token        = @@twitter_access_token
    config.oauth_token_secret = @@twitter_secret
  end

  Twitter::Client.new.update(status)
end