Class: Onering::API
- Inherits:
-
Object
- Object
- Onering::API
- Includes:
- HTTParty, Util
- Defined in:
- lib/onering/api.rb,
lib/onering/plugins/devices.rb,
lib/onering/plugins/automation.rb,
lib/onering/plugins/authentication.rb
Direct Known Subclasses
Auth, AutomationJobs, AutomationRequests, AutomationTasks, Devices
Defined Under Namespace
Modules: Actions, Errors Classes: Auth, AutomationJobs, AutomationRequests, AutomationTasks, Devices
Constant Summary collapse
- DEFAULT_CONFIG =
{}
- DEFAULT_BASE =
"https://onering"- DEFAULT_PATH =
"/api"- DEFAULT_OPTIONS_FILE =
["~/.onering/cli.yml", "/etc/onering/cli.yml"]
- DEFAULT_CLIENT_PEM =
["~/.onering/client.pem", "/etc/onering/client.pem"]
- DEFAULT_CLIENT_KEY =
["~/.onering/client.key", "/etc/onering/client.key"]
- DEFAULT_VALIDATION_PEM =
"/etc/onering/validation.pem"
Constants included from Util
Instance Attribute Summary collapse
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #connect(options = {}) ⇒ Object
- #delete(endpoint, options = {}) ⇒ Object
- #get(endpoint, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
A new instance of API.
-
#method_missing(method, *args, &block) ⇒ Object
I’m not a huge fan of what’s happening here, but metaprogramming is hard…
- #opt(name, default = nil) ⇒ Object
- #post(endpoint, options = {}, &block) ⇒ Object
- #put(endpoint, options = {}) ⇒ Object
- #request(method, endpoint, options = {}) ⇒ Object
- #status ⇒ Object
Methods included from Util
#fact, #gem_path, #http_status, #make_filter
Constructor Details
#initialize(options = {}) ⇒ API
Returns a new instance of API.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/onering/api.rb', line 37 def initialize(={}) @_config = {} @_plugins = {} @_connection_options = # load and merge all config file sources _load_config(@_connection_options[:configfile], @_connection_options.get(:config, {})) # set API connectivity details Onering::API.base_uri @_config.get(:url, DEFAULT_BASE) Onering::Reporter.setup() connect() if .get(:autoconnect, true) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
I’m not a huge fan of what’s happening here, but metaprogramming is hard…
“Don’t let the perfect be the enemy of the good.”
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/onering/api.rb', line 119 def method_missing(method, *args, &block) modname = method.to_s.split('_').map(&:capitalize).join if not (plugin = (Onering::API.const_get(modname) rescue nil)).nil? @_plugins[method] ||= plugin.new.connect(@_connection_options) return @_plugins[method] else super end end |
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
25 26 27 |
# File 'lib/onering/api.rb', line 25 def url @url end |
Instance Method Details
#connect(options = {}) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/onering/api.rb', line 52 def connect(={}) # setup authentication _setup_auth() return self end |
#delete(endpoint, options = {}) ⇒ Object
110 111 112 |
# File 'lib/onering/api.rb', line 110 def delete(endpoint, ={}) request(:delete, endpoint, ) end |
#get(endpoint, options = {}) ⇒ Object
86 87 88 |
# File 'lib/onering/api.rb', line 86 def get(endpoint, ={}) request(:get, endpoint, ) end |
#opt(name, default = nil) ⇒ Object
130 131 132 |
# File 'lib/onering/api.rb', line 130 def opt(name, default=nil) @_config.get(name, default) end |
#post(endpoint, options = {}, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/onering/api.rb', line 90 def post(endpoint, ={}, &block) if block_given? request(:post, endpoint, .merge({ :body => yield })) else request(:post, endpoint, ) end end |
#put(endpoint, options = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/onering/api.rb', line 100 def put(endpoint, ={}) if block_given? request(:put, endpoint, .merge({ :body => yield })) else request(:put, endpoint, ) end end |
#request(method, endpoint, options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/onering/api.rb', line 60 def request(method, endpoint, ={}) endpoint = [@_config.get(:path, DEFAULT_PATH).strip, endpoint.sub(/^\//,'')].join('/') case method when :post rv = Onering::API.post(endpoint, ) when :put rv = Onering::API.put(endpoint, ) when :delete rv = Onering::API.delete(endpoint, ) when :head rv = Onering::API.head(endpoint, ) else rv = Onering::API.get(endpoint, ) end if rv.code >= 500 raise Errors::ServerError.new("HTTP #{rv.code} - #{Onering::Util.http_status(rv.code)} #{rv.parsed_response.get('error.message','') rescue ''}") elsif rv.code >= 400 raise Errors::ClientError.new("HTTP #{rv.code} - #{Onering::Util.http_status(rv.code)} #{rv.parsed_response.get('error.message', '') rescue ''}") else rv end end |