module MyJohnDeere::RESTMethods::ClassMethods

Attributes

base_jd_resource[RW]
list_resource_path[RW]
retrieve_resource_path[RW]
supports_delete[RW]

Public Instance Methods

build_resource_base_path!(resource_path, options = {}) click to toggle source
# File lib/myjohndeere/rest_methods.rb, line 57
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, base_resources
  rescue KeyError
    raise ArgumentError.new("You must specify #{expected_definitions.join(", ")} as part of this request path")
  end
end
delete(access_token, id) click to toggle source
# File lib/myjohndeere/rest_methods.rb, line 50
def delete(access_token, id)
  raise UnsupportedRequestError.new("Delete is not supported by this resource") if !self.supports_delete

  response = access_token.execute_request(:delete, "#{self.base_jd_resource}/#{id}")
  return response.code == 204
end
list(access_token, options = {}) click to toggle source

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

# File lib/myjohndeere/rest_methods.rb, line 10
def list(access_token, options = {})
  validate_access_token(access_token)
  options = {count: 10, start: 0, etag: nil}.merge(options)
  if !options[:etag].nil? then
    options.delete(:count)
    options.delete(:start)
  end
  options[:body] ||= {}
  # The count and start are in this list,so move them into the body
  SPECIAL_BODY_PARAMETERS.each do |sbp|
    next if options[sbp].nil?
    options[:body][sbp] = options[sbp]
  end

  path, base_resources = build_resource_base_path!(self.list_resource_path, options)
  response = access_token.execute_request(:get, path, 
    options
  )
  return ListObject.new(
    self,
    access_token,
    response.data,
    response.code,
    base_resources,
    options: options.merge(
      etag: response.http_headers[MyJohnDeere::ETAG_HEADER_KEY]
    )
  )
end
retrieve(access_token, id, options={}) click to toggle source
# File lib/myjohndeere/rest_methods.rb, line 40
def retrieve(access_token, id, options={})
  validate_access_token(access_token)
  path, = build_resource_base_path!(self.retrieve_resource_path, options)
  response = access_token.execute_request(:get, 
    "#{path}/#{id}",
    options)

  return new(response.data, access_token)
end
send_create(access_token, body, path_builder_options = {}) click to toggle source
# File lib/myjohndeere/rest_methods.rb, line 76
def send_create(access_token, body, path_builder_options = {})
  path, = build_resource_base_path!(self.list_resource_path, path_builder_options)
  response = access_token.execute_request(:post, 
    path,
    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) click to toggle source
# File lib/myjohndeere/rest_methods.rb, line 72
def validate_access_token(access_token)
  raise ArgumentError.new("The first argument must be an #{AccessToken}") if !access_token.is_a?(AccessToken)
end