Class: BoxtalLite::Client
- Inherits:
-
Object
- Object
- BoxtalLite::Client
- Defined in:
- lib/boxtal_lite/client.rb
Instance Method Summary collapse
- #build_connection ⇒ Object
- #build_v1_connection ⇒ Object
- #config ⇒ Object
- #connection(api_key) ⇒ Object
- #delete(path, options = {}) ⇒ Object
- #extract_error_message(response_body) ⇒ Object
- #extract_v1_error_message(response_body) ⇒ Object
- #get(path, params = nil, options = {}) ⇒ Object
- #post(path, payload, options = {}) ⇒ Object
- #v1_connection(api_key, creds) ⇒ Object
- #v1_get(path, params = nil, options = {}) ⇒ Object
- #with_error_handling ⇒ Object
- #with_v1_error_handling ⇒ Object
Instance Method Details
#build_connection ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/boxtal_lite/client.rb', line 76 def build_connection Faraday.new(url: BoxtalLite.url) do |builder| builder.[:timeout] = config.timeout builder.[:open_timeout] = config.timeout builder.request :json builder.headers['Accept'] = 'application/json' builder.response :json, content_type: /\bjson$/ builder.response :raise_error builder.adapter Faraday.default_adapter end end |
#build_v1_connection ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/boxtal_lite/client.rb', line 91 def build_v1_connection Faraday.new(url: BoxtalLite.v1_url) do |builder| builder.[:timeout] = config.timeout builder.[:open_timeout] = config.timeout #builder.request :xml builder.headers['Accept'] = 'application/xml' builder.response :xml, content_type: /\bxml$/ builder.response :raise_error builder.adapter Faraday.default_adapter end end |
#config ⇒ Object
117 118 119 |
# File 'lib/boxtal_lite/client.rb', line 117 def config BoxtalLite.config end |
#connection(api_key) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/boxtal_lite/client.rb', line 56 def connection(api_key) token = api_key || BoxtalLite.config.api_key raise(Error, 'v3 API key is not set') unless token @connection = build_connection @connection.headers['Authorization'] = "Basic #{token}" @connection.headers['Content-Type'] = 'application/json' @connection end |
#delete(path, options = {}) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/boxtal_lite/client.rb', line 30 def delete(path, = {}) with_error_handling do response = connection([:creds]).delete(path) response.body end end |
#extract_error_message(response_body) ⇒ Object
105 106 107 108 109 |
# File 'lib/boxtal_lite/client.rb', line 105 def (response_body) result = JSON.parse(response_body) result['message'] || result['messages'] rescue JSON::ParserError end |
#extract_v1_error_message(response_body) ⇒ Object
111 112 113 114 115 |
# File 'lib/boxtal_lite/client.rb', line 111 def (response_body) result = Nokogiri::XML(response_body) result.xpath('//error/message').text || result.xpath('//error/messages').text rescue Nokogiri::XML::SyntaxError end |
#get(path, params = nil, options = {}) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/boxtal_lite/client.rb', line 7 def get(path, params = nil, = {}) with_error_handling do response = connection([:api_key]).get(path, params) response.body end end |
#post(path, payload, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/boxtal_lite/client.rb', line 21 def post(path, payload, = {}) begin response = connection([:creds]).post(path, payload) rescue => e raise e.inspect end response.body end |
#v1_connection(api_key, creds) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/boxtal_lite/client.rb', line 65 def v1_connection(api_key, creds) token = api_key || BoxtalLite.config.v1_api_key creds = creds || BoxtalLite.config.v1_creds raise(Error, 'v1 API key is not set') unless token raise(Error, 'v1 Creds are not set') unless creds @connection = build_v1_connection @connection.headers['Access_key'] = token @connection.headers['Authorization'] = "Basic #{creds}" @connection end |
#v1_get(path, params = nil, options = {}) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/boxtal_lite/client.rb', line 14 def v1_get(path, params = nil, = {}) with_v1_error_handling do response = v1_connection([:api_key], [:creds]).get(path, params) response.body end end |
#with_error_handling ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/boxtal_lite/client.rb', line 38 def with_error_handling yield rescue Faraday::ClientError => e = (e.response[:body]) if e.response ||= e. raise(Error, ) end |
#with_v1_error_handling ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/boxtal_lite/client.rb', line 47 def with_v1_error_handling yield rescue Faraday::ClientError => e = (e.response[:body]) if e.response ||= e. raise(Error, ) end |