Class: BusinessCentral::RequestBuilder
- Inherits:
-
Object
- Object
- BusinessCentral::RequestBuilder
- Defined in:
- lib/business_central/request_builder.rb
Overview
An object to build the request object
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#etag ⇒ Object
readonly
Returns the value of attribute etag.
-
#request ⇒ Object
Returns the value of attribute request.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Instance Method Summary collapse
-
#add_data ⇒ Object
If form data is supplied, add it to the request body as JSON.
-
#add_etag ⇒ Object
If an etag is supplied, add it to the request in an [‘If-Match’] header.
-
#build ⇒ Object
Create the appropriate request object.
-
#initialize(client, opts = {}) ⇒ RequestBuilder
constructor
A new instance of RequestBuilder.
-
#request_object ⇒ Object
initialization opts Hash.
-
#uri ⇒ Object
Contstruct URI for the request.
Constructor Details
#initialize(client, opts = {}) ⇒ RequestBuilder
Returns a new instance of RequestBuilder.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/business_central/request_builder.rb', line 7 def data @data end |
#etag ⇒ Object (readonly)
Returns the value of attribute etag.
7 8 9 |
# File 'lib/business_central/request_builder.rb', line 7 def etag @etag end |
#request ⇒ Object
Returns the value of attribute request.
6 7 8 |
# File 'lib/business_central/request_builder.rb', line 6 def request @request end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
7 8 9 |
# File 'lib/business_central/request_builder.rb', line 7 def url @url end |
#verb ⇒ Object (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_data ⇒ Object
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_etag ⇒ Object
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 |
#build ⇒ Object
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_object ⇒ Object
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 |
#uri ⇒ Object
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 |