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.
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
.default ⇒ Object
[View source]
19 20 21 |
# File 'lib/algolia/api_client.rb', line 19 def self.default @@default ||= ApiClient.new end |
Instance Method Details
#build_collection_param(param, collection_format) ⇒ Object
Build parameter value according to the given collection format.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/algolia/api_client.rb', line 133 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.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/algolia/api_client.rb', line 26 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.
64 65 66 67 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 |
# File 'lib/algolia/api_client.rb', line 64 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) 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/algolia/api_client.rb', line 43 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.
121 122 123 124 125 126 127 128 129 |
# File 'lib/algolia/api_client.rb', line 121 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.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/algolia/api_client.rb', line 105 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 |