Class: Happi::Client
- Inherits:
-
Object
- Object
- Happi::Client
- Defined in:
- lib/happi/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(method, url, params) ⇒ Object
- #config ⇒ Object
- #connection ⇒ Object
- #default_logger ⇒ Object
- #delete(resource, params = {}) ⇒ Object
- #errors ⇒ Object
- #get(resource, params = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #logger ⇒ Object
- #param_check(params) ⇒ Object
- #patch(resource, params = {}) ⇒ Object
- #post(resource, params = {}) ⇒ Object
- #raise_error(response) ⇒ Object
- #url(resource) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 |
# File 'lib/happi/client.rb', line 19 def initialize( = {}) .each do |key, value| config.send("#{key}=", value) end end |
Class Method Details
.config ⇒ Object
11 12 13 |
# File 'lib/happi/client.rb', line 11 def self.config @global_config ||= Happi::Configuration.new end |
.configure {|config| ... } ⇒ Object
15 16 17 |
# File 'lib/happi/client.rb', line 15 def self.configure yield config end |
Instance Method Details
#call(method, url, params) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/happi/client.rb', line 49 def call(method, url, params) if config.log_level == :debug logger.debug("Happi: #{method.upcase} #{config.host}#{url}, #{params}") else logger.info("Happi: #{method.upcase} #{config.host}#{url}") end response = connection.send(method, url, params) raise_error(response) if errors[response.status] response end |
#config ⇒ Object
7 8 9 |
# File 'lib/happi/client.rb', line 7 def config @config ||= self.class.config.dup end |
#connection ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/happi/client.rb', line 95 def connection @connection ||= Faraday.new(config.host) do |f| f.use FaradayMiddleware::OAuth2, config.oauth_token f.use FaradayMiddleware::ParseJson, content_type: 'application/json' if self.config.use_json f.use FaradayMiddleware::EncodeJson f.request :json f.response :json else f.request :multipart f.request :url_encoded end f.adapter :net_http end end |
#default_logger ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/happi/client.rb', line 75 def default_logger if defined?(Rails) Rails.try(:logger) || Logger.new(STDOUT) else Logger.new(STDOUT) end end |
#delete(resource, params = {}) ⇒ Object
30 31 32 33 |
# File 'lib/happi/client.rb', line 30 def delete(resource, params = {}) call(:delete, url(resource), param_check(params)) .body.with_indifferent_access end |
#errors ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/happi/client.rb', line 113 def errors @errors ||= { 400 => Happi::Error::BadRequest, 401 => Happi::Error::Unauthorized, 403 => Happi::Error::Forbidden, 404 => Happi::Error::NotFound, 406 => Happi::Error::NotAcceptable, 408 => Happi::Error::RequestTimeout, 422 => Happi::Error::UnprocessableEntity, 429 => Happi::Error::TooManyRequests, 500 => Happi::Error::InternalServerError, 502 => Happi::Error::BadGateway, 503 => Happi::Error::ServiceUnavailable, 504 => Happi::Error::GatewayTimeout, } end |
#get(resource, params = {}) ⇒ Object
25 26 27 28 |
# File 'lib/happi/client.rb', line 25 def get(resource, params = {}) call(:get, url(resource), param_check(params)) .body.with_indifferent_access end |
#logger ⇒ Object
71 72 73 |
# File 'lib/happi/client.rb', line 71 def logger @logger ||= default_logger end |
#param_check(params) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/happi/client.rb', line 83 def param_check(params) Hash[params.map do |key, value| if value.is_a? Hash [key, param_check(value)] elsif value.respond_to?(:multipart) [key, value.multipart] else [key, value] end end] end |
#patch(resource, params = {}) ⇒ Object
35 36 37 38 |
# File 'lib/happi/client.rb', line 35 def patch(resource, params = {}) call(:patch, url(resource), param_check(params)) .body.with_indifferent_access end |
#post(resource, params = {}) ⇒ Object
40 41 42 43 |
# File 'lib/happi/client.rb', line 40 def post(resource, params = {}) call(:post, url(resource), param_check(params)) .body.with_indifferent_access end |
#raise_error(response) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/happi/client.rb', line 61 def raise_error(response) if response.body['errors'] = response.body['errors'] else = response.body end fail errors[response.status].new(, response) end |
#url(resource) ⇒ Object
45 46 47 |
# File 'lib/happi/client.rb', line 45 def url(resource) "/api/#{config.version}/#{resource}" end |