Class: AqumulateAPI::Session
- Inherits:
-
Object
- Object
- AqumulateAPI::Session
- Defined in:
- lib/aqumulate_api/session.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
readonly
Returns the value of attribute auth.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #api_request(resource, body) ⇒ Object
- #check_raise_request_error(response) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize ⇒ Session
constructor
A new instance of Session.
- #login ⇒ Object
- #response_has_error?(response) ⇒ Boolean
Constructor Details
#initialize ⇒ Session
Returns a new instance of Session.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/aqumulate_api/session.rb', line 19 def initialize if block_given? @config = AqumulateAPI::Configuration.new yield(@config) else @config = AqumulateAPI.config end login end |
Instance Attribute Details
#auth ⇒ Object (readonly)
Returns the value of attribute auth.
17 18 19 |
# File 'lib/aqumulate_api/session.rb', line 17 def auth @auth end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
17 18 19 |
# File 'lib/aqumulate_api/session.rb', line 17 def config @config end |
Instance Method Details
#api_request(resource, body) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/aqumulate_api/session.rb', line 61 def api_request(resource, body) response = HTTParty.post( "#{config.url}/api/#{resource}", headers: { 'Content-Type' => 'application/json', 'Authorization' => "bearer #{auth['access_token']}" }, body: body.to_json, timeout: config.timeout ) if config.debug puts response.request.inspect puts response.inspect end handle_response response end |
#check_raise_request_error(response) ⇒ Object
80 81 82 |
# File 'lib/aqumulate_api/session.rb', line 80 def check_raise_request_error(response) raise RequestError.new(response['ErrorMessage'], response) if response_has_error?(response) end |
#handle_response(response) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aqumulate_api/session.rb', line 50 def handle_response(response) if response.parsed_response.is_a?(Hash) && response.parsed_response.has_key?('error') raise AuthenticationError.new(response.parsed_response['error_description'], response.request) elsif response.code != 200 raise RequestError.new(response.response.msg, response.response) end response.parsed_response end |
#login ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/aqumulate_api/session.rb', line 30 def login response = HTTParty.post( "#{config.url}/token", headers: { 'Accept' => 'application/x-www-form-urlencoded' }, body: { grant_type: config.grant_type, 'userName' => config.username, password: config.password }, timeout: config.timeout ) if config.debug puts response.request.inspect puts response.inspect end @auth = handle_response response end |
#response_has_error?(response) ⇒ Boolean
84 85 86 |
# File 'lib/aqumulate_api/session.rb', line 84 def response_has_error?(response) response.has_key?('ErrorMessage') && !response['ErrorMessage'].to_s.empty? end |