rxg_client 0.0.2 → 1.1.1

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 +5 -5
  2. data/lib/rxg_client.rb +116 -36
  3. metadata +5 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 68761d8e4d49bbe48b4371ea2d9d3c70cd3aac75
4
- data.tar.gz: b45bc993937e321af2f95061985e0b2ae6ad1f1f
2
+ SHA256:
3
+ metadata.gz: b1701747767676c3880b62220730a35f0f8f44c7e5a6300219e677f51699498c
4
+ data.tar.gz: ef34d6aca636d4bde2a0874e7377d87ba8fe088d984fb0d0033d969bdae710e9
5
5
  SHA512:
6
- metadata.gz: 0892a54598001398d715d0c066812fb322155dec90dc93f8530cd80f8b397316894e955c4e79cc814cbc4786e17736499a1d5f5277f1cda46daec97c1f78a7aa
7
- data.tar.gz: 4713ef391b5f1f65c2cf386b6b0a243be0541793a4f7f53dfc38a02d5133b65c96be27df88af48cbda7f6ba4543f6a1376c9c3bc8cb267d1e63013bc73472a99
6
+ metadata.gz: ede8e416e727eb38cb3f7b330bcb6c78972196bad4034076d74889ccc78f5b3e68bc647baf0865125a128a32a0bc04cacca687aed8a9ad479be642c4e2672198
7
+ data.tar.gz: cb7242e69c10248b585456fc5cc9c464bd7d6e1abad06e78f9a5beae7ba1056123b6d5904c4ed9ecb73a24a558fd9d9733a4e65b472bb4ad19ddfae9e9b5312e
@@ -1,44 +1,127 @@
1
1
  class RxgClient
2
- require 'delegate'
3
2
  require 'httparty'
4
3
 
5
4
  include HTTParty
6
5
 
7
- attr_accessor :api_key, :hostname, :request_format, :raise_exceptions
6
+ attr_accessor :api_key, :hostname, :base_uri, :fleet, :request_format,
7
+ :raise_exceptions, :verify_ssl, :auth_method, :default_timeout, :debug_output
8
8
 
9
9
  def request_format= (requested_format)
10
10
  raise HTTParty::UnsupportedFormat unless [ :json, :xml ].include?(requested_format.to_sym)
11
11
  @request_format = requested_format
12
12
  end
13
13
 
14
- def initialize(hostname, api_key, options = { })
15
- # Valid options:
16
- # :request_format => :json or :xml
17
- # :default_timeout => timeout in seconds
18
- # :raise_exceptions => true or false
19
- # :verify_ssl => true or false
14
+ # The following options can be configured when initializing the client:
15
+ # - default_timeout: the amount of time in seconds to wait for a response.
16
+ # default is 5
17
+ # - raise_exceptions: true or false.
18
+ # default is true
19
+ # - verify_ssl: true or false.
20
+ # default is true
21
+ # If using an IP, must be false.
22
+ # - fleet: pass true if authentication should use the 'fleetkey' header
23
+ # instead of apikey.
24
+ # default is false
25
+ # - auth_method: must be one of: :headers, :query
26
+ # default is :headers
27
+ # If fleet is true, headers will always be used
28
+ # - debug: pass a logger or $stdout to have debug_output logged, or nil to disable.
29
+ # default is nil
30
+ # - base_uri: provide an alternative base_uri, either a full URL or just the
31
+ # path to append to the hostname.
32
+ # default uses the admin/scaffolds context to access the traditional API
33
+ def initialize(hostname, api_key, request_format: :json, default_timeout: 5,
34
+ raise_exceptions: false, verify_ssl: false, fleet: false, debug_output: nil,
35
+ base_uri: 'admin/scaffolds', auth_method: :headers)
20
36
 
21
37
  self.api_key = api_key
22
-
23
- self.request_format = options[:request_format] ? options[:request_format].to_sym : :json
24
- self.class.format self.request_format
25
38
 
26
39
  self.hostname = hostname
27
- self.class.base_uri "https://#{self.hostname}/admin/scaffolds"
28
-
29
- self.class.default_timeout options[:timeout] || 5
30
40
 
31
- self.raise_exceptions = options[:raise_exceptions] || false
41
+ self.set_base_uri(base_uri)
32
42
 
33
- if defined?(options[:verify_ssl])
34
- self.class.default_options.update(verify: options[:verify_ssl])
43
+ self.fleet = fleet
44
+
45
+ self.default_timeout = default_timeout
46
+
47
+ self.raise_exceptions = raise_exceptions
48
+
49
+ self.verify_ssl = verify_ssl
50
+
51
+ self.debug_output = debug_output
52
+
53
+ self.request_format = request_format.to_sym
54
+
55
+ self.auth_method = auth_method
56
+
57
+ end
58
+
59
+
60
+ # change the active base_uri
61
+ def set_base_uri(base_uri)
62
+ if base_uri =~ /^https?:\/\//
63
+ self.base_uri = base_uri
64
+ else
65
+ self.base_uri = "https://#{self.hostname}/#{base_uri.delete_prefix('/')}"
66
+ end
67
+ end
68
+
69
+ # temporarily change the base_uri for the duration of the provided block, then
70
+ # change it back to its previous value
71
+ def with_base_uri(new_base_uri, &blk)
72
+ if block_given?
73
+ begin
74
+ old_uri = self.base_uri
75
+
76
+ set_base_uri(new_base_uri)
77
+
78
+ blk.call
79
+ ensure
80
+ set_base_uri(old_uri)
81
+ end
82
+ end
83
+ end
84
+
85
+
86
+
87
+ def default_header
88
+ @headers ||= begin
89
+ h = { 'Accept' => "application/#{self.request_format}" }
90
+ if self.fleet
91
+ h['fleetkey'] = self.api_key
92
+ elsif self.auth_method == :headers # compatible with rXg version 11.442 or later
93
+ h['apikey'] = self.api_key
94
+ end
95
+ h
35
96
  end
