Class: RubyShift::Request

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rubyshift/request.rb

Direct Known Subclasses

API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



12
13
14
# File 'lib/rubyshift/request.rb', line 12

def access_token
  @access_token
end

#endpointObject

Returns the value of attribute endpoint.



12
13
14
# File 'lib/rubyshift/request.rb', line 12

def endpoint
  @endpoint
end

Class Method Details

.decode(response) ⇒ Object



40
41
42
43
44
# File 'lib/rubyshift/request.rb', line 40

def self.decode(response)
  JSON.load response
rescue JSON::ParserError
  raise Error::Parsing.new "The response is not a valid JSON"      
end

.parse(body) ⇒ Object



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
# File 'lib/rubyshift/request.rb', line 14

def self.parse(body)
  data = decode(body)['data']

  if data.is_a? Hash
    data.delete('links')
    data.delete('members')
    OpenStruct.new data
  elsif data.is_a? Array
    data.collect! { |d|
      d.delete('links')
      d.delete('members')
      OpenStruct.new(d)
    }
  elsif data
    true
  elsif !data
    false
  elsif data.nil?
    false
  else
    raise Error::Parsing.new "Couldn't parse the response body: #{body.inspect}" 
  end
rescue
  raise Error::Parsing.new "Couldn't parse the response body: #{body.inspect}"
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



64
65
66
67
68
# File 'lib/rubyshift/request.rb', line 64

def delete(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.delete(@endpoint + path, options)
end

#get(path, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/rubyshift/request.rb', line 46

def get(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.get(@endpoint + path, options)
end

#post(path, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/rubyshift/request.rb', line 52

def post(path, options={})
  set_httparty_config(options)
  set_authorization_header(options, path)
  validate self.class.post(@endpoint + path, options)
end

#put(path, options = {}) ⇒ Object



58
59
60
61
62
# File 'lib/rubyshift/request.rb', line 58

def put(path, options={})
  set_httparty_config(options)
  set_authorization_header(options)
  validate self.class.put(@endpoint + path, options)
end

#set_request_defaults(httparty = nil) ⇒ Object

Sets default_params for requests.

Raises:



94
95
96
97
# File 'lib/rubyshift/request.rb', line 94

def set_request_defaults(httparty=nil)
  raise Error::MissingCredentials.new("Please set an API endpoint") unless @endpoint
  #self.class.default_params[:httparty] = {} if httparty.nil?
end

#validate(response) ⇒ Object

Checks the response code for common errors. Returns parsed response for successful requests.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubyshift/request.rb', line 72

def validate(response)
  case response.code
  when 400 then fail Error::BadRequest.new error_message(response)
  when 401 then fail Error::Unauthorized.new error_message(response)
  when 403 then fail Error::Forbidden.new error_message(response)
  when 404 then fail Error::NotFound.new error_message(response)
  when 405 then fail Error::MethodNotAllowed.new error_message(response)
  when 409 then fail Error::Conflict.new error_message(response)
  when 422 then fail Error::Unprocessable.new error_message(response)
  when 500 then fail Error::InternalServerError.new error_message(response)
  when 502 then fail Error::BadGateway.new error_message(response)
  when 503 then fail Error::ServiceUnavailable.new error_message(response)
  end

  parsed = response.parsed_response
  parsed.client = self if parsed.respond_to?(:client=)
  parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!)
  parsed
end