Class: Bandwidth::Client
- Inherits:
-
Object
- Object
- Bandwidth::Client
- Defined in:
- lib/bandwidth/client.rb
Overview
Catapult client class. It is used by any api related class
Constant Summary
- @@global_options =
{}
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
Class Method Summary collapse
-
.get_id_from_location_header(location) ⇒ String
Extract id from location header.
-
.global_options ⇒ Hash
Return global options.
-
.global_options=(v) ⇒ Object
Set global options.
Instance Method Summary collapse
-
#check_response(response) ⇒ Object
Check response object and raise error if status code >= 400.
-
#concat_user_path(path) ⇒ Object
Build url path like /users/<user-id>/<path>.
-
#create_connection ⇒ Faraday::Connection
Return new configured connection object.
-
#initialize(user_id = nil, api_token = nil, api_secret = nil, api_endpoint = 'https://api.catapult.inetwork.com', api_version = 'v1') ⇒ Client
constructor
Initializer.
-
#make_request(method, path, data = {}) ⇒ Array
Make HTTP request to Catapult API.
Constructor Details
#initialize(user_id = nil, api_token = nil, api_secret = nil, api_endpoint = 'https://api.catapult.inetwork.com', api_version = 'v1') ⇒ Client
Initializer
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bandwidth/client.rb', line 21 def initialize (user_id = nil, api_token = nil, api_secret = nil, api_endpoint = 'https://api.catapult.inetwork.com', api_version = 'v1') if api_token == nil && api_secret == nil if user_id == nil user_id = @@global_options end if user_id.is_a?(Hash) opts = user_id api_version = opts[:api_version] if opts[:api_version] api_endpoint = opts[:api_endpoint] if opts[:api_endpoint] api_secret = opts[:api_secret] api_token = opts[:api_token] user_id = opts[:user_id] end end raise Errors::MissingCredentialsError.new() if (user_id || '').length == 0 || (api_token || '').length == 0 || (api_secret || '').length == 0 @concat_user_path = lambda {|path| "/users/#{user_id}" + (if path[0] == "/" then path else "/#{path}" end) } @build_path = lambda {|path| "/#{api_version}" + (if path[0] == "/" then path else "/#{path}" end) } @set_adapter = lambda {|faraday| faraday.adapter(Faraday.default_adapter)} @create_connection = lambda{|| Faraday.new(api_endpoint) { |faraday| faraday.basic_auth(api_token, api_secret) faraday.headers['Accept'] = 'application/json' faraday.headers['User-Agent'] = "ruby-bandwidth/v#{Bandwidth::VERSION}" @set_adapter.call(faraday) } } @api_endpoint = api_endpoint @api_version = api_version end |
Instance Attribute Details
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint
51 52 53 |
# File 'lib/bandwidth/client.rb', line 51 def api_endpoint @api_endpoint end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version
51 52 53 |
# File 'lib/bandwidth/client.rb', line 51 def api_version @api_version end |
Class Method Details
.get_id_from_location_header(location) ⇒ String
Extract id from location header
70 71 72 73 74 |
# File 'lib/bandwidth/client.rb', line 70 def Client.get_id_from_location_header(location) items = (location || '').split('/') raise StandardError.new('Missing id in the location header') if items.size < 2 items.last end |
.global_options ⇒ Hash
Return global options
57 58 59 |
# File 'lib/bandwidth/client.rb', line 57 def Client. @@global_options end |
.global_options=(v) ⇒ Object
Set global options
63 64 65 |
# File 'lib/bandwidth/client.rb', line 63 def Client.(v) @@global_options = v end |
Instance Method Details
#check_response(response) ⇒ Object
Check response object and raise error if status code >= 400
98 99 100 101 102 103 |
# File 'lib/bandwidth/client.rb', line 98 def check_response(response) if response.status >= 400 parsed_body = JSON.parse(response.body) raise Errors::GenericError.new(parsed_body['code'], parsed_body['message']) end end |
#concat_user_path(path) ⇒ Object
Build url path like /users/<user-id>/<path>
106 107 108 |
# File 'lib/bandwidth/client.rb', line 106 def concat_user_path(path) @concat_user_path.call(path) end |
#create_connection ⇒ Faraday::Connection
Return new configured connection object
112 113 114 |
# File 'lib/bandwidth/client.rb', line 112 def create_connection() @create_connection.call() end |
#make_request(method, path, data = {}) ⇒ Array
Make HTTP request to Catapult API
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bandwidth/client.rb', line 81 def make_request(method, path, data = {}) d = camelcase(data) connection = @create_connection.call() response = if method == :get || method == :delete connection.run_request(method, @build_path.call(path), nil, nil) do |req| req.params = d unless d == nil || d.empty? end else connection.run_request(method, @build_path.call(path), d.to_json(), {'Content-Type' => 'application/json'}) end check_response(response) r = if response.body.strip().size > 0 then symbolize(JSON.parse(response.body)) else {} end [r, symbolize(response.headers || {})] end |