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.



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

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.



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

def routes
  @routes
end

Instance Method Details

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



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

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

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



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

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

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



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

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

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



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

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



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

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

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



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

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

#route_path(route, params) ⇒ Object



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

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

#signed_route_path(route, params) ⇒ Object



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

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



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

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