module ClientApiBuilder::Router

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

base_url(options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/client_api_builder/router.rb, line 394
def parse_response(response, options)
  response.body && JSON.parse(response.body)
end
root_router() click to toggle source
# File lib/client_api_builder/router.rb, line 416
def root_router
  self
end