Module: ClientApiBuilder::Router

Included in:
NestedRouter
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
12
# File 'lib/client_api_builder/router.rb', line 6

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

Instance Method Details

#base_url(options) ⇒ Object



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

def base_url(options)
  options[:base_url] || self.class.base_url
end

#build_body(body, options) ⇒ Object



372
373
374
375
376
377
378
379
# File 'lib/client_api_builder/router.rb', line 372

def build_body(body, options)
  body = options[:body] if options.key?(:body)

  return nil unless body
  return body if body.is_a?(String)

  self.class.build_body(self, body, options)
end

#build_connection_options(options) ⇒ Object



341
342
343
344
345
346
347
# File 'lib/client_api_builder/router.rb', line 341

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



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/client_api_builder/router.rb', line 321

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] && options[:headers].each(&add_header_proc)

  headers
end

#build_query(query, options) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/client_api_builder/router.rb', line 349

def build_query(query, options)
  return nil if query.nil? && self.class.query_params.empty?

  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 && query.each(&add_query_param_proc)
  options[:query] && options[:query].each(&add_query_param_proc)

  self.class.build_query(query_params)
end

#build_uri(path, query, options) ⇒ Object



381
382
383
384
385
# File 'lib/client_api_builder/router.rb', line 381

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

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



387
388
389
390
391
392
# File 'lib/client_api_builder/router.rb', line 387

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



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/client_api_builder/router.rb', line 398

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



394
395
396
# File 'lib/client_api_builder/router.rb', line 394

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

#root_routerObject



416
417
418
# File 'lib/client_api_builder/router.rb', line 416

def root_router
  self
end