Class: BusinessCentral::Subscription

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

Constant Summary collapse

API_OBJECT =
"subscriptions"
SUPPORTED_METHODS =
[:post]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#build_url, #process

Methods included from ApiMethods

#delete, #get_child, #method_missing, #query, #query_child, #update

Constructor Details

#initialize(client, resource, endpoint) ⇒ Subscription

Returns a new instance of Subscription.


17
18
19
20
21
# File 'lib/business_central/subscription.rb', line 17

def initialize(client, resource, endpoint)
  @client = client.nil? ? BusinessCentral::Client.new() : client
  @resource = resource
  @endpoint = endpoint
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BusinessCentral::ApiMethods

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


15
16
17
# File 'lib/business_central/subscription.rb', line 15

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint


15
16
17
# File 'lib/business_central/subscription.rb', line 15

def endpoint
  @endpoint
end

#resourceObject (readonly)

Returns the value of attribute resource


15
16
17
# File 'lib/business_central/subscription.rb', line 15

def resource
  @resource
end

Instance Method Details

#build_request_objectObject


60
61
# File 'lib/business_central/subscription.rb', line 60

def build_request_object
end

#createObject


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/business_central/subscription.rb', line 31

def create
  request = Net::HTTP::Post.new(URI(request_url))
  request.content_type = "application/json"
  request.basic_auth(@client.api_username, @client.api_password)
  request.body = JSON.generate(post_data)
  request

  response = perform_request(request)

  JSON.parse(response.body)
end

#getObject


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

def get
  request = Net::HTTP::Get.new(URI(request_url))
  request.content_type = "application/json"
  request.basic_auth(@client.api_username, @client.api_password)
  response = perform_request(request)
  JSON.parse(response.body)
end

#perform_request(request) ⇒ Object


43
44
45
46
47
# File 'lib/business_central/subscription.rb', line 43

def perform_request(request)
  Net::HTTP.start(request.uri.hostname, request.uri.port, use_ssl: true) do |http|
    http.request(request)
  end
end

#post_dataObject


53
54
55
56
57
58
# File 'lib/business_central/subscription.rb', line 53

def post_data
  {
    "notificationUrl": @endpoint,
    "resource": @resource
  }
end

#request_urlObject


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

def request_url
  "#{@client.api_host}#{@client.api_version}#{@client.api_path}/subscriptions"
end