Class: Request::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/violet/request.rb

Overview

this class is used to “translate” our Events into URLs. a query contains an event to send and the serial/token of the target Rabbit. That way, you can send the same event to many nabaztag easily.

see api.nabaztag.com/docs/home.html

Examples

q = Query.new :token => "my_token", :serial => "my_serial", :event => GET_RABBIT_NAME # =>  #<Request::Query:0x2aaaaaee10b8 @event=#<Request::Action:0x2b74bb47f828 @id=10>, @token="my_token", @serial="my_serial">

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Query

create a new Query object with the give parameters. serial and token parameters should be checked at a higher level. event parameter is usually an Event object, but you can give any Object that respond to to_url, it should return a string that contains some GET parameters like “foo=bar&oni=2”, or an array of GET options like [ “foo=bar”, “oni=2” ].

Raises:

  • (ArgumentError)


103
104
105
106
107
108
109
# File 'lib/violet/request.rb', line 103

def initialize h
  raise ArgumentError.new('event parameter has no "to_url" method or is empty') unless h[:event] and h[:event].respond_to?(:to_url)
  raise ArgumentError.new('need a :serial') unless h[:serial]
  raise ArgumentError.new('need a :token' ) unless h[:token]

  @event, @serial, @token = h[:event], h[:serial], h[:token]
end

Instance Method Details

#send!(response_type = nil) ⇒ Object

send the query to the server. it return a ServerRsp object from the corresponding class if no args is given.

Arguments

:xml

the raw xml server’s response

Examples

q = Query.new :token => "my_token", :serial => "my_serial", :event => GET_RABBIT_NAME # =>  #<Request::Query:0x2aaaaaee10b8 @event=#<Request::Action:0x2b74bb47f828 @id=10>, @token="my_token", @serial="my_serial">
q.send!          # => #<Response::RabbitName:0x2b74b8c38798 @xml=<UNDEFINED> ... </>>
q.send!(:xml)    # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rsp><rabbitName>Makoto</rabbitName></rsp>\n"


131
132
133
134
# File 'lib/violet/request.rb', line 131

def send! response_type=nil
  rsp = open(self.to_url) { |rsp| rsp.read }
  if response_type == :xml then rsp else Response.parse(rsp) end
end

#to_urlObject

return the complet url



112
113
114
115
116
117
118
119
# File 'lib/violet/request.rb', line 112

def to_url
  opts = @event.to_url
  if opts.respond_to?(:join) then opts = opts.join('&') end

  base_url = if @event.streamed? then APISTREAM_URL else API_URL end

  "#{base_url}?" << [ "sn=#{@serial}", "token=#{@token}", opts ].join('&')
end