Class: ChromeData::BaseRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/chrome_data/base_request.rb

Direct Known Subclasses

CollectionRequest, Vehicle

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ BaseRequest

Returns a new instance of BaseRequest.



6
7
8
9
10
# File 'lib/chrome_data/base_request.rb', line 6

def initialize(attrs={})
  attrs.each do |k, v|
    send "#{k}=", v
  end
end

Class Method Details

.build_requestObject

Builds request, returns LolSoap::Request



57
58
59
# File 'lib/chrome_data/base_request.rb', line 57

def build_request
  client.request request_name
end

.clientObject



61
62
63
# File 'lib/chrome_data/base_request.rb', line 61

def client
  @@client ||= LolSoap::Client.new(wsdl_body)
end

.endpoint_uriObject



69
70
71
# File 'lib/chrome_data/base_request.rb', line 69

def endpoint_uri
  @@endpoint_uri ||= URI(client.wsdl.endpoint)
end

.find_elements(element_name, response) ⇒ Object

Given an element_name and LolSoap::Response, returns an array of Nokogiri::XML::Elements



74
75
76
# File 'lib/chrome_data/base_request.rb', line 74

def find_elements(element_name, response)
  response.body.xpath(".//x:#{element_name}", 'x' => response.body.namespace.href)
end

.make_request(request) ⇒ Object

Makes request, returns LolSoap::Response



48
49
50
51
52
53
54
# File 'lib/chrome_data/base_request.rb', line 48

def make_request(request)
  raw_response = Net::HTTP.start(endpoint_uri.host, endpoint_uri.port) do |http|
    http.request_post(endpoint_uri.path, request.content, request.headers)
  end

  client.response(request, raw_response.body)
end

.parse_response(response) ⇒ Object

Internal: Given a LolSoap::Response, returns appropriately parsed response

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/chrome_data/base_request.rb', line 79

def parse_response(response)
  raise NotImplementedError, '.parse_response should be implemented in subclass'
end

.request(data) ⇒ Object

Builds request, sets additional data on request element, makes request, and returns array of child elements wrapped in instances of this class



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chrome_data/base_request.rb', line 15

def request(data)
  request = build_request

  request.body do |b|
    # Set configured account info on builder
    b.accountInfo(
      number: ChromeData.config.,
      secret: ChromeData.config.,
      country: ChromeData.config.country,
      language: ChromeData.config.language
    )

    # Set additional elements on builder
    data.each do |k, v|
      # Add the key/value pair as an attribute to the request element if that's what it should be,
      # otherwise add it as a sub-element
      # NOTE: This basically mirrors LolSoap::Builder#method_missing
      #       because Builder undefines most methods, including #send
      if b.__type__.has_attribute?(k)
        b.__attribute__ k, v
      else
        b.__tag__ k, v
      end
    end
  end

  # Make the request
  response = make_request(request)

  parse_response(response)
end

.request_nameObject

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/chrome_data/base_request.rb', line 83

def request_name
  raise NotImplementedError, '.request_name should be implemented in subclass'
end

.wsdl_bodyObject



65
66
67
# File 'lib/chrome_data/base_request.rb', line 65

def wsdl_body
  @@wsdl_body ||= Net::HTTP.get_response(URI('http://services.chromedata.com/Description/7a?wsdl')).body
end