Class | Request::Query |
In: |
lib/violet/request.rb
|
Parent: | Object |
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
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">
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" ].
# File lib/violet/request.rb, line 103 103: def initialize h 104: raise ArgumentError.new('event parameter has no "to_url" method or is empty') unless h[:event] and h[:event].respond_to?(:to_url) 105: raise ArgumentError.new('need a :serial') unless h[:serial] 106: raise ArgumentError.new('need a :token' ) unless h[:token] 107: 108: @event, @serial, @token = h[:event], h[:serial], h[:token] 109: end
send the query to the server. it return a ServerRsp object from the corresponding class if no args is given.
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"
# File lib/violet/request.rb, line 131 131: def send! response_type=nil 132: rsp = open(self.to_url) { |rsp| rsp.read } 133: if response_type == :xml then rsp else Response.parse(rsp) end 134: end
return the complet url
# File lib/violet/request.rb, line 112 112: def to_url 113: opts = @event.to_url 114: if opts.respond_to?(:join) then opts = opts.join('&') end 115: 116: base_url = if @event.streamed? then APISTREAM_URL else API_URL end 117: 118: "#{base_url}?" << [ "sn=#{@serial}", "token=#{@token}", opts ].join('&') 119: end