Class: Etna::Client

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

Defined Under Namespace

Classes: Retrier, TokenRefresher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, token, routes_available: true, ignore_ssl: false, max_retries: 10, backoff_time: 15, logger: nil) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/etna/client.rb', line 7

def initialize(host, token, routes_available: true, ignore_ssl: false, max_retries: 10, backoff_time: 15, logger: nil)
  @host = host.sub(%r!/$!, '')
  @token = token
  @ignore_ssl = ignore_ssl
  @max_retries = max_retries
  @backoff_time = backoff_time

  default_logger = ::Etna::Logger.new('/dev/stdout', 0, 1048576)
  default_logger.level = ::Logger::WARN
  @logger = logger || default_logger

  if routes_available
    set_routes
    define_route_helpers
  end
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



24
25
26
# File 'lib/etna/client.rb', line 24

def routes
  @routes
end

Instance Method Details

#delete(endpoint, params = {}, &block) ⇒ Object



83
84
85
# File 'lib/etna/client.rb', line 83

def delete(endpoint, params = {}, &block)
  body_request(Net::HTTP::Delete, endpoint, params, &block)
end

#get(endpoint, params = {}, &block) ⇒ Object



71
72
73
# File 'lib/etna/client.rb', line 71

def get(endpoint, params = {}, &block)
  query_request(Net::HTTP::Get, endpoint, params, &block)
end

#head(endpoint, params = {}, &block) ⇒ Object



75
76
77
# File 'lib/etna/client.rb', line 75

def head(endpoint, params = {}, &block)
  query_request(Net::HTTP::Head, endpoint, params, &block)
end

#multipart_post(endpoint, content, &block) ⇒ Object



60
61
62
63
64
65
# File 'lib/etna/client.rb', line 60

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}")
  retrier.retry_request(uri, multipart, &block)
end

#options(endpoint, params = {}, &block) ⇒ Object



79
80
81
# File 'lib/etna/client.rb', line 79

def options(endpoint, params = {}, &block)
  query_request(Net::HTTP::Options, endpoint, params, &block)
end

#post(endpoint, params = {}, &block) ⇒ Object



67
68
69
# File 'lib/etna/client.rb', line 67

def post(endpoint, params = {}, &block)
  body_request(Net::HTTP::Post, endpoint, params, &block)
end

#route_path(route, params) ⇒ Object



56
57
58
# File 'lib/etna/client.rb', line 56

def route_path(route, params)
  Etna::Route.path(route[:route], params)
end

#signed_route_path(route, params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/etna/client.rb', line 33

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



26
27
28
29
30
31
# File 'lib/etna/client.rb', line 26

def with_headers(headers, &block)
  @request_headers = headers.compact
  result = instance_eval(&block)
  @request_headers = nil
  return result
end