Class: BandwidthIris::Client
- Inherits:
-
Object
- Object
- BandwidthIris::Client
- Defined in:
- lib/bandwidth-iris/client.rb
Constant Summary collapse
- @@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.
Instance Method Summary collapse
- #build_xml(data) ⇒ Object
-
#check_response(response) ⇒ Object
Check response object and raise error if status code >= 400.
-
#concat_account_path(path) ⇒ Object
Build url path like /accounts/
/ . -
#create_connection ⇒ Faraday::Connection
Return new configured connection object.
-
#initialize(account_id = nil, user_name = nil, password = nil, options = nil) ⇒ Client
constructor
A new instance of Client.
-
#make_request(method, path, data = {}) ⇒ Array
Make HTTP request to IRIS API.
-
#make_request_file_download(method, path, data = {}) ⇒ string
Makes an HTTP request for a file download.
-
#make_request_file_upload(method, path, data, content_type) ⇒ Array
Makes an HTTP request for file uploads.
Constructor Details
#initialize(account_id = nil, user_name = nil, password = nil, options = nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bandwidth-iris/client.rb', line 12 def initialize (account_id = nil, user_name = nil, password = nil, = nil) if user_name == nil && password == nil && == nil if account_id && account_id.is_a?(Hash) = account_id account_id = nil end end = || @@global_options account_id = [:account_id] unless account_id user_name = [:user_name] || [:username] unless user_name password = [:password] unless password [:api_endpoint] = @@global_options[:api_endpoint] unless [:api_endpoint] [:api_version] = @@global_options[:api_version] unless [:api_version] api_endpoint = [:api_endpoint] || "https://api.inetwork.com" api_version = [:api_version] || "v1.0" @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(user_name, password) #faraday.response :logger faraday.headers['Accept'] = 'application/xml' faraday.use FaradayMiddleware::FollowRedirects @set_adapter.call(faraday) } } @concat_account_path = lambda {|path| "/accounts/#{account_id}" + (if path then (if path[0] == "/" then path else "/#{path}" end) else '' end) } @api_endpoint = api_endpoint @api_version = api_version end |
Instance Attribute Details
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
44 45 46 |
# File 'lib/bandwidth-iris/client.rb', line 44 def api_endpoint @api_endpoint end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
44 45 46 |
# File 'lib/bandwidth-iris/client.rb', line 44 def api_version @api_version end |
Instance Method Details
#build_xml(data) ⇒ Object
155 156 157 158 |
# File 'lib/bandwidth-iris/client.rb', line 155 def build_xml(data) doc = build_doc(data, data.keys.first.to_s().camelcase(:upper)) doc.values.first.to_xml({:root => doc.keys.first, :skip_types => true, :indent => 0 }) end |
#check_response(response) ⇒ Object
Check response object and raise error if status code >= 400
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/bandwidth-iris/client.rb', line 119 def check_response(response) parsed_body = parse_xml(response.body || '') code = find_first_descendant(parsed_body, :error_code) description = find_first_descendant(parsed_body, :description) unless code error = find_first_descendant(parsed_body, :error) if error code = error[:code] description = error[:description] else errors = find_first_descendant(parsed_body, :errors) if errors == nil || errors.length == 0 code = find_first_descendant(parsed_body, :result_code) description = find_first_descendant(parsed_body, :result_message) else errors = [errors] if errors.is_a?(Hash) raise Errors::AgregateError.new(errors.map {|e| Errors::GenericError.new(e[:code], e[:description], response.status)}) end end end raise Errors::GenericError.new(code, description, response.status) if code && description && code != '0' && code != 0 raise Errors::GenericError.new('', "Http code #{response.status}", response.status) if response.status >= 400 parsed_body end |
#concat_account_path(path) ⇒ Object
Build url path like /accounts/
145 146 147 |
# File 'lib/bandwidth-iris/client.rb', line 145 def concat_account_path(path) @concat_account_path.call(path) end |
#create_connection ⇒ Faraday::Connection
Return new configured connection object
151 152 153 |
# File 'lib/bandwidth-iris/client.rb', line 151 def create_connection() @create_connection.call() end |
#make_request(method, path, data = {}) ⇒ Array
Make HTTP request to IRIS API
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bandwidth-iris/client.rb', line 72 def make_request(method, path, data = {}) connection = @create_connection.call() response = if method == :get || method == :delete d = camelcase(data) 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), build_xml(data), {'Content-Type' => 'application/xml'}) end body = check_response(response) [body || {}, symbolize(response.headers || {})] end |
#make_request_file_download(method, path, data = {}) ⇒ string
Makes an HTTP request for a file download
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/bandwidth-iris/client.rb', line 104 def make_request_file_download(method, path, data = {}) connection = @create_connection.call() response = if method == :get || method == :delete d = camelcase(data) 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), build_xml(data), {'Content-Type' => 'application/xml'}) end return response.body end |
#make_request_file_upload(method, path, data, content_type) ⇒ Array
Makes an HTTP request for file uploads
92 93 94 95 96 97 |
# File 'lib/bandwidth-iris/client.rb', line 92 def make_request_file_upload(method, path, data, content_type) connection = @create_connection.call() response = connection.run_request(method, @build_path.call(path), data, {'Content-Type' => content_type}) body = check_response(response) [body || {}, symbolize(response.headers || {})] end |