Module: ClientApiBuilder::Router

Defined in:
lib/client_api_builder/router.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
# File 'lib/client_api_builder/router.rb', line 6

def self.included(base)
  base.extend InheritanceHelper::Methods
  base.extend ClassMethods
  base.include ::ClientApiBuilder::NetHTTP::Request
  base.attr_reader :response
end

Instance Method Details

#base_url(options) ⇒ Object



220
221
222
# File 'lib/client_api_builder/router.rb', line 220

def base_url(options)
  self.class.base_url
end

#build_body(body, options) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/client_api_builder/router.rb', line 272

def build_body(body, options)
  return unless body
  return body if body.is_a?(String)

  body.merge!(options[:body]) if options[:body]
  body.to_json
end

#build_connection_options(options) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/client_api_builder/router.rb', line 244

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) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/client_api_builder/router.rb', line 224

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]&.each(&add_header_proc)

  headers
end

#build_query(query, options) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/client_api_builder/router.rb', line 252

def build_query(query, options)
  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&.each(&add_query_param_proc)

  self.class.build_query(query_params)
end

#build_uri(path, query, options) ⇒ Object



280
281
282
283
284
# File 'lib/client_api_builder/router.rb', line 280

def build_uri(path, query, options)
  uri = URI(base_url(options) + path)
  uri.query = build_query(query, options) if query
  uri
end

#expected_response_code!(response, expected_response_codes, options) ⇒ Object



286
287
288
289
290
291
# File 'lib/client_api_builder/router.rb', line 286

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) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/client_api_builder/router.rb', line 297

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) ⇒ Object



293
294
295
# File 'lib/client_api_builder/router.rb', line 293

def parse_response(response, options)
  response.body && JSON.parse(response.body)
end