megam_api 0.36 → 0.38
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/lib/megam/api.rb +22 -3
- data/lib/megam/api/accounts.rb +15 -1
- data/lib/megam/api/availableunits.rb +35 -0
- data/lib/megam/api/balances.rb +46 -0
- data/lib/megam/api/billinghistories.rb +35 -0
- data/lib/megam/api/billings.rb +35 -0
- data/lib/megam/api/components.rb +2 -2
- data/lib/megam/api/credithistories.rb +35 -0
- data/lib/megam/api/discounts.rb +35 -0
- data/lib/megam/api/profile.rb +42 -0
- data/lib/megam/api/subscriptions.rb +35 -0
- data/lib/megam/api/version.rb +1 -1
- data/lib/megam/core/account.rb +85 -8
- data/lib/megam/core/assembly.rb +5 -9
- data/lib/megam/core/availableunits.rb +181 -0
- data/lib/megam/core/availableunits_collection.rb +144 -0
- data/lib/megam/core/balances.rb +201 -0
- data/lib/megam/core/balances_collection.rb +144 -0
- data/lib/megam/core/billinghistories.rb +206 -0
- data/lib/megam/core/billinghistories_collection.rb +144 -0
- data/lib/megam/core/billings.rb +242 -0
- data/lib/megam/core/billings_collection.rb +144 -0
- data/lib/megam/core/credithistories.rb +191 -0
- data/lib/megam/core/credithistories_collection.rb +144 -0
- data/lib/megam/core/discounts.rb +191 -0
- data/lib/megam/core/discounts_collection.rb +144 -0
- data/lib/megam/core/json_compat.rb +57 -15
- data/lib/megam/core/subscriptions.rb +191 -0
- data/lib/megam/core/subscriptions_collection.rb +144 -0
- data/megam_api.gemspec +2 -2
- data/test/test_accounts.rb +19 -0
- data/test/test_availableunits.rb +29 -0
- data/test/test_balances.rb +39 -0
- data/test/test_billinghistories.rb +31 -0
- data/test/test_billings.rb +34 -0
- data/test/test_credithistories.rb +30 -0
- data/test/test_discounts.rb +30 -0
- data/test/test_helper.rb +2 -2
- data/test/test_subscriptions.rb +30 -0
- metadata +61 -7
@@ -0,0 +1,144 @@
|
|
1
|
+
##
|
2
|
+
## Copyright [2013-2015] [Megam Systems]
|
3
|
+
##
|
4
|
+
## Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
## you may not use this file except in compliance with the License.
|
6
|
+
## You may obtain a copy of the License at
|
7
|
+
##
|
8
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
##
|
10
|
+
## Unless required by applicable law or agreed to in writing, software
|
11
|
+
## distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
## See the License for the specific language governing permissions and
|
14
|
+
## limitations under the License.
|
15
|
+
##
|
16
|
+
module Megam
|
17
|
+
class BalancesCollection
|
18
|
+
include Enumerable
|
19
|
+
|
20
|
+
attr_reader :iterator
|
21
|
+
def initialize
|
22
|
+
@balance = Array.new
|
23
|
+
@balance_by_name = Hash.new
|
24
|
+
@insert_after_idx = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def all_balance
|
28
|
+
@balance
|
29
|
+
end
|
30
|
+
|
31
|
+
def [](index)
|
32
|
+
@balance[index]
|
33
|
+
end
|
34
|
+
|
35
|
+
def []=(index, arg)
|
36
|
+
is_megam_balance(arg)
|
37
|
+
@balance[index] = arg
|
38
|
+
@balance_by_name[arg.name] = index
|
39
|
+
end
|
40
|
+
|
41
|
+
def <<(*args)
|
42
|
+
args.flatten.each do |a|
|
43
|
+
is_megam_balance(a)
|
44
|
+
@balance << a
|
45
|
+
@balance_by_name[a.name] =@balance.length - 1
|
46
|
+
end
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
# 'push' is an alias method to <<
|
51
|
+
alias_method :push, :<<
|
52
|
+
|
53
|
+
def insert(balance)
|
54
|
+
is_megam_balance(balance)
|
55
|
+
if @insert_after_idx
|
56
|
+
# in the middle of executing a run, so any predefs inserted now should
|
57
|
+
# be placed after the most recent addition done by the currently executing
|
58
|
+
# balance
|
59
|
+
@balance.insert(@insert_after_idx + 1, balance)
|
60
|
+
# update name -> location mappings and register new balance
|
61
|
+
@balance_by_name.each_key do |key|
|
62
|
+
@balance_by_name[key] += 1 if@balance_by_name[key] > @insert_after_idx
|
63
|
+
end
|
64
|
+
@balance_by_name[balance.name] = @insert_after_idx + 1
|
65
|
+
@insert_after_idx += 1
|
66
|
+
else
|
67
|
+
@balance << balance
|
68
|
+
@balance_by_name[balance.name] =@balance.length - 1
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def each
|
73
|
+
@balance.each do |balance|
|
74
|
+
yield balance
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def each_index
|
79
|
+
@balance.each_index do |i|
|
80
|
+
yield i
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def empty?
|
85
|
+
@balance.empty?
|
86
|
+
end
|
87
|
+
|
88
|
+
def lookup(balance)
|
89
|
+
lookup_by = nil
|
90
|
+
if balance.kind_of?(Megam::Balances)
|
91
|
+
lookup_by = balance.name
|
92
|
+
elsif balance.kind_of?(String)
|
93
|
+
lookup_by = balance
|
94
|
+
else
|
95
|
+
raise ArgumentError, "Must pass a Megam::Balance or String to lookup"
|
96
|
+
end
|
97
|
+
res =@balance_by_name[lookup_by]
|
98
|
+
unless res
|
99
|
+
raise ArgumentError, "Cannot find a balance matching #{lookup_by} (did you define it first?)"
|
100
|
+
end
|
101
|
+
@balance[res]
|
102
|
+
end
|
103
|
+
|
104
|
+
# Transform the ruby obj -> to a Hash
|
105
|
+
def to_hash
|
106
|
+
index_hash = Hash.new
|
107
|
+
self.each do |balance|
|
108
|
+
index_hash[balance.name] = balance.to_s
|
109
|
+
end
|
110
|
+
index_hash
|
111
|
+
end
|
112
|
+
|
113
|
+
# Serialize this object as a hash: called from JsonCompat.
|
114
|
+
# Verify if this called from JsonCompat during testing.
|
115
|
+
def to_json(*a)
|
116
|
+
for_json.to_json(*a)
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.json_create(o)
|
120
|
+
collection = self.new()
|
121
|
+
o["results"].each do |balance_list|
|
122
|
+
balance_array = balance_list.kind_of?(Array) ? balance_list : [ balance_list ]
|
123
|
+
balance_array.each do |balance|
|
124
|
+
collection.insert(balance)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
collection
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
def is_megam_balance(arg)
|
133
|
+
unless arg.kind_of?(Megam::Balances)
|
134
|
+
raise ArgumentError, "Members must be Megam::balance's"
|
135
|
+
end
|
136
|
+
true
|
137
|
+
end
|
138
|
+
|
139
|
+
def to_s
|
140
|
+
Megam::Stuff.styled_hash(to_hash)
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
##
|
2
|
+
## Copyright [2013-2015] [Megam Systems]
|
3
|
+
##
|
4
|
+
## Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
## you may not use this file except in compliance with the License.
|
6
|
+
## You may obtain a copy of the License at
|
7
|
+
##
|
8
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
##
|
10
|
+
## Unless required by applicable law or agreed to in writing, software
|
11
|
+
## distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
## See the License for the specific language governing permissions and
|
14
|
+
## limitations under the License.
|
15
|
+
##
|
16
|
+
module Megam
|
17
|
+
class Billinghistories < Megam::ServerAPI
|
18
|
+
def initialize(email=nil, api_key=nil)
|
19
|
+
@id = nil
|
20
|
+
@accounts_id = nil
|
21
|
+
@assembly_id = nil
|
22
|
+
@bill_type = nil
|
23
|
+
@billing_amount = nil
|
24
|
+
@currency_type = nil
|
25
|
+
@created_at = nil
|
26
|
+
@some_msg = {}
|
27
|
+
super(email, api_key)
|
28
|
+
end
|
29
|
+
|
30
|
+
def billinghistories
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
def id(arg=nil)
|
36
|
+
if arg != nil
|
37
|
+
@id = arg
|
38
|
+
else
|
39
|
+
@id
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def accounts_id(arg=nil)
|
44
|
+
if arg != nil
|
45
|
+
@accounts_id= arg
|
46
|
+
else
|
47
|
+
@accounts_id
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def assembly_id(arg=nil)
|
52
|
+
if arg != nil
|
53
|
+
@assembly_id = arg
|
54
|
+
else
|
55
|
+
@assembly_id
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def bill_type(arg=nil)
|
60
|
+
if arg != nil
|
61
|
+
@bill_type = arg
|
62
|
+
else
|
63
|
+
@bill_type
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def billing_amount(arg=nil)
|
68
|
+
if arg != nil
|
69
|
+
@billing_amount= arg
|
70
|
+
else
|
71
|
+
@billing_amount
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def currency_type(arg=nil)
|
76
|
+
if arg != nil
|
77
|
+
@currency_type = arg
|
78
|
+
else
|
79
|
+
@currency_type
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def created_at(arg=nil)
|
84
|
+
if arg != nil
|
85
|
+
@created_at = arg
|
86
|
+
else
|
87
|
+
@created_at
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def some_msg(arg=nil)
|
92
|
+
if arg != nil
|
93
|
+
@some_msg = arg
|
94
|
+
else
|
95
|
+
@some_msg
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def error?
|
100
|
+
crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
|
101
|
+
end
|
102
|
+
|
103
|
+
# Transform the ruby obj -> to a Hash
|
104
|
+
def to_hash
|
105
|
+
index_hash = Hash.new
|
106
|
+
index_hash["json_claz"] = self.class.name
|
107
|
+
index_hash["id"] = id
|
108
|
+
index_hash["accounts_id"] = accounts_id
|
109
|
+
index_hash["assembly_id"] = assembly_id
|
110
|
+
index_hash["bill_type"] = bill_type
|
111
|
+
index_hash["billing_amount"] = billing_amount
|
112
|
+
index_hash["currency_type"] = currency_type
|
113
|
+
index_hash["created_at"] = created_at
|
114
|
+
index_hash
|
115
|
+
end
|
116
|
+
|
117
|
+
# Serialize this object as a hash: called from JsonCompat.
|
118
|
+
# Verify if this called from JsonCompat during testing.
|
119
|
+
def to_json(*a)
|
120
|
+
for_json.to_json(*a)
|
121
|
+
end
|
122
|
+
|
123
|
+
def for_json
|
124
|
+
result = {
|
125
|
+
"id" => id,
|
126
|
+
"accounts_id" => accounts_id,
|
127
|
+
"assembly_id" => assembly_id,
|
128
|
+
"bill_type" => bill_type,
|
129
|
+
"billing_amount" => billing_amount,
|
130
|
+
"currency_type" => currency_type,
|
131
|
+
"created_at" => created_at
|
132
|
+
}
|
133
|
+
result
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
def self.json_create(o)
|
138
|
+
bal = new
|
139
|
+
bal.id(o["id"]) if o.has_key?("id")
|
140
|
+
bal.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
|
141
|
+
bal.assembly_id(o["assembly_id"]) if o.has_key?("assembly_id")
|
142
|
+
bal.bill_type(o["bill_type"]) if o.has_key?("bill_type")
|
143
|
+
bal.billing_amount(o["billing_amount"]) if o.has_key?("billing_amount")
|
144
|
+
bal.currency_type(o["currency_type"]) if o.has_key?("currency_type")
|
145
|
+
bal.created_at(o["created_at"]) if o.has_key?("created_at")
|
146
|
+
#success or error
|
147
|
+
bal.some_msg[:code] = o["code"] if o.has_key?("code")
|
148
|
+
bal.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
|
149
|
+
bal.some_msg[:msg]= o["msg"] if o.has_key?("msg")
|
150
|
+
bal.some_msg[:links] = o["links"] if o.has_key?("links")
|
151
|
+
bal
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.from_hash(o,tmp_email=nil, tmp_api_key=nil)
|
155
|
+
bal = self.new(tmp_email, tmp_api_key)
|
156
|
+
bal.from_hash(o)
|
157
|
+
bal
|
158
|
+
end
|
159
|
+
|
160
|
+
def from_hash(o)
|
161
|
+
@id = o[:id] if o.has_key?(:id)
|
162
|
+
@accounts_id = o[:accounts_id] if o.has_key?(:accounts_id)
|
163
|
+
@assembly_id = o[:assembly_id] if o.has_key?(:assembly_id)
|
164
|
+
@bill_type = o[:bill_type] if o.has_key?(:bill_type)
|
165
|
+
@billing_amount = o[:billing_amount] if o.has_key?(:billing_amount)
|
166
|
+
@currency_type = o[:currency_type] if o.has_key?(:currency_type)
|
167
|
+
@created_at = o[:created_at] if o.has_key?(:created_at)
|
168
|
+
self
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.create(o,tmp_email=nil, tmp_api_key=nil)
|
172
|
+
acct = from_hash(o,tmp_email, tmp_api_key)
|
173
|
+
acct.create
|
174
|
+
end
|
175
|
+
|
176
|
+
# Create the billing histories via the REST API
|
177
|
+
def create
|
178
|
+
megam_rest.post_billinghistories(to_hash)
|
179
|
+
end
|
180
|
+
|
181
|
+
# Load all billing histories -
|
182
|
+
# returns a BillingHistoriesCollection
|
183
|
+
# don't return self. check if the Megam::BillingHistoriesCollection is returned.
|
184
|
+
def self.list(tmp_email=nil, tmp_api_key=nil)
|
185
|
+
billhistory = self.new(tmp_email,tmp_api_key)
|
186
|
+
billhistory.megam_rest.get_billinghistories
|
187
|
+
end
|
188
|
+
|
189
|
+
# Show a particular billing history by name,
|
190
|
+
# Megam::BillingHistory
|
191
|
+
def self.show(p_name,tmp_email=nil, tmp_api_key=nil)
|
192
|
+
pre = self.new(tmp_email,tmp_api_key)
|
193
|
+
pre.megam_rest.get_billinghistory(p_name)
|
194
|
+
end
|
195
|
+
|
196
|
+
def self.delete(p_name,tmp_email=nil, tmp_api_key=nil)
|
197
|
+
pre = self.new(tmp_email,tmp_api_key)
|
198
|
+
pre.megam_rest.delete_billinghistory(p_name)
|
199
|
+
end
|
200
|
+
|
201
|
+
def to_s
|
202
|
+
Megam::Stuff.styled_hash(to_hash)
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
206
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
##
|
2
|
+
## Copyright [2013-2015] [Megam Systems]
|
3
|
+
##
|
4
|
+
## Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
## you may not use this file except in compliance with the License.
|
6
|
+
## You may obtain a copy of the License at
|
7
|
+
##
|
8
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
##
|
10
|
+
## Unless required by applicable law or agreed to in writing, software
|
11
|
+
## distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
## See the License for the specific language governing permissions and
|
14
|
+
## limitations under the License.
|
15
|
+
##
|
16
|
+
module Megam
|
17
|
+
class BillinghistoriesCollection
|
18
|
+
include Enumerable
|
19
|
+
|
20
|
+
attr_reader :iterator
|
21
|
+
def initialize
|
22
|
+
@billinghistories = Array.new
|
23
|
+
@billinghistories_by_name = Hash.new
|
24
|
+
@insert_after_idx = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def all_billinghistories
|
28
|
+
@billinghistories
|
29
|
+
end
|
30
|
+
|
31
|
+
def [](index)
|
32
|
+
@billinghistories[index]
|
33
|
+
end
|
34
|
+
|
35
|
+
def []=(index, arg)
|
36
|
+
is_megam_billinghistories(arg)
|
37
|
+
@billinghistories[index] = arg
|
38
|
+
@billinghistories_by_name[arg.accounts_id] = index
|
39
|
+
end
|
40
|
+
|
41
|
+
def <<(*args)
|
42
|
+
args.flatten.each do |a|
|
43
|
+
is_megam_billinghistories(a)
|
44
|
+
@billinghistories << a
|
45
|
+
@billinghistories_by_name[a.accounts_id] =@billinghistories.length - 1
|
46
|
+
end
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
# 'push' is an alias method to <<
|
51
|
+
alias_method :push, :<<
|
52
|
+
|
53
|
+
def insert(billinghistories)
|
54
|
+
is_megam_billinghistories(billinghistories)
|
55
|
+
if @insert_after_idx
|
56
|
+
# in the middle of executing a run, so any predefs inserted now should
|
57
|
+
# be placed after the most recent addition done by the currently executing
|
58
|
+
# billinghistories
|
59
|
+
@billinghistories.insert(@insert_after_idx + 1, billinghistories)
|
60
|
+
# update name -> location mappings and register new billinghistories
|
61
|
+
@billinghistories_by_name.each_key do |key|
|
62
|
+
@billinghistories_by_name[key] += 1 if@billinghistories_by_name[key] > @insert_after_idx
|
63
|
+
end
|
64
|
+
@billinghistories_by_name[billinghistories.accounts_id] = @insert_after_idx + 1
|
65
|
+
@insert_after_idx += 1
|
66
|
+
else
|
67
|
+
@billinghistories << billinghistories
|
68
|
+
@billinghistories_by_name[billinghistories.accounts_id] =@billinghistories.length - 1
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def each
|
73
|
+
@billinghistories.each do |billinghistories|
|
74
|
+
yield billinghistories
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def each_index
|
79
|
+
@billinghistories.each_index do |i|
|
80
|
+
yield i
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def empty?
|
85
|
+
@billinghistories.empty?
|
86
|
+
end
|
87
|
+
|
88
|
+
def lookup(billinghistories)
|
89
|
+
lookup_by = nil
|
90
|
+
if billinghistories.kind_of?(Megam::Billinghistories)
|
91
|
+
lookup_by = billinghistories.accounts_id
|
92
|
+
elsif billinghistories.kind_of?(String)
|
93
|
+
lookup_by = billinghistories
|
94
|
+
else
|
95
|
+
raise ArgumentError, "Must pass a Megam::billinghistories or String to lookup"
|
96
|
+
end
|
97
|
+
res =@billinghistories_by_name[lookup_by]
|
98
|
+
unless res
|
99
|
+
raise ArgumentError, "Cannot find a billinghistories matching #{lookup_by} (did you define it first?)"
|
100
|
+
end
|
101
|
+
@billinghistories[res]
|
102
|
+
end
|
103
|
+
|
104
|
+
# Transform the ruby obj -> to a Hash
|
105
|
+
def to_hash
|
106
|
+
index_hash = Hash.new
|
107
|
+
self.each do |billinghistories|
|
108
|
+
index_hash[billinghistories.accounts_id] = billinghistories.to_s
|
109
|
+
end
|
110
|
+
index_hash
|
111
|
+
end
|
112
|
+
|
113
|
+
# Serialize this object as a hash: called from JsonCompat.
|
114
|
+
# Verify if this called from JsonCompat during testing.
|
115
|
+
def to_json(*a)
|
116
|
+
for_json.to_json(*a)
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.json_create(o)
|
120
|
+
collection = self.new()
|
121
|
+
o["results"].each do |billinghistories_list|
|
122
|
+
billinghistories_array = billinghistories_list.kind_of?(Array) ? billinghistories_list : [ billinghistories_list ]
|
123
|
+
billinghistories_array.each do |billinghistories|
|
124
|
+
collection.insert(billinghistories)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
collection
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
def is_megam_billinghistories(arg)
|
133
|
+
unless arg.kind_of?(Megam::Billinghistories)
|
134
|
+
raise ArgumentError, "Members must be Megam::billinghistories's"
|
135
|
+
end
|
136
|
+
true
|
137
|
+
end
|
138
|
+
|
139
|
+
def to_s
|
140
|
+
Megam::Stuff.styled_hash(to_hash)
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|