Class: WebSpeak::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", port = 80, path = "/") ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
# File 'lib/webspeak/request.rb', line 10

def initialize(host="localhost", port=80, path="/")
  @host = host
  @port = port
  @path = path
  self.verbose = false
  @cookies = {}
  @query_params = {}
  @params = {}
  @headers = {}
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



7
8
9
# File 'lib/webspeak/request.rb', line 7

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/webspeak/request.rb', line 7

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/webspeak/request.rb', line 8

def host
  @host
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/webspeak/request.rb', line 7

def params
  @params
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/webspeak/request.rb', line 8

def path
  @path
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/webspeak/request.rb', line 8

def port
  @port
end

#query_paramsObject (readonly)

Returns the value of attribute query_params.



7
8
9
# File 'lib/webspeak/request.rb', line 7

def query_params
  @query_params
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/webspeak/request.rb', line 8

def response
  @response
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/webspeak/request.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#getObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/webspeak/request.rb', line 37

def get
  add_cookies_to_header
  
  @path_with_params = "#{@path}"
  sep = "?"
  unless query_params.empty?
    @path_with_params += "#{sep}#{to_param_string(query_params)}"
    sep = "&"
  end
  @path_with_params += "#{sep}#{to_param_string(params)}" unless params.empty?
    
  Net::HTTP.start(@host, @port) do |http|
   puts "Getting http://#{@host}:#{@port}#{@path_with_params}" if self.verbose
    @response = http.get(@path_with_params, headers)
  end
end

#postObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webspeak/request.rb', line 21

def post

  log_hash(params, "PARAMS") if self.verbose
  log_hash(query_params, "QUERY_PARAMS") if self.verbose

  add_cookies_to_header
  log_hash(headers, "HEADERS") if self.verbose

  @path_with_params = "#{@path}"
  @path_with_params += "?#{to_param_string(query_params)}" unless query_params.empty?
  Net::HTTP.start(@host, @port) do |http|
   puts "Posting to http://#{@host}:#{@port}#{@path_with_params}" if self.verbose
    @response = http.post(@path_with_params, to_param_string(params), headers)
  end
end