Class: KOSapiClient::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/kosapi_client/request_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, http_client, url_builder = URLBuilder.new(resource_name.to_s)) ⇒ RequestBuilder

Returns a new instance of RequestBuilder.



35
36
37
38
39
40
41
42
43
# File 'lib/kosapi_client/request_builder.rb', line 35

def initialize(resource_name, http_client, url_builder = URLBuilder.new(resource_name.to_s))
  @base_url = resource_name
  @http_client = http_client
  @operation = :get
  @body = nil
  @headers = { Accept: 'application/atom+xml' }
  @url_builder = url_builder
  @id = nil
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/kosapi_client/request_builder.rb', line 4

def response
  @response
end

Instance Method Details

#finalizeObject



45
46
47
48
# File 'lib/kosapi_client/request_builder.rb', line 45

def finalize
  options = {headers: @headers, body: @body }
  @response = @http_client.send_request(@operation, @url_builder.url, options)
end

#find(id) ⇒ Object



6
7
8
9
10
# File 'lib/kosapi_client/request_builder.rb', line 6

def find(id)
  @id = id
  @url_builder.set_path(id)
  self
end

#limit(num) ⇒ Object



17
18
19
20
# File 'lib/kosapi_client/request_builder.rb', line 17

def limit(num)
  @url_builder.set_query_param(:limit, num)
  self
end

#offset(num) ⇒ Object



12
13
14
15
# File 'lib/kosapi_client/request_builder.rb', line 12

def offset(num)
  @url_builder.set_query_param(:offset, num)
  self
end

#query(params = {}) ⇒ Object Also known as: where



22
23
24
25
26
27
28
29
30
31
# File 'lib/kosapi_client/request_builder.rb', line 22

def query(params = {})
  raise 'Empty parameters to query are not allowed' if params.empty?
  if params.instance_of?(String)
    rsql = params
  else
    rsql = params.map { |k, v| "#{k}==#{v}" }.join(';')
  end
  @url_builder.set_query_param(:query, rsql)
  self
end