Class: RubyWsOne::Request

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Device, User

Instance Attribute Summary collapse

Instance Method Summary collapse

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_codeObject

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_idObject

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_secretObject

Returns the value of attribute client_secret.



41
42
43
# File 'lib/ruby_ws_one.rb', line 41

def client_secret
  @client_secret
end

#domainObject

Returns the value of attribute domain.



41
42
43
# File 'lib/ruby_ws_one.rb', line 41

def domain
  @domain
end

#systemObject (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&.message || 'Error connecting to manager service provider', status: :not_found }
end

#tokenObject

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