killbill-client 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fa054d6429ec8d29d211fe1d581ecd3e1cb8f90
4
- data.tar.gz: 9658639f68539f0f6d7ea5d77b96027e3485045d
3
+ metadata.gz: 7c3cd7bf2ed2d8a9477660ab9e345657fa331eb3
4
+ data.tar.gz: b249ca8abe133b75a6f38e23364cbef0a79e5629
5
5
  SHA512:
6
- metadata.gz: 058d93cbc47043cfbaa068f1c15e09ff1c48386768b2af5d85d8a9e903d914d8040feaefe4fbf1b510deeb63e000a2d638a42cab10085f513ada53d90b9f3ecb
7
- data.tar.gz: aad960ae930ae5057c3c4b3a1a9a0dd87737dafbd588e8572bd5beee1b2e70a1d14954f2f2a681f1d48583b3ecaeb1df50dfec882be72829af89745b757ba41b
6
+ metadata.gz: 58171e8f433fde25fd3d146586f4bcbe5f1f647ff7087162cdba2aa368c9b100c8797bc68b029f7b7f04636196548748a6b328783974b66c57f09c4a5a1ed646
7
+ data.tar.gz: 8df7abc43524e0b912cd2216ab1bdec510f609469ee08298169b235a57f46abc2a6532379b18ceeb1b7b9bce4a2f02e3378c9271984c7c39327ac93a70bd608d
@@ -41,10 +41,15 @@ module KillBillClient
41
41
 
42
42
  attr_writer :url
43
43
 
44
- # Tenant key/password. Optional.
44
+ # Tenant key/secret. Optional.
45
45
  attr_accessor :api_key
46
46
  attr_accessor :api_secret
47
47
 
48
+ # RBAC username/password. Optional.
49
+ # These can also be configured on a per request basis
50
+ attr_accessor :username
51
+ attr_accessor :password
52
+
48
53
  # @return [String, nil] A default currency.
49
54
  def default_currency
50
55
  return @default_currency if defined? @default_currency
@@ -47,11 +47,21 @@ module KillBillClient
47
47
  end
48
48
  request = METHODS[method].new uri.request_uri, head
49
49
 
50
- # Configure auth, if enabled
50
+ # Configure multi-tenancy headers, if enabled
51
51
  if KillBillClient.api_key and KillBillClient.api_secret
52
- request.basic_auth(*[KillBillClient.api_key, KillBillClient.api_secret].flatten[0, 2])
53
- request['X-Killbill-ApiKey'] = KillBillClient.api_key
54
- request['X-Killbill-ApiSecret'] = KillBillClient.api_secret
52
+ request['X-Killbill-ApiKey'] = options[:api_key] || KillBillClient.api_key
53
+ request['X-Killbill-ApiSecret'] = options[:api_secret] || KillBillClient.api_secret
54
+ end
55
+
56
+ # Configure RBAC, if enabled
57
+ username = options[:username] || KillBillClient.username
58
+ password = options[:password] || KillBillClient.password
59
+ if username and password
60
+ request.basic_auth(*[username, password].flatten[0, 2])
61
+ end
62
+ session_id = options[:session_id]
63
+ if session_id
64
+ request['Cookie'] = "JSESSIONID=#{session_id}"
55
65
  end
56
66
 
57
67
  if options[:body]
@@ -4,16 +4,17 @@ module KillBillClient
4
4
  KILLBILL_API_ACCOUNTS_PREFIX = "#{KILLBILL_API_PREFIX}/accounts"
5
5
 
6
6
  class << self
7
- def find_by_id(account_id, with_balance = false, with_balance_and_cba = false)
7
+ def find_by_id(account_id, with_balance = false, with_balance_and_cba = false, options = {})
8
8
  get "#{KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}",
9
9
  {
10
10
  :accountWithBalance => with_balance,
11
11
  :accountWithBalanceAndCBA => with_balance_and_cba
12
- }
12
+ },
13
+ options
13
14
  end
14
15
  end
15
16
 
16
- def create(user = nil, reason = nil, comment = nil)
17
+ def create(user = nil, reason = nil, comment = nil, options = {})
17
18
  created_account = self.class.post KILLBILL_API_ACCOUNTS_PREFIX,
18
19
  to_json,
19
20
  {},
