rxg_client 1.0.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rxg_client.rb +93 -33
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bef3b2acf98fd65afe6eb1147133d1d9bbe5cfd86d8e1a3e0a71ec90069d04af
4
- data.tar.gz: bfd2186b41c7220bf278e0aac274056abb8d778a2fffe8b909b8350991f7d6ca
3
+ metadata.gz: fd7c586f4282d7652ad39640f1f9f580ca51edc17e1c3b6ee2b9e18b7ad2ec8f
4
+ data.tar.gz: 4fd801c3d1e00b240a86092816460d2718127474fcf962f2ab8b0f3afc91f904
5
5
  SHA512:
6
- metadata.gz: f9cf653e5dc2909c2ca35cd7540bf5ad9078d3cc22002e75c4c31641f1e7d68133ab9a27b23e7ea9415fa787c24c8e42864d363e2f89004d9203963146d2e14a
7
- data.tar.gz: 89c833d17d2061e306d343e2609bdc396e199935a7454958826fd4cd3ffb60866d3c4ef574727927578d041265815fcc15566ac2449ee2cf86638f9878ffbb8c
6
+ metadata.gz: c54d7b8167c68f5a1a001cd2bd8e2052694c640c718ee62cb295d821aa7ce50e855ac423864fdb0192d0522e23f63256e0a347e1d150468a23c6f4cf31583b55
7
+ data.tar.gz: 1c708ce608bd7eb575febb10d860b9a077ac159b72f42373f90bdfa230fb9e377f8b33ac04476871b0f6136a8149a8e3ceb01eff3b65d80fd39884d066c3b7ab
data/lib/rxg_client.rb CHANGED
@@ -3,7 +3,9 @@ class RxgClient
3
3
 
4
4
  include HTTParty
5
5
 
6
- attr_accessor :api_key, :hostname, :request_format, :raise_exceptions, :auth
6
+ attr_accessor :api_key, :hostname, :base_uri, :fleet, :request_format,
7
+ :raise_exceptions, :verify_ssl, :auth_method, :default_timeout, :debug_output,
8
+ :response_headers
7
9
 
8
10
  def request_format= (requested_format)
9
11
  raise HTTParty::UnsupportedFormat unless [ :json, :xml ].include?(requested_format.to_sym)
@@ -26,42 +28,107 @@ class RxgClient
26
28
  # If fleet is true, headers will always be used
27
29
  # - debug: pass a logger or $stdout to have debug_output logged, or nil to disable.
28
30
  # default is nil
31
+ # - base_uri: provide an alternative base_uri, either a full URL or just the
32
+ # path to append to the hostname.
33
+ # default uses the admin/scaffolds context to access the traditional API
29
34
  def initialize(hostname, api_key, request_format: :json, default_timeout: 5,
30
- raise_exceptions: false, verify_ssl: false, fleet: false, debug: nil,
31
- auth_method: :headers)
35
+ raise_exceptions: false, verify_ssl: false, fleet: false, debug_output: nil,
36
+ base_uri: 'admin/scaffolds', auth_method: :headers)
32
37
 
33
38
  self.api_key = api_key
34
39
 
35
- self.request_format = request_format.to_sym
36
- self.class.format self.request_format
37
-
38
40
  self.hostname = hostname
39
- self.class.base_uri "https://#{self.hostname}/admin/scaffolds"
40
41
 
41
- self.class.default_timeout default_timeout
42
+ self.set_base_uri(base_uri)
43
+
44
+ self.fleet = fleet
45
+
46
+ self.default_timeout = default_timeout
42
47
 
43
48
  self.raise_exceptions = raise_exceptions
44
49
 
45
- self.class.default_options.update(verify: verify_ssl)
50
+ self.verify_ssl = verify_ssl
46
51
 
47
- self.class.debug_output debug
52
+ self.debug_output = debug_output
48
53
 
49
- case auth_method
50
- when :headers # compatible with rXg version 11.442 or later
51
- if fleet
52
- self.class.headers({ 'fleetkey' => self.api_key })
53
- else
54
- self.class.headers({ 'api_key' => self.api_key })
54
+ self.request_format = request_format.to_sym
55
+
56
+ self.auth_method = auth_method
57
+
58
+ end
59
+
60
+
61
+ # change the active base_uri
62
+ def set_base_uri(base_uri)
63
+ if base_uri =~ /^https?:\/\//
64
+ self.base_uri = base_uri
65
+ else
66
+ self.base_uri = "https://#{self.hostname}/#{base_uri.delete_prefix('/')}"
67
+ end
68
+ end
69
+
70
+ # temporarily change the base_uri for the duration of the provided block, then
71
+ # change it back to its previous value
72
+ def with_base_uri(new_base_uri, &blk)
73
+ if block_given?
74
+ begin
75
+ old_uri = self.base_uri
76
+
77
+ set_base_uri(new_base_uri)
78
+
79
+ blk.call
80
+ ensure
81
+ set_base_uri(old_uri)
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+
88
+ def default_header
89
+ @headers ||= begin
90
+ h = { 'Accept' => "application/#{self.request_format}" }
91
+ if self.fleet
92
+ h['fleetkey'] = self.api_key
93
+ elsif self.auth_method == :headers # compatible with rXg version 11.442 or later
94
+ h['apikey'] = self.api_key
55
95
  end
