Class: Amply::Client
- Inherits:
-
Object
- Object
- Amply::Client
- Defined in:
- lib/amply/client.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ Accept: 'application/json', 'Content-Type': 'application/json' }
- @@access_token =
''- @@url =
'https://sendamply.com/api/v1'
Class Method Summary collapse
- .auth_header ⇒ Object
- .parse_response(resp) ⇒ Object
- .post(path, body, options = {}) ⇒ Object
- .set_access_token(token) ⇒ Object
Class Method Details
.auth_header ⇒ Object
50 51 52 |
# File 'lib/amply/client.rb', line 50 def auth_header { Authorization: "Bearer #{@@access_token}" } end |
.parse_response(resp) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/amply/client.rb', line 32 def parse_response(resp) code = resp.code.to_i if [301, 302].include?(code) return resp['location'] elsif [401, 403].include?(code) raise Exceptions::APIException, resp elsif code == 404 raise Exceptions::ResourcNotFoundException, resp elsif code == 422 raise Exceptions::ValidationException, resp elsif code < 200 || code >= 300 raise Exceptions::APIException, resp end JSON.parse(resp.body, symbolize_names: true) end |
.post(path, body, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/amply/client.rb', line 19 def post(path, body, = {}) uri = URI("#{@@url}#{path}") headers = [ DEFAULT_HEADERS, [:headers] || {}, auth_header ].inject(&:merge) resp = Net::HTTP.post(uri, body.to_json, headers) parse_response(resp) end |
.set_access_token(token) ⇒ Object
15 16 17 |
# File 'lib/amply/client.rb', line 15 def set_access_token(token) @@access_token = token end |