Class: LCBO::CrawlKit::Request

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

Constant Summary collapse

MAX_RETRIES =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_prototype, query_p = {}, body_p = {}) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
# File 'lib/lcbo/crawlkit/request.rb', line 9

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.



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

def body_params
  @body_params
end

#query_paramsObject

Returns the value of attribute query_params.



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

def query_params
  @query_params
end

#request_prototypeObject (readonly)

Returns the value of attribute request_prototype.



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

def request_prototype
  @request_prototype
end

Instance Method Details

#configObject



27
28
29
30
31
32
33
# File 'lib/lcbo/crawlkit/request.rb', line 27

def config
  opts = {}
  opts[:method]  = request_prototype.http_method
  opts[:headers] = { 'User-Agent' => LCBO.user_agent }
  opts[:body]    = body_params if (body_params && body_params.any?) && !gettable?
  opts
end

#gettable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/lcbo/crawlkit/request.rb', line 23

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

#runObject



44
45
46
# File 'lib/lcbo/crawlkit/request.rb', line 44

def run
  _run
end

#uriObject



35
36
37
38
39
40
41
42
# File 'lib/lcbo/crawlkit/request.rb', line 35

def uri
  @uri ||= begin
    template = request_prototype.uri_template.dup
    query_params.reduce(template) do |mem, (key, value)|
      mem.gsub("{#{key}}", value.to_s)
    end
  end
end