Class: Onering::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Util
Defined in:
lib/onering/api.rb,
lib/onering/plugins/devices.rb,
lib/onering/plugins/automation.rb,
lib/onering/plugins/authentication.rb

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

Util::HTTP_STATUS_CODES

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  @_config = {}
  @_plugins = {}
  @_connection_options = 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(options) if options.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

#urlObject

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(options={})
# setup authentication
  _setup_auth()

  return self
end

#delete(endpoint, options = {}) ⇒ Object



110
111
112
# File 'lib/onering/api.rb', line 110

def delete(endpoint, options={})
  request(:delete, endpoint, options)
end

#get(endpoint, options = {}) ⇒ Object



86
87
88
# File 'lib/onering/api.rb', line 86

def get(endpoint, options={})
  request(:get, endpoint, options)
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, options={}, &block)
  if block_given?
    request(:post, endpoint, options.merge({
      :body => yield
    }))
  else
    request(:post, endpoint, options)
  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, options={})
  if block_given?
    request(:put, endpoint, options.merge({
      :body => yield
    }))
  else
    request(:put, endpoint, options)
  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, options={})
  endpoint = [@_config.get(:path, DEFAULT_PATH).strip, endpoint.sub(/^\//,'')].join('/')

  case method
  when :post
    rv = Onering::API.post(endpoint, options)
  when :put
    rv = Onering::API.put(endpoint, options)
  when :delete
    rv = Onering::API.delete(endpoint, options)
  when :head
    rv = Onering::API.head(endpoint, options)
  else
    rv = Onering::API.get(endpoint, options)
  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

#statusObject



135
136
137
# File 'lib/onering/api.rb', line 135

def status()
  Onering::API.get("/").parsed_response
end