Module: OpenAPI::ClassMethods

Included in:
OpenAPI, Client, Client
Defined in:
lib/openapi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_methodsObject

Returns the value of attribute api_methods.



18
19
20
# File 'lib/openapi/client.rb', line 18

def api_methods
  @api_methods
end

#application_keyObject

Returns the value of attribute application_key.



18
19
20
# File 'lib/openapi/client.rb', line 18

def application_key
  @application_key
end

#application_secretObject

Returns the value of attribute application_secret.



18
19
20
# File 'lib/openapi/client.rb', line 18

def application_secret
  @application_secret
end

#auth_tokenObject

Returns the value of attribute auth_token.

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/openapi/client.rb', line 18

def auth_token
  @auth_token
end

#cacheObject

Returns the value of attribute cache.



18
19
20
# File 'lib/openapi/client.rb', line 18

def cache
  @cache
end

#client_idObject

Returns the value of attribute client_id.



18
19
20
# File 'lib/openapi/client.rb', line 18

def client_id
  @client_id
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/openapi/client.rb', line 18

def logger
  @logger
end

#max_retryObject

Returns the value of attribute max_retry.



18
19
20
# File 'lib/openapi/client.rb', line 18

def max_retry
  @max_retry
end

#request_timeoutObject

Returns the value of attribute request_timeout.



18
19
20
# File 'lib/openapi/client.rb', line 18

def request_timeout
  @request_timeout
end

#siteObject

Returns the value of attribute site.



18
19
20
# File 'lib/openapi/client.rb', line 18

def site
  @site
end

#use_sslObject

Returns the value of attribute use_ssl.



18
19
20
# File 'lib/openapi/client.rb', line 18

def use_ssl
  @use_ssl
end

Instance Method Details

#build_path(path, params = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/openapi/client.rb', line 47

def build_path(path, params=nil)
  uri = URI("/#{path}")
  if params != nil
    uri.query = URI.encode_www_form(params)
  end
  return uri
end

#call_api(request) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openapi/client.rb', line 31

def call_api(request)
  Timer.timeout(request_timeout) do
    start_time = Time.now
    response = http_client.request(request)
    log_request_and_response(request, response, Time.now - start_time)

    response = OpenAPI::Response.wrap(response)
    return response
  end
rescue Timeout::Error => e
  unless logger.nil?
    logger.error "Request timed out after #{request_timeout} seconds: [#{request.path} #{request.body}]"
  end
  raise e
end

#create_method(name, callback) ⇒ Object



25
26
27
28
29
# File 'lib/openapi/client.rb', line 25

def create_method(name, callback)
  metaclass.instance_eval do
    define_method(name, callback)
  end
end

#do_request(http_method, path, params: {}, body: nil, headers: {}, options: {}) ⇒ Object



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

def do_request(http_method, path, params: {}, body: nil, headers: {}, options: {})
  path = build_path(path, params)
  klass = Net::HTTP.const_get(http_method.to_s.capitalize)
  request = klass.new(path.to_s)
  request.add_field "Content-Type", headers["Content-Type"] || "application/json"

  if options[:skip_auth] != true && !auth_token.nil?
    auth_token.headers.each do |k,v|
      request.add_field k, v
    end
  end

  request.add_field "Accept", headers["Accept"] || "application/json"
  headers.each do |h,v|
    request.add_field h, v
  end
  request.body = body if body.present?
  response = call_api(request)
  return response
end

#format_time(time) ⇒ Object



106
107
108
109
# File 'lib/openapi/client.rb', line 106

def format_time(time)
  time = Time.parse(time) if time.is_a?(String)
  time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
end

#http_clientObject



89
90
91
92
93
94
95
96
# File 'lib/openapi/client.rb', line 89

def http_client
  uri = URI(site)
  client = Net::HTTP.new(uri.hostname, uri.port)
  if uri.scheme == "https"
    client.tap{|http| http.use_ssl = true}
  end
  return client
end

#log_request_and_response(request, response, time) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/openapi/client.rb', line 98

def log_request_and_response(request, response, time)
  return if logger.nil?
  time = (time * 1000).to_i
  http_method = request.class.to_s.split('::')[-1]
  logger.info "#{self.class.name} (#{time}ms): [#{http_method} #{request.path} #{request.to_hash}, #{request.body}], [#{response.code}, #{response.body}]"
  logger.flush if logger.respond_to?(:flush)
end

#verify_configuration_values(*symbols) ⇒ Object



80
81
82
83
# File 'lib/openapi/client.rb', line 80

def verify_configuration_values(*symbols)
  absent_values = symbols.select{|symbol| instance_variable_get("@#{symbol}").nil? }
  raise("Must configure #{absent_values.join(", ")} before making this request.") unless absent_values.empty?
end