Class: BirdGrinder::Client
- Inherits:
-
Object
- Object
- BirdGrinder::Client
- 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
-
#tweeter ⇒ Object
readonly
Returns the value of attribute tweeter.
Class Method Summary collapse
-
.run ⇒ Object
Starts processing as a new client instance.
-
.stop ⇒ Object
Stops the event loop so the program can be stopped.
Instance Method Summary collapse
-
#dm(user, text, opts = {}) ⇒ Object
Direct messages a given user with the given text.
-
#initialize ⇒ Client
constructor
Initializes this client and creates a new, associated tweeter instance with this client set as the delegate.
-
#receive_message(type, options = BirdGrinder::Nash.new) ⇒ Object
Forwards a given message type (with options) to each handler, storing the current id if changed.
-
#reply(user, text, opts = {}) ⇒ Object
Replies to a given user with the given text.
-
#search(q, opts = {}) ⇒ Object
Searches for a given query.
-
#streaming ⇒ Object
Returns the streaming api instance.
-
#tweet(text, opts = {}) ⇒ Object
Tweets some text as the current user.
-
#update_all ⇒ Object
Fetches all direct messages and mentions and also schedules the next set of updates.
Constructor Details
#initialize ⇒ Client
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
#tweeter ⇒ Object (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
.run ⇒ Object
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 |
.stop ⇒ Object
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
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 (type, = BirdGrinder::Nash.new) = .to_nash if .respond_to?(:to_nash) logger.debug "receiving message: #{type.inspect} - #{options.id? ? options.id : 'unknown id'}" dispatch(type.to_sym, ) update_stored_id_for(type, .id) if .id? end |
#reply(user, text, opts = {}) ⇒ Object
Replies to a given user with the given text.
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
59 60 61 |
# File 'lib/bird_grinder/client.rb', line 59 def search(q, opts = {}) @tweeter.search(q, opts) end |
#streaming ⇒ Object
Returns the streaming api instance
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
73 74 75 |
# File 'lib/bird_grinder/client.rb', line 73 def tweet(text, opts = {}) @tweeter.tweet(text, opts) end |
#update_all ⇒ Object
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 |