Class: Killbill::Aviate::AviateClient

Inherits:
KillBillClient::Model::Resource
  • Object
show all
Defined in:
lib/aviate/client.rb

Constant Summary collapse

KILLBILL_AVIATE_PREFIX =
'/plugins/aviate-plugin/v1'

Class Method Summary collapse

Class Method Details

.authenticate(email, password, options = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aviate/client.rb', line 84

def authenticate(email, password, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/auth"
  auth = Base64.strict_encode64("#{email}:#{password}")

  base_url = KillBillClient::API.base_uri
  uri = URI("#{base_url}#{path}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new(uri.path)
  request['Content-Type'] = 'application/json'
  request['X-Killbill-ApiKey'] = options[:api_key]
  request['X-Killbill-ApiSecret'] = options[:api_secret]
  request['Authorization'] = "Basic #{auth}"
  request.body = {}.to_json
  response = http.request(request)
  JSON.parse(response.body)
rescue StandardError => e
  e.message.to_s
end

.aviate_plugin_available?(options = nil) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/aviate/client.rb', line 9

def aviate_plugin_available?(options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/health/data"
  
  request_options = build_request_options(options)
  
  KillBillClient::API.get path, {}, request_options

  [true, nil]
rescue KillBillClient::API::ResponseError => e
  [false, e.message.to_s]
end

.aviate_plugin_installed(options = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/aviate/client.rb', line 21

def aviate_plugin_installed(options = nil)
  nodes_info = KillBillClient::Model::NodesInfo.nodes_info(options) || []
  plugins_info = nodes_info.empty? ? [] : (nodes_info.first.plugins_info || [])

  return [true, nil] if plugins_info.any? { |plugin| plugin.plugin_name == 'aviate-plugin' }

  [false, nil]
rescue KillBillClient::API::ResponseError => e
  [false, e.message.to_s]
end

.create_wallet(account_id, wallet, options = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aviate/client.rb', line 57

def create_wallet(, wallet, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/wallet"
  body = {
    kbAccountId: ,
    currency: wallet[:currency],
    initCredit: {
      creditType: wallet[:credit_type],
      amount: wallet[:amount],
      expDate: wallet[:exp_date]
    },
    topOff: {
      topOffType: wallet[:top_off_type],
      lowWatermark: wallet[:low_watermark],
      amount: wallet[:top_off_amount],
      expDurationUnit: wallet[:exp_duration_unit],
      expDurationLength: wallet[:exp_duration_length]
    }
  }

  request_options = build_request_options(options)

  response = KillBillClient::API.post path, body.to_json, {}, request_options
  JSON.parse(response.body)
rescue StandardError => e
  JSON.parse(e.message)
end

.modify_wallet(wallet_params, options = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/aviate/client.rb', line 41

def modify_wallet(wallet_params, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/wallet/#{wallet_params[:wallet_id]}/topOffConfig"
  body = {
    topOffType: wallet_params[:top_off_type],
    lowWatermark: wallet_params[:low_watermark],
    amount: wallet_params[:top_off_amount],
    expDurationUnit: wallet_params[:exp_duration_unit],
    expDurationLength: wallet_params[:exp_duration_length]
  }
  
  request_options = build_request_options(options)
  
  response = KillBillClient::API.put path, body.to_json, {}, request_options
  response.code
end

.retrieve_wallets(account_id, options = nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/aviate/client.rb', line 32

def retrieve_wallets(, options = nil)
  path = "#{KILLBILL_AVIATE_PREFIX}/wallet/#{}"
  
  request_options = build_request_options(options)
  
  response = KillBillClient::API.get path, {}, request_options
  JSON.parse(response.body)
end