Module: MyJohnDeere::RESTMethods::ClassMethods

Defined in:
lib/myjohndeere/rest_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_jd_resourceObject

Returns the value of attribute base_jd_resource.



4
5
6
# File 'lib/myjohndeere/rest_methods.rb', line 4

def base_jd_resource
  @base_jd_resource
end

#list_resource_pathObject

Returns the value of attribute list_resource_path.



6
7
8
# File 'lib/myjohndeere/rest_methods.rb', line 6

def list_resource_path
  @list_resource_path
end

#retrieve_resource_pathObject

Returns the value of attribute retrieve_resource_path.



5
6
7
# File 'lib/myjohndeere/rest_methods.rb', line 5

def retrieve_resource_path
  @retrieve_resource_path
end

Instance Method Details

#build_resource_base_path!(resource_path, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/myjohndeere/rest_methods.rb', line 40

def build_resource_base_path!(resource_path, options = {})
  expected_definitions = resource_path.scan(/%{(.+?)}/)
  return resource_path if expected_definitions.empty?
  base_resources = {}
  options.each do |key, val|
    base_resources[key] = options.delete(key) if key.match(/_id\Z/)
  end
  MyJohnDeere.logger.info("Building resource path: #{resource_path}, with ids: #{base_resources}")
  begin 
    return resource_path % base_resources
  rescue KeyError
    raise ArgumentError.new("You must specify #{expected_definitions.join(", ")} as part of this request path")
  end
end

#list(access_token, options = {}) ⇒ Object

If the resource requires a base resource, specify it in the format of: <resource_singular_name_id>: <ID>



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/myjohndeere/rest_methods.rb', line 9

def list(access_token, options = {})
  validate_access_token(access_token)
  options = {count: 10, start: 0, etag: nil}.merge(options)
  options[:body] ||= {}
  # The count and start are in this list,so move them into the body
  SPECIAL_BODY_PARAMETERS.each do |sbp|
    options[:body][sbp] = options[sbp]
  end

  response = access_token.execute_request(:get, build_resource_base_path!(self.list_resource_path, options), 
    options
  )
  return ListObject.new(
    self,
    access_token,
    response.data,
    options: options.merge(
      etag: response.http_headers[MyJohnDeere::ETAG_HEADER_KEY]
    )
  )
end

#retrieve(access_token, id, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/myjohndeere/rest_methods.rb', line 31

def retrieve(access_token, id, options={})
  validate_access_token(access_token)
  response = access_token.execute_request(:get, 
    "#{build_resource_base_path!(self.retrieve_resource_path, options)}/#{id}",
    options)

  return new(response.data, access_token)
end

#send_create(access_token, body, path_builder_options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/myjohndeere/rest_methods.rb', line 59

def send_create(access_token, body, path_builder_options = {})
  response = access_token.execute_request(:post, 
    build_resource_base_path!(self.list_resource_path, path_builder_options),
    body: body
  )
  #{"Content-Type"=>"text/plain", "X-Deere-Handling-Server"=>"ldxtc3", "X-Frame-Options"=>"SAMEORIGIN", "Location"=>"https://sandboxapi.deere.com/platform/mapLayers/e2711205-c5df-445e-aad5-81eaf9090e6c", "X-Deere-Elapsed-Ms"=>"162", "Vary"=>"Accept-Encoding", "Expires"=>"Thu, 14 Sep 2017 15:52:24 GMT", "Cache-Control"=>"max-age=0, no-cache", "Pragma"=>"no-cache", "Date"=>"Thu, 14 Sep 2017 15:52:24 GMT", "Transfer-Encoding"=>"chunked", "Connection"=>"close, Transfer-Encoding"}
  id = get_created_id_from_response_headers(self.base_jd_resource, response)
  if id.nil? then
    return nil
  else
    return self.new(HashUtils.deep_stringify_keys({"id" => id}.merge(body)))
  end
end

#validate_access_token(access_token) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
# File 'lib/myjohndeere/rest_methods.rb', line 55

def validate_access_token(access_token)
  raise ArgumentError.new("The first argument must be an #{AccessToken}") if !access_token.is_a?(AccessToken)
end