Class: Bimbly
- Inherits:
-
Object
- Object
- Bimbly
- Defined in:
- lib/bimbly.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#cert ⇒ Object
Returns the value of attribute cert.
-
#data_type ⇒ Object
readonly
Returns the value of attribute data_type.
-
#doc_pointer ⇒ Object
Returns the value of attribute doc_pointer.
-
#error_codes ⇒ Object
readonly
Returns the value of attribute error_codes.
-
#error_names ⇒ Object
readonly
Returns the value of attribute error_names.
-
#file ⇒ Object
Returns the value of attribute file.
-
#file_select ⇒ Object
Returns the value of attribute file_select.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#menu ⇒ Object
Returns the value of attribute menu.
-
#meth_name ⇒ Object
Returns the value of attribute meth_name.
-
#obj_sets ⇒ Object
readonly
Returns the value of attribute obj_sets.
-
#param_pointer ⇒ Object
Returns the value of attribute param_pointer.
-
#password ⇒ Object
Returns the value of attribute password.
-
#pointer ⇒ Object
Returns the value of attribute pointer.
-
#port ⇒ Object
Returns the value of attribute port.
-
#req_pointer ⇒ Object
Returns the value of attribute req_pointer.
-
#user ⇒ Object
Returns the value of attribute user.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
- #available_methods ⇒ Object
- #build_params(hash) ⇒ Object
- #call(opts = {}) ⇒ Object
- #data_types(type = nil) ⇒ Object
- #details ⇒ Object
- #doc ⇒ Object
- #error_format(messages) ⇒ Object
- #gen_method_hash ⇒ Object
- #gen_methods ⇒ Object
- #gen_uri(opts = {}) ⇒ Object
-
#initialize(opts = {}) ⇒ Bimbly
constructor
A new instance of Bimbly.
- #new_connection(opts = {}) ⇒ Object
- #parameters ⇒ Object
- #request ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Bimbly
Returns a new instance of Bimbly.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/bimbly.rb', line 12 def initialize(opts = {}) # Read in setup files if opts[:version] == "3" opts.delete(:version) @error_codes = YAML.load(File.read("#{File.dirname(__FILE__)}/error_codes_v3.yml")) @obj_sets = YAML.load(File.read("#{File.dirname(__FILE__)}/object_sets_v3.yml")) @data_type = YAML.load(File.read("#{File.dirname(__FILE__)}/data_types_v3.yml")) else @error_codes = YAML.load(File.read("#{File.dirname(__FILE__)}/error_codes_v4.yml")) @obj_sets = YAML.load(File.read("#{File.dirname(__FILE__)}/object_sets_v4.yml")) @data_type = YAML.load(File.read("#{File.dirname(__FILE__)}/data_types_v4.yml")) end @meth_name = nil @base_url = "NotConnected" = [] @param_pointer = @obj_sets @doc_pointer = @obj_sets @req_pointer = @obj_sets gen_methods new_connection(opts) end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def array @array end |
#base_url ⇒ Object
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def base_url @base_url end |
#cert ⇒ Object
Returns the value of attribute cert.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def cert @cert end |
#data_type ⇒ Object (readonly)
Returns the value of attribute data_type.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def data_type @data_type end |
#doc_pointer ⇒ Object
Returns the value of attribute doc_pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def doc_pointer @doc_pointer end |
#error_codes ⇒ Object (readonly)
Returns the value of attribute error_codes.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def error_codes @error_codes end |
#error_names ⇒ Object (readonly)
Returns the value of attribute error_names.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def error_names @error_names end |
#file ⇒ Object
Returns the value of attribute file.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def file @file end |
#file_select ⇒ Object
Returns the value of attribute file_select.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def file_select @file_select end |
#headers ⇒ Object
Returns the value of attribute headers.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def headers @headers end |
#menu ⇒ Object
Returns the value of attribute menu.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def end |
#meth_name ⇒ Object
Returns the value of attribute meth_name.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def meth_name @meth_name end |
#obj_sets ⇒ Object (readonly)
Returns the value of attribute obj_sets.
8 9 10 |
# File 'lib/bimbly.rb', line 8 def obj_sets @obj_sets end |
#param_pointer ⇒ Object
Returns the value of attribute param_pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def param_pointer @param_pointer end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def password @password end |
#pointer ⇒ Object
Returns the value of attribute pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def pointer @pointer end |
#port ⇒ Object
Returns the value of attribute port.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def port @port end |
#req_pointer ⇒ Object
Returns the value of attribute req_pointer.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def req_pointer @req_pointer end |
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def user @user end |
#verb ⇒ Object
Returns the value of attribute verb.
9 10 11 |
# File 'lib/bimbly.rb', line 9 def verb @verb end |
Instance Method Details
#available_methods ⇒ Object
184 185 186 |
# File 'lib/bimbly.rb', line 184 def available_methods self.methods - Object.methods end |
#build_params(hash) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/bimbly.rb', line 206 def build_params(hash) raise ArgumentError, "Please provide a valid hash for parameters" unless hash.instance_of? Hash and hash != {} url_params = "?" size_count = 0 hash.each { |key, value| url_params = "#{url_params}#{key}=#{value}" size_count += 1 url_params = "#{url_params}&" unless size_count == hash.size } url_params end |
#call(opts = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bimbly.rb', line 37 def call(opts = {}) verb = @verb payload = @payload uri = @uri reset raise StandardError, "Instantiate a connection to an array" if @array.nil? raise StandardError, "Method to be used has not been loaded" if verb.nil? # Check if url is valid raise ArgumentError, "Invalid URL: #{uri}" unless uri =~ /\A#{URI::regexp}\z/ # Do some payload stuff to wrap with 'data' and make sure it's usable if not payload.nil? # payload = payload["data"] if payload.keys.size == 1 and payload.keys.include?("data") payload = { data: payload } end payload = payload.to_json if payload.class == Hash begin response = RestClient::Request.execute( method: verb.to_sym, url: uri, ssl_ca_file: @cert, headers: @headers, payload: payload ) rescue RestClient::ExceptionWithResponse => e puts "Response Code: #{e.response.code}" puts "Response Headers:" pp e.response.headers puts "Response Body:" pp error_format(e.response.body) puts "Response Object:" puts e.response.request.inspect end begin JSON.parse(response.body) unless response.nil? || response.body == '' rescue JSON::ParserError => e puts e end end |
#data_types(type = nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/bimbly.rb', line 188 def data_types(type = nil) if type return @data_type[type] elsif @param_pointer.nil? return @data_type else @param_pointer.each { |key,value| puts "[#{key}]" @data_type[value].each { |key, value| puts "#{key}: #{value}" } puts "" } end return nil end |
#details ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/bimbly.rb', line 159 def details puts "Method Selected: #{@meth_name}" puts "URI: #{@uri}" puts "Verb: #{@verb}" if not @payload.nil? puts "Payload:" pp @payload else puts "Payload: n/a" end return end |
#doc ⇒ Object
172 173 174 |
# File 'lib/bimbly.rb', line 172 def doc puts @doc_pointer.to_yaml end |
#error_format(messages) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/bimbly.rb', line 84 def error_format() = [] JSON.parse()["messages"].each { || .merge!(@error_names[["code"]]) .delete("Error_Desc") << } end |
#gen_method_hash ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/bimbly.rb', line 224 def gen_method_hash method_hash = {} name = "" @obj_sets.each { |obj_key, obj_value| obj_value.each { |op_key, op_value| method_suffix = "" op_value.each { |key, value| next if not key.match(/DELETE|GET|POST|PUT/) if key.match(/id/) method_suffix = "_by_id" elsif key.match(/detail/) method_suffix = "_detailed" end verb, url_suffix = key.split(' ') hash = {} hash[:verb] = verb.downcase.to_sym hash[:url_suffix] = url_suffix hash[:avail_params] = value hash[:request] = @obj_sets[obj_key][op_key]["Request"] hash[:object] = obj_key hash[:op] = op_key name = "#{op_key}_#{obj_key}#{method_suffix}" method_hash[name.to_sym] = hash .push name } } } method_hash end |
#gen_methods ⇒ Object
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/bimbly.rb', line 255 def gen_methods method_hash = gen_method_hash method_hash.each { |method_name, hash| define_singleton_method(method_name) { |opts = {}| @param_pointer = hash[:avail_params] @doc_pointer = @obj_sets[hash[:object]][hash[:op]] @req_pointer = @obj_sets[hash[:object]][hash[:op]]["Request"] @meth_name = method_name raise ArgumentError, "Please provide id" if method_name.match(/id/) and opts[:id].nil? url_suffix = hash[:url_suffix] url_suffix = url_suffix.gsub(/\/id/, "/#{opts[:id]}") if method_name.match(/id/) uri = gen_uri(url_suffix: url_suffix, params: opts[:params]) payload = opts[:payload] # Unwrap the payload data if it is json or wrapped in 'data' so we can inspect the attrs if not payload.nil? payload = JSON.parse(payload) if payload.class == String payload = payload["data"] if payload.keys.include?("data") end @uri = uri @verb = hash[:verb] @payload = payload if not opts[:params].nil? opts[:params].each { |key, value| raise ArgumentError, "Invalid parameter for #{method_name}: #{key}" unless hash[:avail_params].include?(key) } end # Maybe pull this out into its own method # Check for mandatory payload info mando_array = [] if not hash[:request].nil? hash[:request].each { |key, value| mando_array << key } end raise ArgumentError, "Must supply :payload with attributes #{mando_array} on #{method_name}" if not mando_array.empty? and payload.nil? mando_array.each { |ele| raise ArgumentError, "\'#{ele}\' is a mandatory attribute in the payload for #{method_name}: Please supply these attributes #{mando_array}" unless payload.keys.to_s.include?(ele) } if not payload.nil? payload.keys.each { |key| raise ArgumentError, "The attribute \'#{key}\' is not an available attribute for #{method_name}" unless hash[:request].keys.include?(key.to_s) } end self } } end |
#gen_uri(opts = {}) ⇒ Object
219 220 221 222 |
# File 'lib/bimbly.rb', line 219 def gen_uri(opts = {}) url_params = build_params(opts[:params]) if opts[:params] uri = "#{@base_url}/#{opts[:url_suffix]}#{url_params}" end |
#new_connection(opts = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/bimbly.rb', line 94 def new_connection(opts = {}) @file = opts[:file] @file_select = opts[:file_select] @array = opts[:array] @cert = opts[:cert] @port = opts[:port] @user = opts[:user] @password = opts[:password] return if opts.empty? if @file conn_data = YAML.load(File.read(File.(@file))) conn_data = conn_data[@file_select] if @file_select @array = conn_data["array"] @cert = conn_data["cert"] @user = conn_data["user"] @password = conn_data["password"] end @port = "5392" if @port.nil? raise ArgumentError, "You must provide an array" if @array.nil? raise ArgumentError, "You must provide a CA cert" if @cert.nil? raise ArgumentError, "You must provide a user" if @user.nil? raise ArgumentError, "You must provide a password" if @password.nil? @base_url = "https://#{array}:#{port}" @uri = "#{@base_url}/v1/tokens" # Get initial connection credentials creds = { data: { username: @user, password: @password } } begin response = RestClient::Request.execute( method: :post, url: @uri, payload: creds.to_json, ssl_ca_file: @cert, ssl_ciphers: 'AESGCM:!aNULL' ) rescue RestClient::ExceptionWithResponse => e puts "Response Code: #{e.response.code}" puts "Response Headers: #{e.response.headers}" puts "Response Body: #{e.response.body}" puts "Response Object: #{e.response.request.inspect}" end token = JSON.parse(response)["data"]["session_token"] @headers = { 'X-Auth-Token' => token } end |
#parameters ⇒ Object
176 177 178 |
# File 'lib/bimbly.rb', line 176 def parameters @param_pointer end |
#request ⇒ Object
180 181 182 |
# File 'lib/bimbly.rb', line 180 def request puts @req_pointer.to_yaml end |
#reset ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/bimbly.rb', line 150 def reset @verb = nil @payload = nil @uri = nil @meth_name = nil @doc_pointer = @obj_sets @param_pointer = @obj_sets end |