require 'relax/query' module Relax class Request def initialize(options = {}) self.class.class_variables.each do |variable| instance_variable_set(variable.slice(1..-1), self.class.send(:class_variable_get, variable)) end options.each do |key, value| instance_variable_set "@#{key}", value end end def to_query keys.inject(Query.new) do |parameters, key| parameters[convert_key(key)] = send(key) parameters end end def to_s keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key| "#{key.to_s}=#{ERB::Util.url_encode(send('[]', key).to_s)}" end.join('&') end class << self def parameter(name, options = {}) attr_accessor name class_variable_set("@@#{name}", options.delete(:value)) if options[:value] end def []=(key, value) parameter(key, {:value => value}) end end protected def keys instance_variables.collect { |v| v.sub('@', '') } end def convert_key(key) key end end end