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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aviate/client.rb', line 73

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)

  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
# File 'lib/aviate/client.rb', line 9

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

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

.aviate_plugin_installed(options = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/aviate/client.rb', line 18

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aviate/client.rb', line 48

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]
    }
  }

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

.modify_wallet(wallet_params, options = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aviate/client.rb', line 35

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]
  }
  response = KillBillClient::API.put path, body.to_json, {}, get_token(options)
  response.code
end

.retrieve_wallets(account_id, options = nil) ⇒ Object



29
30
31
32
33
# File 'lib/aviate/client.rb', line 29

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