Class: MyJohnDeere::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/myjohndeere/util.rb

Class Method Summary collapse

Class Method Details

.build_path_headers_and_body(method, path, headers: {}, body: "", etag: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49
50
51
52
# File 'lib/myjohndeere/util.rb', line 3

def self.build_path_headers_and_body(method, path, headers: {}, body: "", etag: nil)
  # in the case of following one of their paths, just clear out the base
  path = path.sub(MyJohnDeere.configuration.endpoint, '')
  path = "/#{path}" if not path.start_with?("/")
  # always trim platform from the beginning as we have that in our base
  path = path.sub(/\A\/?platform/, "")

  default_headers = nil
  if method == :post || method == :put then
    body = body.to_json() if body.is_a?(Hash)
    default_headers = MyJohnDeere::DEFAULT_POST_HEADER.dup
    content_length = body.length
    default_headers["Content-Length"] ||= body.length.to_s if content_length > 0
  else
    default_headers = MyJohnDeere::DEFAULT_REQUEST_HEADER
  end
  headers = default_headers.merge(headers || {})

  if REQUEST_METHODS_TO_PUT_PARAMS_IN_URL.include?(method) then
    if !etag.nil? then
      # Pass an empty string to have it start
      headers[MyJohnDeere::ETAG_HEADER_KEY] = etag
    end

    # we'll only accept hashes for the body for now
    if body.is_a?(Hash) then
      uri = URI.parse(path)
      new_query_ar = URI.decode_www_form(uri.query || '')
      
      # For reasons beyond me, these are specified as non-parameters
      special_parameters = {}
      SPECIAL_BODY_PARAMETERS.each do |sbp|
        special_parameters[sbp] = body.delete(sbp)
      end

      body.each do |key, val|
        new_query_ar << [key.to_s, val.to_s]
      end
      special_parameters.each do |key,val|
        next if val.nil?
        query_string = "#{key}=#{val}"
        uri.path = "#{uri.path};#{query_string}" if !uri.path.include?(query_string)
      end
      uri.query = URI.encode_www_form(new_query_ar)
      path = uri.to_s
    end
  end

  return path, headers, body
end

.handle_response_error_codes(response) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/myjohndeere/util.rb', line 54

def self.handle_response_error_codes(response)
  headers = response.to_hash
  code = response.code.to_i
  body = response.body
  error = nil
  case code
  when 503
    error = ServerBusyError
  when 400, 404
    error = InvalidRequestError
  when 401
    error = AuthenticationError
  when 403
    error = PermissionError
  when 429
    error = RateLimitError
  when 500
    error = InternalServerError
  end
    
  if error.nil? then
    return
  else
    error = error.new(
      http_status: code, http_body: body,
      http_headers: headers)
    error.response = response
    raise error
  end
end