Class: Flamingo::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/flamingo/stream.rb

Constant Summary collapse

VERSION =
1
RESOURCES =
HashWithIndifferentAccess.new(
  :filter   => "statuses/filter",
  :firehose => "statuses/firehose",
  :retweet  => "statuses/retweet",
  :sample   => "statuses/sample"
)
DEFAULT_CONNECTION_OPTIONS =
{
  :method     =>"POST", 
  :ssl        => false, 
  :user_agent => "Flamingo/#{Flamingo::VERSION}"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params) ⇒ Stream

Returns a new instance of Stream.



28
29
30
31
# File 'lib/flamingo/stream.rb', line 28

def initialize(name,params)
  self.name = name
  self.params = params
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/flamingo/stream.rb', line 26

def name
  @name
end

#paramsObject

Returns the value of attribute params.



26
27
28
# File 'lib/flamingo/stream.rb', line 26

def params
  @params
end

Class Method Details

.get(name) ⇒ Object



21
22
23
# File 'lib/flamingo/stream.rb', line 21

def get(name)
  new(name,StreamParams.new(name))
end

Instance Method Details

#connect(options) ⇒ Object



33
34
35
# File 'lib/flamingo/stream.rb', line 33

def connect(options)
  Twitter::JSONStream.connect(connection_options(options))
end

#connection_options(overrides = {}) ⇒ Object



37
38
39
40
41
# File 'lib/flamingo/stream.rb', line 37

def connection_options(overrides={})
  DEFAULT_CONNECTION_OPTIONS.
    merge(overrides).
    merge(:path=>path,:content=>query)      
end

#pathObject



43
44
45
# File 'lib/flamingo/stream.rb', line 43

def path
  "/#{VERSION}/#{resource}.json"
end

#queryObject



57
58
59
# File 'lib/flamingo/stream.rb', line 57

def query
  params.map{|key,value| "#{key}=#{param_value(value)}" }.join("&")
end

#resourceObject



47
48
49
# File 'lib/flamingo/stream.rb', line 47

def resource
  RESOURCES[name.to_sym]
end

#to_jsonObject



51
52
53
54
55
# File 'lib/flamingo/stream.rb', line 51

def to_json
  ActiveSupport::JSON.encode(
    :name=>name,:resource=>resource,:params=>params.all
  )
end

#to_sObject



61
62
63
# File 'lib/flamingo/stream.rb', line 61

def to_s
  "#{path}?#{query}"
end