Class: RubyWsOne::Request
- Inherits:
-
Object
- Object
- RubyWsOne::Request
- Includes:
- Finest::Builder, HTTParty
- Defined in:
- lib/ruby_ws_one.rb
Overview
Request is the base class that manages basic calls in order to get a Brearer token as well as the generic http_method call that manages all request back and forth from workspace one API.
Instance Attribute Summary collapse
-
#aw_tenant_code ⇒ Object
Returns the value of attribute aw_tenant_code.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#system ⇒ Object
readonly
Returns the value of attribute system.
Instance Method Summary collapse
- #http_method(args = {}) ⇒ Object
-
#initialize(json = {}) ⇒ Request
constructor
A new instance of Request.
-
#token ⇒ Object
Get a Bearer token from one of the Region-Specific Token URLs and keeps it in cache.
-
#transform(res, args) ⇒ Object
Parses the response from workspace one API using a open struct approach to make easier to access the data.
Constructor Details
#initialize(json = {}) ⇒ Request
Returns a new instance of Request.
43 44 45 46 47 |
# File 'lib/ruby_ws_one.rb', line 43 def initialize(json = {}) super(json) @client_secret ||= json[:client_secret] @client_id ||= json[:client_id] end |
Instance Attribute Details
#aw_tenant_code ⇒ Object
Returns the value of attribute aw_tenant_code.
41 42 43 |
# File 'lib/ruby_ws_one.rb', line 41 def aw_tenant_code @aw_tenant_code end |
#client_id ⇒ Object
Returns the value of attribute client_id.
41 42 43 |
# File 'lib/ruby_ws_one.rb', line 41 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
41 42 43 |
# File 'lib/ruby_ws_one.rb', line 41 def client_secret @client_secret end |
#domain ⇒ Object
Returns the value of attribute domain.
41 42 43 |
# File 'lib/ruby_ws_one.rb', line 41 def domain @domain end |
#system ⇒ Object (readonly)
Returns the value of attribute system.
40 41 42 |
# File 'lib/ruby_ws_one.rb', line 40 def system @system end |
Instance Method Details
#http_method(args = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby_ws_one.rb', line 70 def http_method(args = {}) _headers = { authorization: "Bearer #{token[:access_token]}", 'aw-tenant-code': RubyWsOne.aw_tenant_code, 'Content-Type': 'application/json', Accept: "application/json;version=#{args.fetch(:version, 2)}" } _url = RubyWsOne.rest_url % { domain: RubyWsOne.domain || args[:domain], system: args.fetch(:system, @system), element: args.fetch(:element, 'devices'), id: args.fetch(:id, nil), action: args.fetch(:action, nil), command: args.fetch(:command, nil), action_id: args.fetch(:action_id, nil), filter: args.fetch(:filter, {}).to_query } self.class.method(args.fetch(:method, :get)).call(_url, body: args.fetch(:body, {}).to_json, headers: _headers) rescue StandardError => e { error: e&. || 'Error connecting to manager service provider', status: :not_found } end |
#token ⇒ Object
Get a Bearer token from one of the Region-Specific Token URLs and keeps it in cache. Token will expire in 30 minutes and will acquire one on the very next call.
The token URL region has to match where the area your tenant is hosted, otherwise this call will return a invalid_request error.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby_ws_one.rb', line 56 def token Rails.cache.fetch(@client_id, expires_in: 30.minutes) do JSON.parse( http_method_token( body: { grant_type: :client_credentials, client_id: @client_id, client_secret: @client_secret } )&.body )&.transform_keys(&:to_sym) end end |
#transform(res, args) ⇒ Object
Parses the response from workspace one API using a open struct approach to make easier to access the data. It will return an instance of the object requested, ie. Device,User
As part of the response will also include the client_id as well as client_secret to be able to call the API on every object.
user = RubyWsOne::User.new(@credentials).search(filter: { email: '[email protected]' })
resp = user.first.delete(true)
101 102 103 104 105 106 107 108 |
# File 'lib/ruby_ws_one.rb', line 101 def transform(res, args) _resp = JSON.parse(res.body.presence || {}.to_s).transform_keys(&:downcase) return self.class.new(_resp.merge(get_instance_variables)) unless _resp[args[:element]&.to_s] Array.wrap(_resp[args[:element]&.to_s]).map! do |elem| self.class.new(elem.transform_keys(&:downcase).merge(get_instance_variables)) end end |