rxg_client 0.0.2

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 +7 -0
  2. data/lib/rxg_client.rb +140 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68761d8e4d49bbe48b4371ea2d9d3c70cd3aac75
4
+ data.tar.gz: b45bc993937e321af2f95061985e0b2ae6ad1f1f
5
+ SHA512:
6
+ metadata.gz: 0892a54598001398d715d0c066812fb322155dec90dc93f8530cd80f8b397316894e955c4e79cc814cbc4786e17736499a1d5f5277f1cda46daec97c1f78a7aa
7
+ data.tar.gz: 4713ef391b5f1f65c2cf386b6b0a243be0541793a4f7f53dfc38a02d5133b65c96be27df88af48cbda7f6ba4543f6a1376c9c3bc8cb267d1e63013bc73472a99
data/lib/rxg_client.rb ADDED
@@ -0,0 +1,140 @@
1
+ class RxgClient
2
+ require 'delegate'
3
+ require 'httparty'
4
+
5
+ include HTTParty
6
+
7
+ attr_accessor :api_key, :hostname, :request_format, :raise_exceptions
8
+
9
+ def request_format= (requested_format)
10
+ raise HTTParty::UnsupportedFormat unless [ :json, :xml ].include?(requested_format.to_sym)
11
+ @request_format = requested_format
12
+ end
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
20
+
21
+ 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
+
26
+ self.hostname = hostname
27
+ self.class.base_uri "https://#{self.hostname}/admin/scaffolds"
28
+
29
+ self.class.default_timeout options[:timeout] || 5
30
+
31
+ self.raise_exceptions = options[:raise_exceptions] || false
32
+
33
+ if defined?(options[:verify_ssl])
34
+ self.class.default_options.update(verify: options[:verify_ssl])
35
+ end
36
+ end
37
+
38
+ def auth
39
+ {api_key: self.api_key}
40
+ end
41
+
42
+ def parse(body)
43
+ return {success: true} if body == ""
44
+ begin
45
+ case self.request_format
46
+ when :json
47
+ result = JSON.parse(body)
48
+ when :xml
49
+ result = Hash.from_xml(body)
50
+ else
51
+ raise "Request format should be one of: :json, :xml"
52
+ end
53
+
54
+ if result.is_a?(Array)
55
+ result = result.map do |hash|
56
+ # symbolize keys
57
+ hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
58
+ end
59
+ elsif result.is_a?(Hash)
60
+ # symbolize keys
61
+ result = result.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
62
+ end
63
+
64
+ rescue JSON::ParserError => e
65
+ return body
66
+ end
67
+
68
+ end
69
+
70
+ # create a record in the given table with the attributes provided in new_record
71
+ 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)
74
+ end
75
+
76
+ # list all records from the given table
77
+ 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)
80
+ end
81
+
82
+ # return the record from the given table having the given id
83
+ 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)
86
+ end
87
+
88
+ # update a record from the given table, having the given id, with the updated attributes provided in updated_record_hash
89
+ 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)
92
+ end
93
+
94
+ # destroy a record from the given table having the given id
95
+ 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)
98
+ end
99
+
100
+ def execute(table, request)
101
+ # executes an arbitrary method on given scaffold
102
+ # The "request" hash parameters:
103
+ # record_name - The "name" attribute of the desired record, if any. Not required if calling a class method or if record_id is present.
104
+ # record_id - The id of the desired record, if any. Not required if calling a class method or if record_name is present.
105
+ # method_name - The name of the desired class or instance method to be run against the model.
106
+ # method_args - A serialized Array or Hash of the argument(s) expected by the method.
107
+ # example method call:
108
+ # 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)
111
+ end
112
+
113
+ private
114
+
115
+ # define a unified exception handler for some methods
116
+ def self.rescue_from exception, *meths, &handler
117
+ meths.each do |meth|
118
+ # store the previous implementation
119
+ old = instance_method(meth)
120
+ # wrap it
121
+ define_method(meth) do |*args|
122
+ begin
123
+ old.bind(self).call(*args)
124
+ rescue exception => e
125
+ handler.call(e, self)
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ # 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|
134
+ puts exception.message
135
+
136
+ raise exception if instance.raise_exceptions
137
+ end
138
+
139
+
140
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rxg_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Lannar Dean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ description: A simple CRUDE (Create, Read, Update, Delete, Execute) client to interface
28
+ with the RXG's API
29
+ email: ldd@rgnets.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/rxg_client.rb
35
+ homepage: https://github.com/moracca/rxg_client
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.6.11
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: RXG API Client
59
+ test_files: []