36
97
  end
37
98
 
38
- def auth
39
- {api_key: self.api_key}
99
+ def default_query
100
+ case self.auth_method
101
+ when :query
102
+ { api_key: self.api_key }
103
+ when :headers
104
+ { }
105
+ end
40
106
  end
41
107
 
108
+ %i(post get put patch delete).each do |http_method|
109
+ define_method(http_method) do |action, **args|
110
+ action = "/#{action.to_s.delete_prefix('/')}"
111
+ default_args = {
112
+ :headers => self.default_header.merge(args.delete(:headers) || {}),
113
+ :query => self.default_query.merge(args.delete(:query) || {}).presence,
114
+ :base_uri => self.base_uri,
115
+ :timeout => self.default_timeout,
116
+ :format => self.request_format,
117
+ :debug_output => self.debug_output
118
+ }
119
+ response = self.class.send(http_method, action, **default_args.merge(args))
120
+ response.success? ? self.parse(response.body) : raise(response.message)
121
+ end
122
+ end
123
+
124
+
42
125
  def parse(body)
43
126
  return {success: true} if body == ""
44
127
  begin
@@ -47,7 +130,7 @@ class RxgClient
47
130
  result = JSON.parse(body)
48
131
  when :xml
49
132
  result = Hash.from_xml(body)
50
- else
133
+ else
51
134
  raise "Request format should be one of: :json, :xml"
52
135
  end
53
136
 
@@ -69,32 +152,31 @@ class RxgClient
69
152
 
70
153
  # create a record in the given table with the attributes provided in new_record
71
154
  def create(table, new_record)
72
- response = self.class.post("/#{table}/create/index.#{self.request_format}", { body: {record: new_record}.merge(self.auth) })
73
- response.success? ? self.parse(response.body) : raise(response.message)
155
+ self.post("/#{table}/create", body: {record: new_record})
74
156
  end
75
-
157
+
76
158
  # list all records from the given table
77
159
  def list(table)
78
- response = self.class.get("/#{table}/index.#{self.request_format}", {body: self.auth})
79
- response.success? ? self.parse(response.body) : raise(response.message)
160
+ self.get("/#{table}")
161
+ end
162
+
163
+ def search(table, search_params)
164
+ self.post("/#{table}/index", body: search_params)
80
165
  end
81
166
 
82
167
  # return the record from the given table having the given id
83
168
  def show(table, id)
84
- response = self.class.get("/#{table}/show/#{id}.#{self.request_format}", {body: self.auth})
85
- response.success? ? self.parse(response.body) : raise(response.message)
169
+ self.get("/#{table}/show/#{id}")
86
170
  end
87
171
 
88
172
  # update a record from the given table, having the given id, with the updated attributes provided in updated_record_hash
89
173
  def update(table, id, updated_record_hash)
90
- response = self.class.post("/#{table}/update/#{id}.#{self.request_format}", {body: {record: updated_record_hash}.merge(self.auth)})
91
- response.success? ? self.parse(response.body) : raise(response.message)
174
+ self.post("/#{table}/update/#{id}", body: {record: updated_record_hash})
92
175
  end
93
176
 
94
177
  # destroy a record from the given table having the given id
95
178
  def destroy(table, id)
96
- response = self.class.post("/#{table}/destroy/#{id}.#{self.request_format}", {body: self.auth})
97
- response.success? ? self.parse(response.body) : raise(response.message)
179
+ self.post("/#{table}/destroy/#{id}")
98
180
  end
99
181
 
100
182
  def execute(table, request)
@@ -106,8 +188,7 @@ class RxgClient
106
188
  # method_args - A serialized Array or Hash of the argument(s) expected by the method.
107
189
  # example method call:
108
190
  # 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]})
109
- response = self.class.post("/#{table}/execute.#{self.request_format}", {query: self.auth, body: {request: request}})
110
- response.success? ? self.parse(response.body) : raise(response.message)
191
+ self.post("/#{table}/execute", body: {request: request})
111
192
  end
112
193
 
113
194
  private
@@ -129,12 +210,11 @@ class RxgClient
129
210
  end
130
211
 
131
212
  # The listed methods will be rescued from all StandardError exceptions, and the code within
132
- # the block will be executed.
133
- rescue_from StandardError, :create, :list, :show, :update, :destroy, :execute do |exception, instance|
213
+ # the block will be executed.
214
+ rescue_from StandardError, :create, :list, :show, :update, :destroy, :execute, :search do |exception, instance|
134
215
  puts exception.message
135
216
 
136
217
  raise exception if instance.raise_exceptions
137
218
  end
138
219
 
139
-
140
220
  end
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: 0.0.2
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lannar Dean
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-17 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -25,14 +25,14 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.10.0
27
27
  description: A simple CRUDE (Create, Read, Update, Delete, Execute) client to interface
28
- with the RXG's API
28
+ with the rXg's API
29
29
  email: ldd@rgnets.com
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - lib/rxg_client.rb
35
- homepage: https://github.com/moracca/rxg_client
35
+ homepage: https://github.com/rgnets/rxg_client
36
36
  licenses:
37
37
  - MIT
38
38
  metadata: {}
@@ -51,8 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubyforge_project:
55
- rubygems_version: 2.6.11
54
+ rubygems_version: 3.0.6
56
55
  signing_key:
57
56
  specification_version: 4
58
57
  summary: RXG API Client