Class: Algolia::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/algolia/api_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Configuration.default) ⇒ ApiClient

Initializes the ApiClient

Parameters:

  • config (Hash) (defaults to: Configuration.default)

    a customizable set of options

Options Hash (config):

  • Configuration (Configuration)

    for initializing the object, default to Configuration.default



12
13
14
15
16
# File 'lib/algolia/api_client.rb', line 12

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

#configObject

The Configuration object holding settings to be used in the API client.



6
7
8
# File 'lib/algolia/api_client.rb', line 6

def config
  @config
end

#transporterObject

Returns the value of attribute transporter.



8
9
10
# File 'lib/algolia/api_client.rb', line 8

def transporter
  @transporter
end

Class Method Details

.defaultObject



18
19
20
# File 'lib/algolia/api_client.rb', line 18

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.

Parameters:

  • collection_format (String)

    one of :csv, :ssv, :tsv, :pipes and :multi



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/algolia/api_client.rb', line 131

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.

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/algolia/api_client.rb', line 25

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.

Parameters:

  • data (Object)

    Data to be converted

  • return_type (String)

    Return type

Returns:

  • (Mixed)

    Data in a particular type



63
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
# File 'lib/algolia/api_client.rb', line 63

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.

Parameters:

  • body (String)

    of the HTTP response

  • return_type (String)

    some examples: “User”, “Array<User>”, “Hash<String, Integer>”



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/algolia/api_client.rb', line 42

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

#encode_uri(uri) ⇒ Object



149
150
151
# File 'lib/algolia/api_client.rb', line 149

def encode_uri(uri)
  CGI.escape(uri).gsub('+', '%20')
end

#object_to_hash(obj) ⇒ String

Convert object(non-array) to hash.

Parameters:

  • obj (Object)

    object to be converted into JSON string

Returns:

  • (String)

    JSON string representation of the object



119
120
121
122
123
124
125
126
127
# File 'lib/algolia/api_client.rb', line 119

def object_to_hash(obj)
  if obj.is_a?(Hash)
    obj.map { |k, v| [k, object_to_hash(v)] }.to_h
  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.

Parameters:

  • model (Object)

    object to be converted into JSON string

Returns:

  • (String)

    JSON string representation of the object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/algolia/api_client.rb', line 104

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