Class: WebSpeak::Request
- Inherits:
-
Object
- Object
- WebSpeak::Request
- Defined in:
- lib/webspeak/request.rb
Instance Attribute Summary collapse
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#response ⇒ Object
Returns the value of attribute response.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(host = "localhost", port = 80, path = "/") ⇒ Request
constructor
A new instance of Request.
- #post ⇒ Object
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
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
7 8 9 |
# File 'lib/webspeak/request.rb', line 7 def @cookies end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/webspeak/request.rb', line 7 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/webspeak/request.rb', line 8 def host @host end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
7 8 9 |
# File 'lib/webspeak/request.rb', line 7 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/webspeak/request.rb', line 8 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/webspeak/request.rb', line 8 def port @port end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
7 8 9 |
# File 'lib/webspeak/request.rb', line 7 def query_params @query_params end |
#response ⇒ Object
Returns the value of attribute response.
8 9 10 |
# File 'lib/webspeak/request.rb', line 8 def response @response end |
#verbose ⇒ Object
Returns the value of attribute verbose.
8 9 10 |
# File 'lib/webspeak/request.rb', line 8 def verbose @verbose end |
Instance Method Details
#get ⇒ Object
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 @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 |
#post ⇒ Object
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 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 |