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.



55
56
57
58
59
60
61
62
63
# File 'lib/kosapi_client/request_builder.rb', line 55

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



65
66
67
68
# File 'lib/kosapi_client/request_builder.rb', line 65

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

#order_by(*args, **kwargs) ⇒ Object Also known as: order

Specify order of the results.

Examples:

.order_by(:firstName, :lastName)
.order_by(:firstName, lastName: :desc)
.order_by(firstName: :asc, lastName: desc)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kosapi_client/request_builder.rb', line 41

def order_by(*args, **kwargs)
  ordering = args + kwargs.map do |attr, dir|
    if dir != :asc && dir != :desc
      raise ArgumentError, "Direction must be :asc, or :desc, but got #{dir.inspect}"
    end
    "#{attr}@#{dir}"
  end

  @url_builder.set_query_param(:orderBy, ordering.join(','))
  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