@@ -21,27 +22,27 @@ module KillBillClient
21
22
  :user => user,
22
23
  :reason => reason,
23
24
  :comment => comment,
24
- }
25
+ }.merge(options)
25
26
  created_account.refresh
26
27
  end
27
28
 
28
- def payments
29
+ def payments(options = {})
29
30
  self.class.get "#{KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}/payments",
30
31
  {},
31
- {},
32
+ options,
32
33
  Payment
33
34
  end
34
35
 
35
- def tags(audit = 'NONE')
36
+ def tags(audit = 'NONE', options = {})
36
37
  self.class.get "#{KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}/tags",
37
38
  {
38
39
  :audit => audit
39
40
  },
40
- {},
41
+ options,
41
42
  Tag
42
43
  end
43
44
 
44
- def add_tag(tag_name, user = nil, reason = nil, comment = nil)
45
+ def add_tag(tag_name, user = nil, reason = nil, comment = nil, options = {})
45
46
  tag_definition = TagDefinition.find_by_name(tag_name)
46
47
  if tag_definition.nil?
47
48
  tag_definition = TagDefinition.new
@@ -59,12 +60,12 @@ module KillBillClient
59
60
  :user => user,
60
61
  :reason => reason,
61
62
  :comment => comment,
62
- },
63
+ }.merge(options),
63
64
  Tag
64
65
  created_tag.refresh
65
66
  end
66
67
 
67
- def remove_tag(tag_name, user = nil, reason = nil, comment = nil)
68
+ def remove_tag(tag_name, user = nil, reason = nil, comment = nil, options = {})
68
69
  tag_definition = TagDefinition.find_by_name(tag_name)
69
70
  return nil if tag_definition.nil?
70
71
 
@@ -76,7 +77,7 @@ module KillBillClient
76
77
  :user => user,
77
78
  :reason => reason,
78
79
  :comment => comment,
79
- }
80
+ }.merge(options)
80
81
  end
81
82
  end
82
83
  end
@@ -8,11 +8,12 @@ module KillBillClient
8
8
  has_many :invoices, KillBillClient::Model::Invoice
9
9
 
10
10
  class << self
11
- def find_by_account_id(account_id, audit = 'MINIMAL')
11
+ def find_by_account_id(account_id, audit = 'MINIMAL', options = {})
12
12
  get "#{Account::KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}/timeline",
13
13
  {
14
14
  :audit => audit
15
- }
15
+ },
16
+ options
16
17
  end
17
18
  end
18
19
  end
@@ -10,11 +10,12 @@ module KillBillClient
10
10
  create_alias :bundle_keys, :external_bundle_keys
11
11
 
12
12
  class << self
13
- def find_by_id_or_number(id_or_number, with_items = true)
13
+ def find_by_id_or_number(id_or_number, with_items = true, options = {})
14
14
  get "#{KILLBILL_API_INVOICES_PREFIX}/#{id_or_number}",
15
15
  {
16
16
  :withItems => with_items
17
- }
17
+ },
18
+ options
18
19
  end
19
20
  end
20
21
  end
@@ -1,7 +1,7 @@
1
1
  module KillBillClient
2
2
  module Model
3
3
  class InvoiceItem < InvoiceItemAttributesSimple
4
- def create(user = nil, reason = nil, comment = nil)
4
+ def create(user = nil, reason = nil, comment = nil, options = {})
5
5
  created_invoice_item = self.class.post "#{Invoice::KILLBILL_API_INVOICES_PREFIX}/charges",
6
6
  to_json,
7
7
  {},
@@ -9,7 +9,7 @@ module KillBillClient
9
9
  :user => user,
10
10
  :reason => reason,
11
11
  :comment => comment,
12
- }
12
+ }.merge(options)
13
13
  created_invoice_item.refresh(Invoice)
14
14
  end
15
15
  end
@@ -1,4 +1,5 @@
1
1
  require 'killbill_client/models/resource'
2
+ require 'killbill_client/models/resources'
2
3
 
3
4
  require 'killbill_client/models/gen/require_gen'
4
5
 
@@ -16,8 +17,10 @@ require 'killbill_client/models/account'
16
17
  require 'killbill_client/models/invoice'
17
18
  require 'killbill_client/models/payment'
18
19
  require 'killbill_client/models/payment_method'
