Class: Triglav::Client
- Inherits:
-
Object
- Object
- Triglav::Client
- Defined in:
- lib/triglav/client.rb,
lib/triglav/client/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- API_ENDPOINT_MAP =
{ services: { method: :get, path: '/api/services' }, roles: { method: :get, path: '/api/roles' }, roles_in: { method: :get, path: ['/api/services/%s/roles'] }, hosts: { method: :get, path: '/api/hosts' }, hosts_in: { method: :get, path: [ '/api/services/%s/hosts', '/api/services/%s/roles/%s/hosts', ] }, }
- VERSION =
"0.0.7"
Instance Attribute Summary collapse
-
#api_token ⇒ Object
Returns the value of attribute api_token.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
Instance Method Summary collapse
- #create(model, params) ⇒ Object
- #dispatch_request(method, path, params = {}) ⇒ Object
- #endpoint_for(type, *args) ⇒ Object
- #hosts(options = {}) ⇒ Object
- #hosts_in(service, role = nil, options = {}) ⇒ Object
-
#initialize(args) ⇒ Client
constructor
A new instance of Client.
- #roles ⇒ Object
- #roles_in(service) ⇒ Object
- #services ⇒ Object
Constructor Details
#initialize(args) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 |
# File 'lib/triglav/client.rb', line 13 def initialize(args) @base_url = args[:base_url] @api_token = args[:api_token] if !@base_url || !@api_token raise ArgumentError.new("Both `base_url` and `api_token` are required.") end end |
Instance Attribute Details
#api_token ⇒ Object
Returns the value of attribute api_token.
11 12 13 |
# File 'lib/triglav/client.rb', line 11 def api_token @api_token end |
#base_url ⇒ Object
Returns the value of attribute base_url.
11 12 13 |
# File 'lib/triglav/client.rb', line 11 def base_url @base_url end |
Instance Method Details
#create(model, params) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/triglav/client.rb', line 55 def create (model, params) case model when :service; Triglav::Model::Service.create(self, params) when :role; Triglav::Model::Role.create(self, params) when :host; Triglav::Model::Host.create(self, params) else raise ArgumentError.new("No such model for #{model}") end end |
#dispatch_request(method, path, params = {}) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/triglav/client.rb', line 128 def dispatch_request(method, path, params = {}) if !method || !path raise ArgumentError.new("Both `method` and `path` are required.") end response = do_request(method, path, params) handle_response(response) end |
#endpoint_for(type, *args) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/triglav/client.rb', line 36 def endpoint_for (type, *args) endpoint = API_ENDPOINT_MAP[type] unless endpoint raise ArgumentError.new("No endpoint found for #{type}") end if args.empty? endpoint else path = endpoint[:path][args.size - 1] { method: endpoint[:method], path: path % args, } end end |
#hosts(options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/triglav/client.rb', line 88 def hosts ( = {}) endpoint = endpoint_for(:hosts) response = dispatch_request(endpoint[:method], endpoint[:path]) response.map do |info| Triglav::Model::Host.new(client: self, info: info) end.select do |h| if [:with_inactive] true else h.info.active end end end |
#hosts_in(service, role = nil, options = {}) ⇒ Object
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 |
# File 'lib/triglav/client.rb', line 102 def hosts_in (service, role = nil, = {}) if role.is_a?(Hash) raise ArgumentError.new( "`role` must be passed (even if it's nil) when you want to pass `options`." ) end if (role) endpoint = endpoint_for(:hosts_in, service, role) response = dispatch_request(endpoint[:method], endpoint[:path]) else endpoint = endpoint_for(:hosts_in, service) response = dispatch_request(endpoint[:method], endpoint[:path]) end response.map do |info| Triglav::Model::Host.new(client: self, info: info) end.select do |h| if [:with_inactive] true else h.info.active end end end |
#roles ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/triglav/client.rb', line 72 def roles endpoint = endpoint_for(:roles) response = dispatch_request(endpoint[:method], endpoint[:path]) response.map do |info| Triglav::Model::Role.new(client: self, info: info) end end |
#roles_in(service) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/triglav/client.rb', line 80 def roles_in (service) endpoint = endpoint_for(:roles_in, service) response = dispatch_request(endpoint[:method], endpoint[:path]) response.map do |info| Triglav::Model::Role.new(client: self, info: info) end end |