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.



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

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



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

def access_token
  options[:access_token]
end

#access_token=(value) ⇒ Object



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

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

#apply_auth_middleware(faraday_builder) ⇒ Object



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

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

#connObject



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

def conn
  @conn ||= connection
end

#connectionObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/garage_client/client.rb', line 58

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

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

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

#headersObject Also known as: default_headers



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

def headers
  @headers ||= GarageClient.configuration.headers.merge(given_headers.stringify_keys)
end

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



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

def headers=(value)
  @headers = value
end

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



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

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