20
+ require 'killbill_client/models/security'
19
21
  require 'killbill_client/models/tag'
20
22
  require 'killbill_client/models/tag_definition'
23
+ require 'killbill_client/models/tenant'
21
24
  require 'killbill_client/models/account_timeline'
22
25
 
23
26
  module KillBillClient
@@ -4,28 +4,31 @@ module KillBillClient
4
4
  KILLBILL_API_PAYMENT_METHODS_PREFIX = "#{KILLBILL_API_PREFIX}/paymentMethods"
5
5
 
6
6
  class << self
7
- def find_by_id(payment_method_id, with_plugin_info = false)
7
+ def find_by_id(payment_method_id, with_plugin_info = false, options = {})
8
8
  get "#{KILLBILL_API_PAYMENT_METHODS_PREFIX}/#{payment_method_id}",
9
9
  {
10
10
  :withPluginInfo => with_plugin_info
11
- }
11
+ },
12
+ options
12
13
  end
13
14
 
14
- def find_all_by_account_id(account_id, with_plugin_info = false)
15
+ def find_all_by_account_id(account_id, with_plugin_info = false, options = {})
15
16
  get "#{Account::KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}/paymentMethods",
16
17
  {
17
18
  :withPluginInfo => with_plugin_info
18
- }
19
+ },
20
+ options
19
21
  end
20
22
 
21
- def find_all_by_search_key(search_key, with_plugin_info = false)
23
+ def find_all_by_search_key(search_key, with_plugin_info = false, options)
22
24
  get "#{KILLBILL_API_PAYMENT_METHODS_PREFIX}/search/#{search_key}",
23
25
  {
24
26
  :withPluginInfo => with_plugin_info
25
- }
27
+ },
28
+ options
26
29
  end
27
30
 
28
- def set_default(payment_method_id, account_id, user = nil, reason = nil, comment = nil)
31
+ def set_default(payment_method_id, account_id, user = nil, reason = nil, comment = nil, options = {})
29
32
  put "#{Account::KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}/paymentMethods/#{payment_method_id}/setDefault",
30
33
  nil,
31
34
  {},
@@ -33,10 +36,10 @@ module KillBillClient
33
36
  :user => user,
34
37
  :reason => reason,
35
38
  :comment => comment,
36
- }
39
+ }.merge(options)
37
40
  end
38
41
 
39
- def destroy(payment_method_id, set_auto_pay_off = false, user = nil, reason = nil, comment = nil)
42
+ def destroy(payment_method_id, set_auto_pay_off = false, user = nil, reason = nil, comment = nil, options = {})
40
43
  delete "#{KILLBILL_API_PAYMENT_METHODS_PREFIX}/#{payment_method_id}",
41
44
  {
42
45
  :deleteDefaultPmWithAutoPayOff => set_auto_pay_off
@@ -45,11 +48,11 @@ module KillBillClient
45
48
  :user => user,
46
49
  :reason => reason,
47
50
  :comment => comment,
48
- }
51
+ }.merge(options)
49
52
  end
50
53
  end
51
54
 
52
- def create(set_default = true, user = nil, reason = nil, comment = nil)
55
+ def create(set_default = true, user = nil, reason = nil, comment = nil, options = {})
53
56
  created_pm = self.class.post "#{Account::KILLBILL_API_ACCOUNTS_PREFIX}/#{account_id}/paymentMethods",
54
57
  to_json,
55
58
  {
@@ -59,12 +62,12 @@ module KillBillClient
59
62
  :user => user,
60
63
  :reason => reason,
61
64
  :comment => comment,
62
- }
65
+ }.merge(options)
63
66
  created_pm.refresh
64
67
  end
65
68
 
66
- def destroy(set_auto_pay_off = false, user = nil, reason = nil, comment = nil)
67
- self.class.destroy(payment_method_id, set_auto_pay_off, user, reason, comment)
69
+ def destroy(set_auto_pay_off = false, user = nil, reason = nil, comment = nil, options = {})
70
+ self.class.destroy(payment_method_id, set_auto_pay_off, user, reason, comment, options)
68
71
  end
69
72
 
70
73
  def plugin_info=(info)
@@ -4,6 +4,10 @@ module KillBillClient
4
4
  module Model
5
5
  class Resource
6
6
 
