Class: BoxtalLite::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/boxtal_lite/client.rb

Instance Method Summary collapse

Instance Method Details

#build_connectionObject



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.options[:timeout] = config.timeout
    builder.options[: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_connectionObject



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.options[:timeout] = config.timeout
    builder.options[: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

#configObject



117
118
119
# File 'lib/boxtal_lite/client.rb', line 117

def config
  BoxtalLite.config
end

#connection(api_key) ⇒ Object

Raises:



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, options = {})
  with_error_handling do
    response = connection(options[: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 extract_error_message(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 extract_v1_error_message(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, options = {})
  with_error_handling do
    response = connection(options[: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, options = {})
  begin
    response = connection(options[:creds]).post(path, payload)
  rescue => e
    raise e.inspect
  end
    response.body
end

#v1_connection(api_key, creds) ⇒ Object

Raises:



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, options = {})
  with_v1_error_handling do
    response = v1_connection(options[:api_key], options[:creds]).get(path, params)
    response.body
  end
end

#with_error_handlingObject



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
  message = extract_error_message(e.response[:body]) if e.response
  message ||= e.message

  raise(Error, message)
end

#with_v1_error_handlingObject



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
  message = extract_v1_error_message(e.response[:body]) if e.response
  message ||= e.message

  raise(Error, message)
end