Class: FleetAPI::Client::Real
- Inherits:
-
Object
- Object
- FleetAPI::Client::Real
- Defined in:
- lib/fleet_api/client.rb,
lib/fleet_api/requests/get_unit.rb,
lib/fleet_api/requests/get_units.rb,
lib/fleet_api/requests/create_unit.rb,
lib/fleet_api/requests/update_unit.rb,
lib/fleet_api/requests/destroy_unit.rb,
lib/fleet_api/requests/get_machines.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #create_unit(params = {}) ⇒ Object
- #destroy_unit(params = {}) ⇒ Object
- #get_machines(params = {}) ⇒ Object
- #get_unit(params = {}) ⇒ Object
- #get_units(params = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Real
constructor
A new instance of Real.
- #request(options = {}) ⇒ Object
- #update_unit(params = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Real
Returns a new instance of Real.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fleet_api/client.rb', line 22 def initialize(={}) @url = URI.parse([:url]) @logger = Logger.new(nil) adapter = Faraday.default_adapter @connection = Faraday.new(:url => url) do |faraday| faraday.response :logger faraday.response :json faraday.request :multipart faraday.request :json faraday.adapter(*adapter) end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
20 21 22 |
# File 'lib/fleet_api/client.rb', line 20 def connection @connection end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
20 21 22 |
# File 'lib/fleet_api/client.rb', line 20 def path @path end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
20 21 22 |
# File 'lib/fleet_api/client.rb', line 20 def url @url end |
Instance Method Details
#create_unit(params = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/fleet_api/requests/create_unit.rb', line 3 def create_unit(params={}) name = params["name"] request( :body => params, :method => 'PUT', :path => "v1-alpha/units/#{name}", ) end |
#destroy_unit(params = {}) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/fleet_api/requests/destroy_unit.rb', line 3 def destroy_unit(params={}) name = params["name"] request( :path => "v1-alpha/units/#{name}", :method => 'DELETE', ) end |
#get_machines(params = {}) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/fleet_api/requests/get_machines.rb', line 3 def get_machines(params={}) request( :query => query, :method => 'GET', :path => "v1-alpha/machines", ) end |
#get_unit(params = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/fleet_api/requests/get_unit.rb', line 3 def get_unit(params={}) name = params["name"] url = params["url"] request( :path => "v1-alpha/units/#{name}", :url => url, ) end |
#get_units(params = {}) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/fleet_api/requests/get_units.rb', line 3 def get_units(params={}) request( :method => 'GET', :path => "v1-alpha/units", ) end |
#request(options = {}) ⇒ Object
36 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 |
# File 'lib/fleet_api/client.rb', line 36 def request(={}) method = ([:method] || "get").to_s.downcase.to_sym url = Addressable::URI.parse([:url] || File.join(@url.to_s, [:path] || "/")) url.query_values = (url.query_values || {}).merge([:query] || {}) params = [:params] || {} body = [:body] headers = [:headers] || {"Accept" => "application/json"} headers.merge!("Content-Type" => "application/x-www-form-urlencoded") if !body && !params.empty? response = @connection.send(method) do |req| req.url(url.to_s) req.headers.merge!(headers) req.params.merge!(params) req.body = body end FleetAPI::Response.new( :status => response.status, :headers => response.headers, :body => response.body, :request => { :method => method, :url => url, :body => body, :headers => headers, } ).raise! end |
#update_unit(params = {}) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/fleet_api/requests/update_unit.rb', line 3 def update_unit(params={}) name = params["name"] request( :body => params, :method => 'PUT', :path => "v1-alpha/units/#{name}", ) end |