7
+ attr_reader :etag,
8
+ :session_id,
9
+ :response
10
+
7
11
  KILLBILL_API_PREFIX = '/1.0/kb/'
8
12
  @@attribute_names = {}
9
13
 
@@ -45,7 +49,8 @@ module KillBillClient
45
49
  response.body
46
50
  when %r{application/json}
47
51
  record = from_json resource_class, response.body
48
- record.instance_eval { @etag, @response = response['ETag'], response }
52
+ session_id = extract_session_id(response)
53
+ record.instance_eval { @etag, @session_id, @response = response['ETag'], session_id, response }
49
54
  record
50
55
  else
51
56
  raise ArgumentError, "#{response['Content-Type']} is not supported by the library"
@@ -64,9 +69,14 @@ module KillBillClient
64
69
  data = JSON.parse json
65
70
 
66
71
  if data.is_a? Array
67
- records = []
72
+ records = Resources.new
68
73
  data.each do |data_element|
69
- records << instantiate_record_from_json(resource_class, data_element)
74
+ if data_element.is_a? Enumerable
75
+ records << instantiate_record_from_json(resource_class, data_element)
76
+ else
77
+ # Value (e.g. String)
78
+ records << data_element
79
+ end
70
80
  end
71
81
  records
72
82
  else
@@ -103,7 +113,7 @@ module KillBillClient
103
113
 
104
114
  record
105
115
  end
106
-
116
+
107
117
  def attribute(name)
108
118
  self.send('attr_accessor', name.to_sym)
109
119
 
@@ -130,7 +140,7 @@ module KillBillClient
130
140
 
131
141
  def has_one(attr_name, type = nil)
132
142
  send("attr_accessor", attr_name.to_sym)
133
-
143
+
134
144
  #add it to attribute_names
135
145
  @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
136
146
  @@attribute_names[self.name][attr_name.to_sym] = { :type => type, :cardinality => :one }
@@ -143,6 +153,20 @@ module KillBillClient
143
153
  alias_method "#{new_name}=".to_sym, "#{old_name}=".to_sym #setter
144
154
  end
145
155
 
156
+ # Extract the session id from a response
157
+ def extract_session_id(response)
158
+ # The Set-Cookie header looks like
159
+ # "set-cookie"=>["JSESSIONID=16; Path=/; HttpOnly", "rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Sat, 17-Aug-2013 23:39:37 GMT"],
160
+ session_cookie = response['set-cookie']
161
+ unless session_cookie.nil?
162
+ session_cookie.split(';').each do |chunk|
163
+ chunk.strip!
164
+ key, value = chunk.split('=')
165
+ return value if key == 'JSESSIONID'
166
+ end
167
+ end
168
+ nil
169
+ end
146
170
  end #end self methods
147
171
 
148
172
  # Set on create call
@@ -0,0 +1,11 @@
1
+ module KillBillClient
2
+ module Model
3
+ class Resources < ::Array
4
+
5
+ attr_reader :etag,
6
+ :session_id,
7
+ :response
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module KillBillClient
2
+ module Model
3
+ class Security < Resource
4
+ KILLBILL_API_SECURITY_PREFIX = "#{KILLBILL_API_PREFIX}/security"
5
+
6
+ class << self
7
+ def find_permissions(options = {})
8
+ get "#{KILLBILL_API_SECURITY_PREFIX}/permissions",
9
+ {},
10
+ options
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,16 +4,18 @@ module KillBillClient
4
4
  KILLBILL_API_TAG_DEFINITIONS_PREFIX = "#{KILLBILL_API_PREFIX}/tagDefinitions"
5
5
 
6
6
  class << self
7
- def all
8
- get KILLBILL_API_TAG_DEFINITIONS_PREFIX
7
+ def all(options = {})
8
+ get KILLBILL_API_TAG_DEFINITIONS_PREFIX,
9
+ {},
10
+ options
9
11
  end
10
12
 
11
- def find_by_name(name)
12
- self.all.select { |tag_definition| tag_definition.name == name }.first
13
+ def find_by_name(name, options = {})
14
+ self.all(options).select { |tag_definition| tag_definition.name == name }.first
13
15
  end
14
16
  end
15
17
 
