Class: LCBO::CrawlKit::Request
- Inherits:
-
Object
- Object
- LCBO::CrawlKit::Request
- Defined in:
- lib/lcbo/crawlkit/request.rb
Instance Attribute Summary collapse
-
#body_params ⇒ Object
Returns the value of attribute body_params.
-
#query_params ⇒ Object
Returns the value of attribute query_params.
-
#request_prototype ⇒ Object
readonly
Returns the value of attribute request_prototype.
Instance Method Summary collapse
- #config ⇒ Object
- #gettable? ⇒ Boolean
-
#initialize(request_prototype, query_p = {}, body_p = {}) ⇒ Request
constructor
A new instance of Request.
- #run ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(request_prototype, query_p = {}, body_p = {}) ⇒ Request
Returns a new instance of Request.
7 8 9 10 11 |
# File 'lib/lcbo/crawlkit/request.rb', line 7 def initialize(request_prototype, query_p = {}, body_p = {}) @request_prototype = request_prototype self.query_params = query_p self.body_params = body_p end |
Instance Attribute Details
#body_params ⇒ Object
Returns the value of attribute body_params.
5 6 7 |
# File 'lib/lcbo/crawlkit/request.rb', line 5 def body_params @body_params end |
#query_params ⇒ Object
Returns the value of attribute query_params.
5 6 7 |
# File 'lib/lcbo/crawlkit/request.rb', line 5 def query_params @query_params end |
#request_prototype ⇒ Object (readonly)
Returns the value of attribute request_prototype.
5 6 7 |
# File 'lib/lcbo/crawlkit/request.rb', line 5 def request_prototype @request_prototype end |
Instance Method Details
#config ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/lcbo/crawlkit/request.rb', line 25 def config opts = {} opts[:method] = request_prototype.http_method opts[:user_agent] = USER_AGENT opts[:params] = body_params unless gettable? opts end |
#gettable? ⇒ Boolean
21 22 23 |
# File 'lib/lcbo/crawlkit/request.rb', line 21 def gettable? [:head, :get].include?(request_prototype.http_method) end |
#run ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lcbo/crawlkit/request.rb', line 40 def run response = Typhoeus::Request.run(uri, config) Response.new \ :code => response.code, :uri => response.request.url, :http_method => response.request.method, :time => response.time, :query_params => query_params, :body_params => body_params, :body => response.body end |
#uri ⇒ Object
33 34 35 36 37 38 |
# File 'lib/lcbo/crawlkit/request.rb', line 33 def uri template = request_prototype.uri_template.dup query_params.reduce(template) do |mem, (key, value)| mem.gsub("{#{key}}", value.to_s) end end |