Class: YandexDelivery::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/yandex-delivery/request.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
60
DEFAULT_OPEN_TIMEOUT =
60

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, app_id: nil, api_endpoint: nil, timeout: nil, open_timeout: nil, proxy: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/yandex-delivery/request.rb', line 8

def initialize(api_key: nil, app_id: nil, api_endpoint: nil, timeout: nil, open_timeout: nil, proxy: nil, faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil)
  @path_parts = []
  @api_key = api_key || self.class.api_key || ENV['YANDEX_DELIVERY_ACCESS_TOKEN']
  @api_key = @api_key.strip if @api_key
  @app_id = app_id || self.class.app_id || ENV['YANDEX_DELIVERY_APP_ID']
  @app_id = @app_id.strip if @app_id
  @api_endpoint = api_endpoint || self.class.api_endpoint
  @timeout = timeout || self.class.timeout || DEFAULT_TIMEOUT
  @open_timeout = open_timeout || self.class.open_timeout || DEFAULT_OPEN_TIMEOUT
  @proxy = proxy || self.class.proxy || ENV['YANDEX_PROXY']
  @faraday_adapter = faraday_adapter || self.class.faraday_adapter || Faraday.default_adapter
  @symbolize_keys = symbolize_keys || self.class.symbolize_keys || false
  @debug = debug || self.class.debug || false
  @logger = logger || self.class.logger || ::Logger.new(STDOUT)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/yandex-delivery/request.rb', line 24

def method_missing(method, *args)
  # To support underscores, we replace them with hyphens when calling the API
  @path_parts << method.to_s.gsub("_", "-").downcase
  @path_parts << args if args.length > 0
  @path_parts.flatten!
  self
end

Class Attribute Details

.api_endpointObject

Returns the value of attribute api_endpoint.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def api_endpoint
  @api_endpoint
end

.api_keyObject

Returns the value of attribute api_key.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def api_key
  @api_key
end

.app_idObject

Returns the value of attribute app_id.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def app_id
  @app_id
end

.debugObject

Returns the value of attribute debug.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def debug
  @debug
end

.faraday_adapterObject

Returns the value of attribute faraday_adapter.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def faraday_adapter
  @faraday_adapter
end

.loggerObject

Returns the value of attribute logger.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def logger
  @logger
end

.open_timeoutObject

Returns the value of attribute open_timeout.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def open_timeout
  @open_timeout
end

.proxyObject

Returns the value of attribute proxy.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def proxy
  @proxy
end

.symbolize_keysObject

Returns the value of attribute symbolize_keys.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def symbolize_keys
  @symbolize_keys
end

.timeoutObject

Returns the value of attribute timeout.



85
86
87
# File 'lib/yandex-delivery/request.rb', line 85

def timeout
  @timeout
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def api_endpoint
  @api_endpoint
end

#api_keyObject

Returns the value of attribute api_key.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def api_key
  @api_key
end

#app_idObject

Returns the value of attribute app_id.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def app_id
  @app_id
end

#debugObject

Returns the value of attribute debug.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def debug
  @debug
end

#faraday_adapterObject

Returns the value of attribute faraday_adapter.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def faraday_adapter
  @faraday_adapter
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def open_timeout
  @open_timeout
end

#proxyObject

Returns the value of attribute proxy.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def proxy
  @proxy
end

#symbolize_keysObject

Returns the value of attribute symbolize_keys.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def symbolize_keys
  @symbolize_keys
end

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/yandex-delivery/request.rb', line 3

def timeout
  @timeout
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



87
88
89
90
91
# File 'lib/yandex-delivery/request.rb', line 87

def method_missing(sym, *args, &block)
  new(api_key: self.api_key, app_id: self.app_id, api_endpoint: self.api_endpoint, timeout: self.timeout,
      open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter, symbolize_keys: self.symbolize_keys,
      debug: self.debug, proxy: self.proxy, logger: self.logger).send(sym, *args, &block)
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/yandex-delivery/request.rb', line 93

def respond_to_missing?(method_name, include_private = false)
  true
end

Instance Method Details

#create(params: nil, headers: nil, body: {}) ⇒ Object



48
49
50
51
52
# File 'lib/yandex-delivery/request.rb', line 48

def create(params: nil, headers: nil, body: {})
  APIRequest.new(builder: self).post(params: params, headers: headers, body: body)
ensure
  reset
end

#delete(params: nil, headers: nil) ⇒ Object



72
73
74
75
76
# File 'lib/yandex-delivery/request.rb', line 72

def delete(params: nil, headers: nil)
  APIRequest.new(builder: self).delete(params: params, headers: headers)
ensure
  reset
end

#pathObject



44
45
46
# File 'lib/yandex-delivery/request.rb', line 44

def path
  @path_parts.join('/')
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/yandex-delivery/request.rb', line 32

def respond_to_missing?(method_name, include_private = false)
  true
end

#retrieve(params: nil, headers: nil) ⇒ Object



66
67
68
69
70
# File 'lib/yandex-delivery/request.rb', line 66

def retrieve(params: nil, headers: nil)
  APIRequest.new(builder: self).get(params: params, headers: headers)
ensure
  reset
end

#send(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/yandex-delivery/request.rb', line 36

def send(*args)
  if args.length == 0
    method_missing(:send, args)
  else
    __send__(*args)
  end
end

#update(params: nil, headers: nil, body: {}) ⇒ Object



54
55
56
57
58
# File 'lib/yandex-delivery/request.rb', line 54

def update(params: nil, headers: nil, body: {})
  APIRequest.new(builder: self).patch(params: params, headers: headers, body: body)
ensure
  reset
end

#upsert(params: nil, headers: nil, body: {}) ⇒ Object



60
61
62
63
64
# File 'lib/yandex-delivery/request.rb', line 60

def upsert(params: nil, headers: nil, body: {})
  APIRequest.new(builder: self).put(params: params, headers: headers, body: body)
ensure
  reset
end