16
- def create(user = nil, reason = nil, comment = nil)
18
+ def create(user = nil, reason = nil, comment = nil, options = {})
17
19
  created_tag_definition = self.class.post KILLBILL_API_TAG_DEFINITIONS_PREFIX,
18
20
  to_json,
19
21
  {},
@@ -21,7 +23,7 @@ module KillBillClient
21
23
  :user => user,
22
24
  :reason => reason,
23
25
  :comment => comment,
24
- }
26
+ }.merge(options)
25
27
  created_tag_definition.refresh
26
28
  end
27
29
  end
@@ -0,0 +1,33 @@
1
+ module KillBillClient
2
+ module Model
3
+ class Tenant < TenantAttributes
4
+ KILLBILL_API_TENANTS_PREFIX = "#{KILLBILL_API_PREFIX}/tenants"
5
+
6
+ class << self
7
+ def find_by_id(tenant_id, options = {})
8
+ get "#{KILLBILL_API_TENANTS_PREFIX}/#{tenant_id}",
9
+ {},
10
+ options
11
+ end
12
+
13
+ def find_by_api_key(api_key, options = {})
14
+ get "#{KILLBILL_API_TENANTS_PREFIX}/?apiKey=#{api_key}",
15
+ {},
16
+ options
17
+ end
18
+ end
19
+
20
+ def create(user = nil, reason = nil, comment = nil, options = {})
21
+ created_tenant = self.class.post KILLBILL_API_TENANTS_PREFIX,
22
+ to_json,
23
+ {},
24
+ {
25
+ :user => user,
26
+ :reason => reason,
27
+ :comment => comment,
28
+ }.merge(options)
29
+ created_tenant.refresh
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,8 +1,8 @@
1
1
  module KillBillClient
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
5
- PATCH = 3
4
+ MINOR = 2
5
+ PATCH = 0
6
6
  PRE = nil
7
7
 
8
8
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.').freeze
@@ -123,4 +123,32 @@ describe KillBillClient::Model do
123
123
  found_tag_definition.description.should == tag_definition.description
124
124
  found_tag_definition.is_control_tag.should be_false
125
125
  end
126
+
127
+ it 'should manipulate tenants' do
128
+ api_key = Time.now.to_i.to_s
129
+ api_secret = 'S4cr3333333t!!!!!!lolz'
130
+
131
+ tenant = KillBillClient::Model::Tenant.new
132
+ tenant.api_key = api_key
133
+ tenant.api_secret = api_secret
134
+
135
+ # Create and verify the tenant
136
+ tenant = tenant.create('KillBill Spec test')
137
+ tenant.api_key.should == api_key
138
+ tenant.tenant_id.should_not be_nil
139
+
140
+ # Try to retrieve it by id
141
+ tenant = KillBillClient::Model::Tenant.find_by_id tenant.tenant_id
142
+ tenant.api_key.should == api_key
143
+
144
+ # Try to retrieve it by api key
145
+ tenant = KillBillClient::Model::Tenant.find_by_api_key tenant.api_key
146
+ tenant.api_key.should == api_key
147
+ end
148
+
149
+ #it 'should retrieve users permissions' do
150
+ # # Tough to verify as it depends on the Kill Bill configuration
151
+ # puts KillBillClient::Model::Security.find_permissions
152
+ # puts KillBillClient::Model::Security.find_permissions(:username => 'admin', :password => 'password')
153
+ #end
126
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: killbill-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Killbill core team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-13 00:00:00.000000000 Z
11
+ date: 2013-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -117,11 +117,14 @@ files:
117
117
  - lib/killbill_client/models/payment_method.rb
118
118
  - lib/killbill_client/models/refund.rb
119
119
  - lib/killbill_client/models/resource.rb
120
+ - lib/killbill_client/models/resources.rb
121
+ - lib/killbill_client/models/security.rb
120
122
  - lib/killbill_client/models/subscription_deleted_event.rb
121
123
  - lib/killbill_client/models/subscription_event.rb
122
124
  - lib/killbill_client/models/subscription_new_event.rb
123
125
  - lib/killbill_client/models/tag.rb
124
126
  - lib/killbill_client/models/tag_definition.rb
127
+ - lib/killbill_client/models/tenant.rb
125
128
  - lib/killbill_client/utils.rb
126
129
  - lib/killbill_client/version.rb
127
130
  - lib/rails/generators/killbill_client/config_generator.rb