Class: BusinessCentral::RequestBuilder

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

Overview

An object to build the request object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, opts = {}) ⇒ RequestBuilder

Returns a new instance of RequestBuilder.

Parameters:



12
13
14
15
16
17
18
19
# File 'lib/business_central/request_builder.rb', line 12

def initialize(client, opts = {})
  @client = client
  @verb = opts[:verb] if opts.has_key?(:verb)
  @url = opts[:url] if opts.has_key?(:url)
  @etag = opts[:etag] if opts.has_key?(:etag)
  @data = opts[:data] if opts.has_key?(:data)
  build
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/business_central/request_builder.rb', line 7

def data
  @data
end

#etagObject (readonly)

Returns the value of attribute etag.



7
8
9
# File 'lib/business_central/request_builder.rb', line 7

def etag
  @etag
end

#requestObject

Returns the value of attribute request.



6
7
8
# File 'lib/business_central/request_builder.rb', line 6

def request
  @request
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/business_central/request_builder.rb', line 7

def url
  @url
end

#verbObject (readonly)

Returns the value of attribute verb.



7
8
9
# File 'lib/business_central/request_builder.rb', line 7

def verb
  @verb
end

Instance Method Details

#add_dataObject

If form data is supplied, add it to the request body as JSON



41
42
43
44
# File 'lib/business_central/request_builder.rb', line 41

def add_data
  return if @data.nil?
  @request.body = JSON.generate(@data)
end

#add_etagObject

If an etag is supplied, add it to the request in an [‘If-Match’] header



34
35
36
37
# File 'lib/business_central/request_builder.rb', line 34

def add_etag
  return if @etag.nil?
  @request['If-Match'] = @etag
end

#buildObject

Create the appropriate request object

Populates @request with the constructed object



24
25
26
27
28
29
30
# File 'lib/business_central/request_builder.rb', line 24

def build
  @request = request_object.new(uri)
  @request.content_type = "application/json"
  @request.basic_auth(@client.api_username, @client.api_password)
  add_etag
  add_data
end

#request_objectObject

initialization opts Hash



49
50
51
# File 'lib/business_central/request_builder.rb', line 49

def request_object
  Object.const_get("Net::HTTP::#{@verb.capitalize}")
end

#uriObject

Contstruct URI for the request



57
58
59
# File 'lib/business_central/request_builder.rb', line 57

def uri
  URI(@client.base_url + @url)
end