Module: ClientApiBuilder::Router

Defined in:
lib/client_api_builder/router.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
# File 'lib/client_api_builder/router.rb', line 6

def self.included(base)
  base.extend InheritanceHelper::Methods
  base.extend ClassMethods
  base.include ::ClientApiBuilder::NetHTTP::Request
  base.attr_reader :response, :request_options
end

Instance Method Details

#base_url(options) ⇒ Object



257
258
259
# File 'lib/client_api_builder/router.rb', line 257

def base_url(options)
  self.class.base_url
end

#build_body(body, options) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/client_api_builder/router.rb', line 309

def build_body(body, options)
  return unless body
  return body if body.is_a?(String)

  body.merge!(options[:body]) if options[:body]
  body.to_json
end

#build_connection_options(options) ⇒ Object



281
282
283
284
285
286
287
# File 'lib/client_api_builder/router.rb', line 281

def build_connection_options(options)
  if options[:connection_options]
    self.class.connection_options.merge(options[:connection_options])
  else
    self.class.connection_options
  end
end

#build_headers(options) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/client_api_builder/router.rb', line 261

def build_headers(options)
  headers = {}

  add_header_proc = proc do |name, value|
    headers[name] =
      if value.is_a?(Proc)
        instance_eval(&value)
      elsif value.is_a?(Symbol)
        send(value)
      else
        value
      end
  end

  self.class.headers.each(&add_header_proc)
  options[:headers]&.each(&add_header_proc)

  headers
end

#build_query(query, options) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/client_api_builder/router.rb', line 289

def build_query(query, options)
  query_params = {}

  add_query_param_proc = proc do |name, value|
    query_params[name] =
      if value.is_a?(Proc)
        instance_eval(&value)
      elsif value.is_a?(Symbol)
        send(value)
      else
        value
      end
  end

  self.class.query_params.each(&add_query_param_proc)
  query&.each(&add_query_param_proc)

  self.class.build_query(query_params)
end

#build_uri(path, query, options) ⇒ Object



317
318
319
320
321
# File 'lib/client_api_builder/router.rb', line 317

def build_uri(path, query, options)
  uri = URI(base_url(options) + path)
  uri.query = build_query(query, options) if query
  uri
end

#expected_response_code!(response, expected_response_codes, options) ⇒ Object



323
324
325
326
327
328
# File 'lib/client_api_builder/router.rb', line 323

def expected_response_code!(response, expected_response_codes, options)
  return if expected_response_codes.empty? && response.kind_of?(Net::HTTPSuccess)
  return if expected_response_codes.include?(response.code)

  raise(::ClientApiBuilder::UnexpectedResponse.new("unexpected response code #{response.code}", response))
end

#handle_response(response, options, &block) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/client_api_builder/router.rb', line 334

def handle_response(response, options, &block)
  data =
    case options[:return]
    when :response
      response
    when :body
      response.body
    else
      parse_response(response, options)
    end

  if block
    instance_exec(data, &block)
  else
    data
  end
end

#parse_response(response, options) ⇒ Object



330
331
332
# File 'lib/client_api_builder/router.rb', line 330

def parse_response(response, options)
  response.body && JSON.parse(response.body)
end