Class: Etna::Client
- Inherits:
-
Object
- Object
- Etna::Client
- Defined in:
- lib/etna/client.rb
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #delete(endpoint, params = {}, &block) ⇒ Object
- #get(endpoint, params = {}, &block) ⇒ Object
- #head(endpoint, params = {}, &block) ⇒ Object
-
#initialize(host, token, routes_available: true, ignore_ssl: false) ⇒ Client
constructor
A new instance of Client.
- #multipart_post(endpoint, content, &block) ⇒ Object
- #options(endpoint, params = {}, &block) ⇒ Object
- #post(endpoint, params = {}, &block) ⇒ Object
- #route_path(route, params) ⇒ Object
- #signed_route_path(route, params) ⇒ Object
- #with_headers(headers, &block) ⇒ Object
Constructor Details
#initialize(host, token, routes_available: true, ignore_ssl: false) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/etna/client.rb', line 7 def initialize(host, token, routes_available: true, ignore_ssl: false) @host = host.sub(%r!/$!, '') @token = token @ignore_ssl = ignore_ssl if routes_available set_routes define_route_helpers end end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
18 19 20 |
# File 'lib/etna/client.rb', line 18 def routes @routes end |
Instance Method Details
#delete(endpoint, params = {}, &block) ⇒ Object
77 78 79 |
# File 'lib/etna/client.rb', line 77 def delete(endpoint, params = {}, &block) body_request(Net::HTTP::Delete, endpoint, params, &block) end |
#get(endpoint, params = {}, &block) ⇒ Object
65 66 67 |
# File 'lib/etna/client.rb', line 65 def get(endpoint, params = {}, &block) query_request(Net::HTTP::Get, endpoint, params, &block) end |
#head(endpoint, params = {}, &block) ⇒ Object
69 70 71 |
# File 'lib/etna/client.rb', line 69 def head(endpoint, params = {}, &block) query_request(Net::HTTP::Head, endpoint, params, &block) end |
#multipart_post(endpoint, content, &block) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/etna/client.rb', line 54 def multipart_post(endpoint, content, &block) uri = request_uri(endpoint) multipart = Net::HTTP::Post::Multipart.new uri.request_uri, content multipart.add_field('Authorization', "Etna #{@token}") request(uri, multipart, &block) end |
#options(endpoint, params = {}, &block) ⇒ Object
73 74 75 |
# File 'lib/etna/client.rb', line 73 def (endpoint, params = {}, &block) query_request(Net::HTTP::Options, endpoint, params, &block) end |
#post(endpoint, params = {}, &block) ⇒ Object
61 62 63 |
# File 'lib/etna/client.rb', line 61 def post(endpoint, params = {}, &block) body_request(Net::HTTP::Post, endpoint, params, &block) end |
#route_path(route, params) ⇒ Object
50 51 52 |
# File 'lib/etna/client.rb', line 50 def route_path(route, params) Etna::Route.path(route[:route], params) end |
#signed_route_path(route, params) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/etna/client.rb', line 27 def signed_route_path(route, params) path = route_path(route,params) signatory = params.delete(:signatory) return path unless signatory hmac = Etna::Hmac.new( signatory, method: route[:method], host: URI(@host).host, path: path, expiration: (DateTime.now + 10).iso8601, id: signatory.id, nonce: SecureRandom.hex, headers: params.except(*route[:params].map(&:to_sym)) ) url_params = hmac.url_params(route[:method] == 'GET') return url_params[:path] + '?' + url_params[:query] end |
#with_headers(headers, &block) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/etna/client.rb', line 20 def with_headers(headers, &block) @request_headers = headers.compact result = instance_eval(&block) @request_headers = nil return result end |