Class: GarageClient::Client

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/garage_client/client.rb

Constant Summary

Constants included from Request

Request::MIME_DICT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • opts (Hash)

    a customizable set of options



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

def initialize(options = {})
  require_necessaries(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/garage_client/client.rb', line 15

def options
  @options
end

Class Method Details

.property(key) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/garage_client/client.rb', line 5

def self.property(key)
  define_method(key) do
    options.fetch(key) { GarageClient.configuration.send(key) }
  end

  define_method("#{key}=") do |value|
    options[key] = value
  end
end

Instance Method Details

#access_tokenObject



39
40
41
# File 'lib/garage_client/client.rb', line 39

def access_token
  options[:access_token]
end

#access_token=(value) ⇒ Object



43
44
45
# File 'lib/garage_client/client.rb', line 43

def access_token=(value)
  options[:access_token] = value
end

#apply_auth_middleware(faraday_builder) ⇒ Object



55
56
57
# File 'lib/garage_client/client.rb', line 55

def apply_auth_middleware(faraday_builder)
  faraday_builder.request :authorization, :Bearer, access_token if access_token
end

#connObject



51
52
53
# File 'lib/garage_client/client.rb', line 51

def conn
  @conn ||= connection
end

#connectionObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/garage_client/client.rb', line 59

def connection
  Faraday.new(headers: headers, url: endpoint) do |builder|
    # Response Middlewares
    builder.use Faraday::Response::Logger if verbose
    builder.use Faraday::Mashify::Middleware
    builder.use Faraday::Response::Json, content_type: /\bjson$/
    builder.use GarageClient::Response::Cacheable, cacher: cacher if cacher
    builder.use GarageClient::Response::RaiseHttpException

    # Request Middlewares
    builder.use Faraday::Multipart::Middleware
    builder.use GarageClient::Request::JsonEncoded
    builder.use GarageClient::Request::PropagateRequestId

    # Tracing Middlewares
    if options[:tracing]
      case options[:tracing][:tracer]
      when 'aws-xray'
        service = options[:tracing][:service]
        raise 'Configure target service name with `tracing.service`' unless service
        require 'aws/xray/faraday'
        builder.use Aws::Xray::Faraday, service
      else
        raise "`tracing` option specified but GarageClient does not support the tracer: #{options[:tracing][:tracer]}"
      end
    end

    # Low-level Middlewares
    apply_auth_middleware builder
    builder.adapter(*adapter)
  end
end

#headersObject Also known as: default_headers



29
30
31
# File 'lib/garage_client/client.rb', line 29

def headers
  @headers ||= GarageClient.configuration.headers.merge(given_headers.transform_keys(&:to_s))
end

#headers=(value) ⇒ Object Also known as: default_headers=



34
35
36
# File 'lib/garage_client/client.rb', line 34

def headers=(value)
  @headers = value
end

#me(params = {}, options = {}) ⇒ Object



47
48
49
# File 'lib/garage_client/client.rb', line 47

def me(params = {}, options = {})
  get('/me', params, options)
end