Class: LCBO::CrawlKit::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_paramsObject

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_paramsObject

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_prototypeObject (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

#configObject



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

Returns:

  • (Boolean)


21
22
23
# File 'lib/lcbo/crawlkit/request.rb', line 21

def gettable?
  [:head, :get].include?(request_prototype.http_method)
end

#runObject



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

#uriObject



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