96
+ h
97
+ end
98
+ end
99
+
100
+ def default_query
101
+ case self.auth_method
56
102
  when :query
57
- if fleet
58
- self.class.headers({ 'fleetkey' => self.api_key })
103
+ { api_key: self.api_key }
104
+ when :headers
105
+ { }
106
+ end
107
+ end
108
+
109
+ %i(post get put patch delete).each do |http_method|
110
+ define_method(http_method) do |action, **args|
111
+ action = "/#{action.to_s.delete_prefix('/')}"
112
+ default_args = {
113
+ :headers => self.default_header.merge(args.delete(:headers) || {}),
114
+ :query => self.default_query.merge(args.delete(:query) || {}).presence,
115
+ :base_uri => self.base_uri,
116
+ :timeout => self.default_timeout,
117
+ :format => self.request_format,
118
+ :debug_output => self.debug_output,
119
+ :verify => self.verify_ssl
120
+ }
121
+ response = self.class.send(http_method, action, **default_args.merge(args))
122
+ if response.success?
123
+ self.response_headers = response.headers
124
+ self.parse(response.body)
59
125
  else
60
- self.class.default_params({ 'api_key' => self.api_key })
126
+ raise(response.message)
61
127
  end
62
128
  end
63
129
  end
64
130
 
131
+
65
132
  def parse(body)
66
133
  return {success: true} if body == ""
67
134
  begin
@@ -92,37 +159,31 @@ class RxgClient
92
159
 
93
160
  # create a record in the given table with the attributes provided in new_record
94
161
  def create(table, new_record)
95
- response = self.class.post("/#{table}/create/index.#{self.request_format}", body: {record: new_record})
96
- response.success? ? self.parse(response.body) : raise(response.message)
162
+ self.post("/#{table}/create", body: {record: new_record})
97
163
  end
98
164
 
99
165
  # list all records from the given table
100
166
  def list(table)
101
- response = self.class.get("/#{table}/index.#{self.request_format}")
102
- response.success? ? self.parse(response.body) : raise(response.message)
167
+ self.get("/#{table}")
103
168
  end
104
169
 
105
170
  def search(table, search_params)
106
- response = self.class.post("/#{table}/index.#{self.request_format}", body: search_params)
107
- response.success? ? self.parse(response.body) : raise(response.message)
171
+ self.post("/#{table}/index", body: search_params)
108
172
  end
109
173
 
110
174
  # return the record from the given table having the given id
111
175
  def show(table, id)
112
- response = self.class.get("/#{table}/show/#{id}.#{self.request_format}")
113
- response.success? ? self.parse(response.body) : raise(response.message)
176
+ self.get("/#{table}/show/#{id}")
114
177
  end
115
178
 
116
179
  # update a record from the given table, having the given id, with the updated attributes provided in updated_record_hash
117
180
  def update(table, id, updated_record_hash)
118
- response = self.class.post("/#{table}/update/#{id}.#{self.request_format}", body: {record: updated_record_hash})
119
- response.success? ? self.parse(response.body) : raise(response.message)
181
+ self.post("/#{table}/update/#{id}", body: {record: updated_record_hash})
120
182
  end
121
183
 
122
184
  # destroy a record from the given table having the given id
123
185
  def destroy(table, id)
124
- response = self.class.post("/#{table}/destroy/#{id}.#{self.request_format}")
125
- response.success? ? self.parse(response.body) : raise(response.message)
186
+ self.post("/#{table}/destroy/#{id}")
126
187
  end
127
188
 
128
189
  def execute(table, request)
@@ -134,8 +195,7 @@ class RxgClient
134
195
  # method_args - A serialized Array or Hash of the argument(s) expected by the method.
135
196
  # example method call:
136
197
  # node.execute("shared_credential_groups", {record_id: 7, method_name: "make_login_session", method_args:["192.168.20.111", "00:00:00:00:00:05", "test", 1]})
137
- response = self.class.post("/#{table}/execute.#{self.request_format}", body: {request: request})
138
- response.success? ? self.parse(response.body) : raise(response.message)
198
+ self.post("/#{table}/execute", body: {request: request})
139
199
  end
140
200
 
141
201
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rxg_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lannar Dean
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-15 00:00:00.000000000 Z
11
+ date: 2021-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -36,7 +36,7 @@ homepage: https://github.com/rgnets/rxg_client
36
36
  licenses:
37
37
  - MIT
38
38
  metadata: {}
39
- post_install_message:
39
+ post_install_message:
40
40
  rdoc_options: []
41
41
  require_paths:
42
42
  - lib
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  version: '0'
53
53
  requirements: []
54
54
  rubygems_version: 3.0.6
55
- signing_key:
55
+ signing_key:
56
56
  specification_version: 4
57
57
  summary: RXG API Client
58
58
  test_files: []