Class: Castle::Request

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

Constant Summary collapse

METHODS =
[:get, :put, :post, :delete, :head, :options]
@@default_method =
:get

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, *query) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/castle/request.rb', line 9

def initialize method, uri, *query
  @method = METHODS.include?(method) ? method : @@default_method
  @uri = URI.parse(uri.include?("http://") ? "#{uri}" : "http://#{uri}")
  @query = {:hash => query[0], :string => ""}
  
  i = 0; @query[:hash].to_a.flatten.each {|q| @query[:string] << q.to_s + ((i%2 == 1) ? "&" : "="); i += 1}; @query[:string].chomp! "&"
  
  if !@query[:string].empty?
    @query[:string] = URI.encode @query[:string] 
    @uri.query = @query[:string]
  end
  
  args = case @method
    when :get  then [@uri.to_s]
    when :post then [@uri.path, @uri.query]
    # TODO: add rest of methods
  end
  
  @response = Response.new Net::HTTP.new(@uri.host).send(@method, *args)
end

Instance Attribute Details

#methodObject

Returns the value of attribute method.



5
6
7
# File 'lib/castle/request.rb', line 5

def method
  @method
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/castle/request.rb', line 5

def response
  @response
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/castle/request.rb', line 5

def uri
  @uri
end