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. Examples:

TODO

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

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.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
# File 'lib/violet/request.rb', line 87

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

TODO



101
102
103
104
105
# File 'lib/violet/request.rb', line 101

def send! response_type=nil
  # TODO: rescue ?
  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: API_URL with the serial, token and options.



96
97
98
# File 'lib/violet/request.rb', line 96

def to_url
  API_URL+'?' << [ "token=#{@token}", "sn=#{@serial}", @event.to_url ].join('&')
end