Class: Twroute::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/twroute/dispatcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, *args) ⇒ Dispatcher

Returns a new instance of Dispatcher.



9
10
11
12
13
14
15
16
17
# File 'lib/twroute/dispatcher.rb', line 9

def initialize(options, *args)
  @options = {
    :host => "",
    :port => false,
    :http_auth_user => false,
    :http_auth_password => false
  }.merge(options)
  self.tweet_routes = [*args] 
end

Instance Attribute Details

#tweet_routesObject

Returns the value of attribute tweet_routes.



7
8
9
# File 'lib/twroute/dispatcher.rb', line 7

def tweet_routes
  @tweet_routes
end

Instance Method Details

#dispatch_route(route) ⇒ Object



32
33
34
35
36
37
# File 'lib/twroute/dispatcher.rb', line 32

def dispatch_route(route)
  uri = get_uri(route)
  post_args = route.get_post_args
  changed
  notify_observers(uri, post_args)
end

#find_route(tweet_hash) ⇒ Object



25
26
27
28
29
30
# File 'lib/twroute/dispatcher.rb', line 25

def find_route(tweet_hash)
  tweet_routes.find do |route|
    route.tweet(tweet_hash)
    return route if route.is_match?
  end
end

#get_uri(route) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/twroute/dispatcher.rb', line 39

def get_uri(route)
  url = "http://"
  url += "#{@options[:http_auth_user]}:#{@options[:http_auth_password]}@" if @options[:http_auth_user] && @options[:http_auth_password]
  url += "#{@options[:host]}"
  url += ":#{@options[:port]}" if @options[:port]
  url += route.path
  URI.parse(url)
end

#update(tweet_hash) ⇒ Object



19
20
21
22
23
# File 'lib/twroute/dispatcher.rb', line 19

def update(tweet_hash)
  puts tweet_hash[:text]
  route = find_route(tweet_hash)
  dispatch_route(route) if route
end