Class: K2Subscribe

Inherits:
Object
  • Object
show all
Includes:
K2Utilities, K2Validation
Defined in:
lib/k2-connect-ruby/k2_entity/k2_subscribe.rb

Overview

Class for Subscription Service

Constant Summary

Constants included from K2Validation

K2Validation::ALL_EVENT_TYPES, K2Validation::TILL_SCOPE_EVENT_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from K2Validation

#determine_scope_details, #incorrect_keys, #nil_values, #to_indifferent_access, #validate_email, #validate_hash, #validate_input, #validate_network, #validate_phone, #validate_settlement_method, #validate_till_number_prefix, #validate_url, #validate_webhook, #validate_webhook_input

Methods included from K2Utilities

#make_hash

Constructor Details

#initialize(access_token) ⇒ K2Subscribe

Initialize with access token

Raises:

  • (ArgumentError)


8
9
10
11
12
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 8

def initialize(access_token)
  raise ArgumentError, 'Nil or Empty Access Token Given!' if access_token.blank?
  @threads = []
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



5
6
7
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 5

def access_token
  @access_token
end

#k2_response_bodyObject (readonly)

Returns the value of attribute k2_response_body.



4
5
6
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 4

def k2_response_body
  @k2_response_body
end

#location_urlObject (readonly)

Returns the value of attribute location_url.



4
5
6
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 4

def location_url
  @location_url
end

#webhook_secretObject

Returns the value of attribute webhook_secret.



5
6
7
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 5

def webhook_secret
  @webhook_secret
end

Instance Method Details

#query_resource_url(url) ⇒ Object

Query Specific Webhook URL



39
40
41
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 39

def query_resource_url(url)
  query_webhook(url)
end

#query_webhook(location_url = @location_url) ⇒ Object

Query Recent Webhook



29
30
31
32
33
34
35
36
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 29

def query_webhook(location_url = @location_url)
  query_hash = make_hash(location_url, 'get', @access_token, 'Subscription', nil)
  @threads << Thread.new do
    sleep 0.25
    @k2_response_body = K2Connect.make_request(query_hash)
  end
  @threads.each(&:join)
end

#webhook_subscribe(params) ⇒ Object

Implemented a Case condition that minimises repetition



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/k2-connect-ruby/k2_entity/k2_subscribe.rb', line 15

def webhook_subscribe(params)
  params = validate_webhook_input(params)
  validate_webhook(params)
  k2_request_body = {
      event_type: params[:event_type],
      url: params[:url],
      scope: params[:scope],
      scope_reference: params[:scope_reference]
  }
  subscribe_hash = make_hash(K2Config.path_url('webhook_subscriptions'), 'post', @access_token,'Subscription', k2_request_body)
  @location_url =  K2Connect.make_request(subscribe_hash)
end