Class: BirdGrinder::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bird_grinder/client.rb

Overview

Glue between BirdGrinder::Tweeter and associated handlers to make it function in an evented fashion.

The client basically brings it all together. It acts as a delegate for the tweeter and converts received results into dispatchable form for each handler.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Initializes this client and creates a new, associated tweeter instance with this client set as the delegate. Also, for all of this clients handlers it will call client= if defined.

Lastly, it updates BirdGrinder::Client.current to point to itself.



30
31
32
33
34
35
36
# File 'lib/bird_grinder/client.rb', line 30

def initialize
  logger.debug "Initializing client..."
  @tweeter = BirdGrinder::Tweeter.new(self)
  logger.debug "Notifying handlers of the client"
  handlers.each { |h| h.client = self if h.respond_to?(:client=) }
  self.current = self
end

Instance Attribute Details

#tweeterObject (readonly)

Returns the value of attribute tweeter.



18
19
20
# File 'lib/bird_grinder/client.rb', line 18

def tweeter
  @tweeter
end

Class Method Details

.runObject

Starts processing as a new client instance. The main entry point into the programs event loop. Once started, will invoke the once_running hook.



94
95
96
97
98
99
100
101
# File 'lib/bird_grinder/client.rb', line 94

def self.run
  logger.info "Preparing to start BirdGrinder"
  client = self.new
  EventMachine.run do
    client.update_all
    BirdGrinder::Loader.invoke_hooks!(:once_running)
  end
end

.stopObject

Stops the event loop so the program can be stopped.



104
105
106
# File 'lib/bird_grinder/client.rb', line 104

def self.stop
  EventMachine.stop_event_loop
end

Instance Method Details

#dm(user, text, opts = {}) ⇒ Object

Direct messages a given user with the given text

See Also:



80
81
82
# File 'lib/bird_grinder/client.rb', line 80

def dm(user, text, opts = {})
  @tweeter.dm(user, text, opts)
end

#receive_message(type, options = BirdGrinder::Nash.new) ⇒ Object

Forwards a given message type (with options) to each handler, storing the current id if changed.



40
41
42
43
44
45
# File 'lib/bird_grinder/client.rb', line 40

def receive_message(type, options = BirdGrinder::Nash.new)
  options = options.to_nash if options.respond_to?(:to_nash)
  logger.debug "receiving message: #{type.inspect} - #{options.id? ? options.id : 'unknown id'}"
  dispatch(type.to_sym, options)
  update_stored_id_for(type, options.id) if options.id?
end

#reply(user, text, opts = {}) ⇒ Object

Replies to a given user with the given text.

See Also:



87
88
89
# File 'lib/bird_grinder/client.rb', line 87

def reply(user, text, opts = {})
  @tweeter.reply(user, text, opts)
end

#search(q, opts = {}) ⇒ Object

Searches for a given query

See Also:



59
60
61
# File 'lib/bird_grinder/client.rb', line 59

def search(q, opts = {})
  @tweeter.search(q, opts)
end

#streamingObject

Returns the streaming api instance

See Also:



66
67
68
# File 'lib/bird_grinder/client.rb', line 66

def streaming
  @tweeter.streaming
end

#tweet(text, opts = {}) ⇒ Object

Tweets some text as the current user

See Also:



73
74
75
# File 'lib/bird_grinder/client.rb', line 73

def tweet(text, opts = {})
  @tweeter.tweet(text, opts)
end

#update_allObject

TODO:

Schedule future fetch only when others are completed.

Fetches all direct messages and mentions and also schedules the next set of updates.



51
52
53
54
# File 'lib/bird_grinder/client.rb', line 51

def update_all
  fetch :direct_message, :mention
  update_and_schedule_fetch
end