Class: Algolia::ApiClient
- Inherits:
-
Object
- Object
- Algolia::ApiClient
- Defined in:
- lib/algolia/api_client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
The Configuration object holding settings to be used in the API client.
-
#transporter ⇒ Object
Returns the value of attribute transporter.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_collection_param(param, collection_format) ⇒ Object
Build parameter value according to the given collection format.
-
#call_api(http_method, path, opts = {}) ⇒ Http::Response
Call an API with given options.
-
#convert_to_type(data, return_type) ⇒ Mixed
Convert data to the given return type.
-
#deserialize(body, return_type) ⇒ Object
Deserialize the response to the given return type.
-
#initialize(config = Configuration.default) ⇒ ApiClient
constructor
Initializes the ApiClient.
-
#object_to_hash(obj) ⇒ String
Convert object(non-array) to hash.
-
#object_to_http_body(model) ⇒ String
Convert object (array, hash, object, etc) to JSON string.
- #set_client_api_key(api_key) ⇒ Object
Constructor Details
#initialize(config = Configuration.default) ⇒ ApiClient
Initializes the ApiClient
13 14 15 16 17 |
# File 'lib/algolia/api_client.rb', line 13 def initialize(config = Configuration.default) @config = config @requester = config.requester || Http::HttpRequester.new("net_http_persistent", LoggerHelper.create) @transporter = Transport::Transport.new(config, @requester) end |
Instance Attribute Details
#config ⇒ Object
The Configuration object holding settings to be used in the API client.
7 8 9 |
# File 'lib/algolia/api_client.rb', line 7 def config @config end |
#transporter ⇒ Object
Returns the value of attribute transporter.
9 10 11 |
# File 'lib/algolia/api_client.rb', line 9 def transporter @transporter end |
Class Method Details
Instance Method Details
#build_collection_param(param, collection_format) ⇒ Object
Build parameter value according to the given collection format.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/algolia/api_client.rb', line 137 def build_collection_param(param, collection_format) case collection_format when :csv param.join(",") when :ssv param.join(" ") when :tsv param.join("\t") when :pipes param.join("|") when :multi # return the array directly as typhoeus will handle it as expected param else raise "unknown collection format: #{collection_format.inspect}" end end |
#call_api(http_method, path, opts = {}) ⇒ Http::Response
Call an API with given options.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/algolia/api_client.rb', line 30 def call_api(http_method, path, opts = {}) begin call_type = opts[:use_read_transporter] || http_method == :GET ? CallType::READ : CallType::WRITE response = transporter.request(call_type, http_method, path, opts[:body], opts) rescue Faraday::TimeoutError raise ApiError, "Connection timed out" rescue Faraday::ConnectionFailed raise ApiError, "Connection failed" end response end |
#convert_to_type(data, return_type) ⇒ Mixed
Convert data to the given return type.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/algolia/api_client.rb', line 68 def convert_to_type(data, return_type) return nil if data.nil? case return_type when "String" data.to_s when "Integer" data.to_i when "Float" data.to_f when "Boolean" data == true when "Time" # parse date time (expecting ISO 8601 format) Time.parse(data) when "Date" # parse date time (expecting ISO 8601 format) Date.parse(data) when "Object" # generic object (usually a Hash), return directly data when /\AArray<(.+)>\z/ # e.g. Array<Pet> sub_type = ::Regexp.last_match(1) data.map { |item| convert_to_type(item, sub_type) } when /\AHash<String, (.+)>\z/ # e.g. Hash<String, Integer> sub_type = ::Regexp.last_match(1) {}.tap do |hash| data.each { |k, v| hash[k] = convert_to_type(v, sub_type) } end else # models (e.g. Pet), enum, or oneOf klass = Algolia.const_get(return_type) klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data) end end |
#deserialize(body, return_type) ⇒ Object
Deserialize the response to the given return type.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/algolia/api_client.rb', line 47 def deserialize(body, return_type) return nil if body.nil? || body.empty? # return response body directly for String return type return body.to_s if return_type == "String" begin data = JSON.parse("[#{body}]", :symbolize_names => true)[0] rescue JSON::ParserError => e raise e unless %w[String Date Time].include?(return_type) data = body end convert_to_type(data, return_type) end |
#object_to_hash(obj) ⇒ String
Convert object(non-array) to hash.
125 126 127 128 129 130 131 132 133 |
# File 'lib/algolia/api_client.rb', line 125 def object_to_hash(obj) if obj.is_a?(Hash) obj.to_h { |k, v| [k, object_to_hash(v)] } elsif obj.respond_to?(:to_hash) obj.to_hash else obj end end |
#object_to_http_body(model) ⇒ String
Convert object (array, hash, object, etc) to JSON string.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/algolia/api_client.rb', line 109 def object_to_http_body(model) return "{}" if model.nil? return model if model.is_a?(String) body = if model.is_a?(Array) model.map { |m| object_to_hash(m) } else object_to_hash(model) end body.to_json end |
#set_client_api_key(api_key) ⇒ Object
23 24 25 |
# File 'lib/algolia/api_client.rb', line 23 def set_client_api_key(api_key) @config.set_client_api_key(api_key) end |