Class: PriceGrabber::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:, pid:, key:, asin: nil, q: nil, masterid: nil, upc: nil, environment: :staging, driver: :net_http) ⇒ Request

Returns a new instance of Request.



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

def initialize(version:, pid:, key:, asin: nil, q: nil, masterid: nil, upc: nil, environment: :staging, driver: :net_http)
  @environment = environment
  @version = version
  @pid = pid
  @key = key
  @asin = asin
  @q = q
  @upc = upc
  @masterid = [*masterid]
  @driver = driver
  @wants = []
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/pricegrabber/request.rb', line 6

def environment
  @environment
end

Instance Method Details

#callObject



50
51
52
53
54
55
# File 'lib/pricegrabber/request.rb', line 50

def call
  request = HTTPI::Request.new
  request.url = to_s
  resp = HTTPI.get(request, @driver)
  PriceGrabber::Response.new(resp, @wants)
end

#pluck(*attributes) ⇒ Object



57
58
59
60
# File 'lib/pricegrabber/request.rb', line 57

def pluck(*attributes)
  @wants = attributes.map(&:to_s)
  self
end

#to_curlObject



46
47
48
# File 'lib/pricegrabber/request.rb', line 46

def to_curl
  "curl -G '#{to_s}'"
end

#to_sObject



42
43
44
# File 'lib/pricegrabber/request.rb', line 42

def to_s
  to_uri.to_s
end

#to_uriObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pricegrabber/request.rb', line 21

def to_uri
  uri = ::URI::HTTP.build({})
  uri.scheme = "http"
  if @environment == :production
    uri.host = "sws.pricegrabber.com"
  else
    uri.host = "sws.api.pricegrabber.com"
  end
  uri.path = "/search_xml.php"
  uri.query = [
    version,
    pid,
    key,
    asin,
    q,
    upc,
    masterid,
  ].compact.sort.join("&")
  uri
end