lib/myjohndeere/rest_methods.rb in myjohndeere-0.0.2 vs lib/myjohndeere/rest_methods.rb in myjohndeere-0.0.3
- old
+ new
@@ -13,11 +13,11 @@
# 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_resouce_base_path!(self.list_resource_path, options),
+ response = access_token.execute_request(:get, build_resource_base_path!(self.list_resource_path, options),
options
)
return ListObject.new(
self,
access_token,
@@ -29,27 +29,46 @@
end
def retrieve(access_token, id, options={})
validate_access_token(access_token)
response = access_token.execute_request(:get,
- "#{build_resouce_base_path!(self.retrieve_resource_path, options)}/#{id}",
+ "#{build_resource_base_path!(self.retrieve_resource_path, options)}/#{id}",
options)
return new(response.data, access_token)
end
- def build_resouce_base_path!(resource_path, options)
+ 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
- return resource_path if base_resources.nil? || base_resources.empty?
MyJohnDeere.logger.info("Building resource path: #{resource_path}, with ids: #{base_resources}")
- return resource_path % 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
def validate_access_token(access_token)
raise ArgumentError.new("The first argument must be an #{AccessToken}") if !access_token.is_a?(AccessToken)
+ end
+
+ 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
end
module InstanceMethods
\ No newline at end of file