Class: Koine::RestClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/koine/rest_client/request.rb

Overview

request object :reek:TooManyInstanceVariables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: '', query_params: {}, path: '', headers: {}, method: 'get') ⇒ Request

Returns a new instance of Request.



15
16
17
18
19
20
21
# File 'lib/koine/rest_client/request.rb', line 15

def initialize(base_url: '', query_params: {}, path: '', headers: {}, method: 'get')
  @method = method
  @base_url = base_url
  @path = path
  @query_params = query_params
  @headers = headers
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



9
10
11
# File 'lib/koine/rest_client/request.rb', line 9

def base_url
  @base_url
end

#bodyObject

Returns the value of attribute body.



13
14
15
# File 'lib/koine/rest_client/request.rb', line 13

def body
  @body
end

#headersObject

Returns the value of attribute headers.



11
12
13
# File 'lib/koine/rest_client/request.rb', line 11

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



8
9
10
# File 'lib/koine/rest_client/request.rb', line 8

def method
  @method
end

#pathObject

Returns the value of attribute path.



10
11
12
# File 'lib/koine/rest_client/request.rb', line 10

def path
  @path
end

#query_paramsObject

Returns the value of attribute query_params.



12
13
14
# File 'lib/koine/rest_client/request.rb', line 12

def query_params
  @query_params
end

Instance Method Details

#optionsObject



52
53
54
55
56
# File 'lib/koine/rest_client/request.rb', line 52

def options
  { body: body, headers: headers }.compact.reject do |_key, value|
    value.empty?
  end
end

#urlObject



47
48
49
50
# File 'lib/koine/rest_client/request.rb', line 47

def url
  url = "#{@base_url.delete_suffix('/')}/#{path.delete_prefix('/')}"
  Url.new(url).with_query_params(query_params).to_s(unescape: ',')
end

#with_added_headers(headers) ⇒ Object



39
40
41
# File 'lib/koine/rest_client/request.rb', line 39

def with_added_headers(headers)
  new(:headers, @headers.merge(headers).compact)
end

#with_added_options(options) ⇒ Object

:reek:ManualDispatch



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/koine/rest_client/request.rb', line 59

def with_added_options(options)
  object = self
  options.each do |key, value|
    if respond_to?("with_#{key}")
      object = object.send("with_#{key}", value)
    end

    if respond_to?("with_added_#{key}")
      object = object.send("with_added_#{key}", value)
    end
  end
  object
end

#with_added_query_params(query_params) ⇒ Object



35
36
37
# File 'lib/koine/rest_client/request.rb', line 35

def with_added_query_params(query_params)
  new(:query_params, @query_params.merge(query_params).compact)
end

#with_base_url(base_url) ⇒ Object



31
32
33
# File 'lib/koine/rest_client/request.rb', line 31

def with_base_url(base_url)
  new(:base_url, base_url)
end

#with_body(body) ⇒ Object



43
44
45
# File 'lib/koine/rest_client/request.rb', line 43

def with_body(body)
  new(:body, body)
end

#with_method(method) ⇒ Object



23
24
25
# File 'lib/koine/rest_client/request.rb', line 23

def with_method(method)
  new(:method, method)
end

#with_path(path) ⇒ Object



27
28
29
# File 'lib/koine/rest_client/request.rb', line 27

def with_path(path)
  new(:path, path)
end