megam_api 0.36 → 0.38

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -3
  3. data/lib/megam/api.rb +22 -3
  4. data/lib/megam/api/accounts.rb +15 -1
  5. data/lib/megam/api/availableunits.rb +35 -0
  6. data/lib/megam/api/balances.rb +46 -0
  7. data/lib/megam/api/billinghistories.rb +35 -0
  8. data/lib/megam/api/billings.rb +35 -0
  9. data/lib/megam/api/components.rb +2 -2
  10. data/lib/megam/api/credithistories.rb +35 -0
  11. data/lib/megam/api/discounts.rb +35 -0
  12. data/lib/megam/api/profile.rb +42 -0
  13. data/lib/megam/api/subscriptions.rb +35 -0
  14. data/lib/megam/api/version.rb +1 -1
  15. data/lib/megam/core/account.rb +85 -8
  16. data/lib/megam/core/assembly.rb +5 -9
  17. data/lib/megam/core/availableunits.rb +181 -0
  18. data/lib/megam/core/availableunits_collection.rb +144 -0
  19. data/lib/megam/core/balances.rb +201 -0
  20. data/lib/megam/core/balances_collection.rb +144 -0
  21. data/lib/megam/core/billinghistories.rb +206 -0
  22. data/lib/megam/core/billinghistories_collection.rb +144 -0
  23. data/lib/megam/core/billings.rb +242 -0
  24. data/lib/megam/core/billings_collection.rb +144 -0
  25. data/lib/megam/core/credithistories.rb +191 -0
  26. data/lib/megam/core/credithistories_collection.rb +144 -0
  27. data/lib/megam/core/discounts.rb +191 -0
  28. data/lib/megam/core/discounts_collection.rb +144 -0
  29. data/lib/megam/core/json_compat.rb +57 -15
  30. data/lib/megam/core/subscriptions.rb +191 -0
  31. data/lib/megam/core/subscriptions_collection.rb +144 -0
  32. data/megam_api.gemspec +2 -2
  33. data/test/test_accounts.rb +19 -0
  34. data/test/test_availableunits.rb +29 -0
  35. data/test/test_balances.rb +39 -0
  36. data/test/test_billinghistories.rb +31 -0
  37. data/test/test_billings.rb +34 -0
  38. data/test/test_credithistories.rb +30 -0
  39. data/test/test_discounts.rb +30 -0
  40. data/test/test_helper.rb +2 -2
  41. data/test/test_subscriptions.rb +30 -0
  42. metadata +61 -7
@@ -0,0 +1,242 @@
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 Billings < Megam::ServerAPI
18
+
19
+ def initialize(email=nil, api_key=nil)
20
+ @id = nil
21
+ @accounts_id = nil
22
+ @line1 = nil
23
+ @line2 = nil
24
+ @country_code = nil
25
+ @postal_code=nil
26
+ @state=nil
27
+ @phone = nil
28
+ @bill_type = nil
29
+ @created_at = nil
30
+ @some_msg = {}
31
+ super(email, api_key)
32
+ end
33
+
34
+ def billings
35
+ self
36
+ end
37
+
38
+
39
+ def id(arg=nil)
40
+ if arg != nil
41
+ @id = arg
42
+ else
43
+ @id
44
+ end
45
+ end
46
+
47
+ def accounts_id(arg=nil)
48
+ if arg != nil
49
+ @accounts_id= arg
50
+ else
51
+ @accounts_id
52
+ end
53
+ end
54
+
55
+ def line1(arg=nil)
56
+ if arg != nil
57
+ @line1 = arg
58
+ else
59
+ @line1
60
+ end
61
+ end
62
+
63
+ def line2(arg=nil)
64
+ if arg != nil
65
+ @line2 = arg
66
+ else
67
+ @line2
68
+ end
69
+ end
70
+
71
+ def country_code(arg=nil)
72
+ if arg != nil
73
+ @country_code = arg
74
+ else
75
+ @country_code
76
+ end
77
+ end
78
+
79
+ def postal_code(arg=nil)
80
+ if arg != nil
81
+ @postal_code= arg
82
+ else
83
+ @postal_code
84
+ end
85
+ end
86
+
87
+ def state(arg=nil)
88
+ if arg != nil
89
+ @state= arg
90
+ else
91
+ @state
92
+ end
93
+ end
94
+
95
+ def phone(arg=nil)
96
+ if arg != nil
97
+ @phone= arg
98
+ else
99
+ @phone
100
+ end
101
+ end
102
+
103
+ def bill_type(arg=nil)
104
+ if arg != nil
105
+ @bill_type = arg
106
+ else
107
+ @bill_type
108
+ end
109
+ end
110
+
111
+ def created_at(arg=nil)
112
+ if arg != nil
113
+ @created_at = arg
114
+ else
115
+ @created_at
116
+ end
117
+ end
118
+
119
+ def some_msg(arg=nil)
120
+ if arg != nil
121
+ @some_msg = arg
122
+ else
123
+ @some_msg
124
+ end
125
+ end
126
+
127
+ def error?
128
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
129
+ end
130
+
131
+ # Transform the ruby obj -> to a Hash
132
+ def to_hash
133
+ index_hash = Hash.new
134
+ index_hash["json_claz"] = self.class.name
135
+ index_hash["id"] = id
136
+ index_hash["accounts_id"] = accounts_id
137
+ index_hash["line1"] = line1
138
+ index_hash["line2"] = line2
139
+ index_hash["country_code"] = country_code
140
+ index_hash["postal_code"] = postal_code
141
+ index_hash["state"] = state
142
+ index_hash["phone"] = phone
143
+ index_hash["bill_type"] = bill_type
144
+ index_hash["created_at"] = created_at
145
+ index_hash["some_msg"] = some_msg
146
+ index_hash
147
+ end
148
+
149
+ # Serialize this object as a hash: called from JsonCompat.
150
+ # Verify if this called from JsonCompat during testing.
151
+ def to_json(*a)
152
+ for_json.to_json(*a)
153
+ end
154
+
155
+ def for_json
156
+ result = {
157
+ "id" => id,
158
+ "accounts_id" => accounts_id,
159
+ "line1" => line1,
160
+ "line2" => line2,
161
+ "country_code" => country_code,
162
+ "postal_code" => postal_code,
163
+ "state" => state,
164
+ "phone" => phone,
165
+ "bill_type" => bill_type,
166
+ "created_at" => created_at
167
+ }
168
+ result
169
+ end
170
+
171
+ #
172
+ def self.json_create(o)
173
+ cts = new
174
+ cts.id(o["id"]) if o.has_key?("id")
175
+ cts.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
176
+ cts.line1(o["line1"]) if o.has_key?("line1")
177
+ cts.line2(o["line2"]) if o.has_key?("line2")
178
+ cts.country_code(o["country_code"]) if o.has_key?("country_code")
179
+ cts.postal_code(o["postal_code"]) if o.has_key?("postal_code")
180
+ cts.state(o["state"]) if o.has_key?("state")
181
+ cts.phone(o["phone"]) if o.has_key?("phone")
182
+ cts.bill_type(o["bill_type"]) if o.has_key?("bill_type")
183
+
184
+ cts.created_at(o["created_at"]) if o.has_key?("created_at")
185
+ #success or error
186
+ cts.some_msg[:code] = o["code"] if o.has_key?("code")
187
+ cts.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
188
+ cts.some_msg[:msg]= o["msg"] if o.has_key?("msg")
189
+ cts.some_msg[:links] = o["links"] if o.has_key?("links")
190
+ cts
191
+ end
192
+
193
+ def self.from_hash(o,tmp_email=nil, tmp_api_key=nil)
194
+ cts = self.new(tmp_email,tmp_api_key)
195
+ cts.from_hash(o)
196
+ cts
197
+ end
198
+
199
+ def from_hash(o)
200
+ @id = o[:id] if o.has_key?(:id)
201
+ @accounts_id = o[:accounts_id] if o.has_key?(:accounts_id)
202
+ @line1 = o[:line1] if o.has_key?(:line1)
203
+ @line2 = o[:line2] if o.has_key?(:line2)
204
+ @country_code = o[:country_code] if o.has_key?(:country_code)
205
+ @postal_code = o[:postal_code] if o.has_key?(:postal_code)
206
+ @state = o[:state] if o.has_key?(:state)
207
+ @bill_type = o[:bill_type] if o.has_key?(:bill_type)
208
+ @created_at = o[:created_at] if o.has_key?(:created_at)
209
+ self
210
+ end
211
+
212
+ def self.create(o,tmp_email=nil, tmp_api_key=nil)
213
+ acct = from_hash(o,tmp_email, tmp_api_key)
214
+ acct.create
215
+ end
216
+
217
+ # Create the billings via the REST API
218
+ def create
219
+ megam_rest.post_billings(to_hash)
220
+ end
221
+
222
+ # Load all billings -
223
+ # returns a billingsCollection
224
+ # don't return self. check if the Megam::billingsCollection is returned.
225
+ def self.list(tmp_email=nil, tmp_api_key=nil)
226
+ cts = self.new(tmp_email, tmp_api_key)
227
+ cts.megam_rest.get_billings
228
+ end
229
+
230
+ # Show a particular billings by name,
231
+ # Megam::billings
232
+ def self.show(p_name,tmp_email=nil, tmp_api_key=nil)
233
+ pre = self.new(tmp_email, tmp_api_key)
234
+ pre.megam_rest.get_billing(p_name)
235
+ end
236
+
237
+ def to_s
238
+ Megam::Stuff.styled_hash(to_hash)
239
+ end
240
+
241
+ end
242
+ 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 BillingsCollection
18
+ include Enumerable
19
+
20
+ attr_reader :iterator
21
+ def initialize
22
+ @billings = Array.new
23
+ @billings_by_name = Hash.new
24
+ @insert_after_idx = nil
25
+ end
26
+
27
+ def all_billings
28
+ @billings
29
+ end
30
+
31
+ def [](index)
32
+ @billings[index]
33
+ end
34
+
35
+ def []=(index, arg)
36
+ is_megam_billings(arg)
37
+ @billings[index] = arg
38
+ @billings_by_name[arg.repo_name] = index
39
+ end
40
+
41
+ def <<(*args)
42
+ args.flatten.each do |a|
43
+ is_megam_billings(a)
44
+ @billings << a
45
+ @billings_by_name[a.repo_name] =@billings.length - 1
46
+ end
47
+ self
48
+ end
49
+
50
+ # 'push' is an alias method to <<
51
+ alias_method :push, :<<
52
+
53
+ def insert(billings)
54
+ is_megam_billings(billings)
55
+ if @insert_after_idx
56
+ # in the middle of executing a run, so any billings inserted now should
57
+ # be placed after the most recent addition done by the currently executing
58
+ # billings
59
+ @billings.insert(@insert_after_idx + 1, billings)
60
+ # update name -> location mappings and register new billings
61
+ @billings_by_name.each_key do |key|
62
+ @billings_by_name[key] += 1 if@billings_by_name[key] > @insert_after_idx
63
+ end
64
+ @billings_by_name[billings.repo_name] = @insert_after_idx + 1
65
+ @insert_after_idx += 1
66
+ else
67
+ @billings << billings
68
+ @billings_by_name[billings.repo_name] =@billings.length - 1
69
+ end
70
+ end
71
+
72
+ def each
73
+ @billings.each do |billings|
74
+ yield billings
75
+ end
76
+ end
77
+
78
+ def each_index
79
+ @billings.each_index do |i|
80
+ yield i
81
+ end
82
+ end
83
+
84
+ def empty?
85
+ @billings.empty?
86
+ end
87
+
88
+ def lookup(billings)
89
+ lookup_by = nil
90
+ if billings.kind_of?(Megam::Billings)
91
+ lookup_by = billings.repo_name
92
+ elsif billings.kind_of?(String)
93
+ lookup_by = billings
94
+ else
95
+ raise ArgumentError, "Must pass a Megam::billings or String to lookup"
96
+ end
97
+ res =@billings_by_name[lookup_by]
98
+ unless res
99
+ raise ArgumentError, "Cannot find a billings matching #{lookup_by} (did you define it first?)"
100
+ end
101
+ @billings[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 |billings|
108
+ index_hash[billings.repo_name] = billings.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 |billings_list|
122
+ billings_array = billings_list.kind_of?(Array) ? billings_list : [ billings_list ]
123
+ billings_array.each do |billings|
124
+ collection.insert(billings)
125
+ end
126
+ end
127
+ collection
128
+ end
129
+
130
+ private
131
+
132
+ def is_megam_billings(arg)
133
+ unless arg.kind_of?(Megam::Billings)
134
+ raise ArgumentError, "Members must be Megam::Billings"
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,191 @@
1
+ # Copyright:: Copyright (c) 2012, 2014 Megam Systems
2
+ # License:: Apache License, Version 2.0
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 Credithistories < Megam::ServerAPI
18
+
19
+ def initialize(email=nil, api_key=nil)
20
+ @id = nil
21
+ @accounts_id = nil
22
+ @bill_type = nil
23
+ @credit_amount = nil
24
+ @currency_type = nil
25
+ @created_at = nil
26
+ @some_msg = {}
27
+ super(email, api_key)
28
+ end
29
+
30
+ def credithistories
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 bill_type(arg=nil)
52
+ if arg != nil
53
+ @bill_type = arg
54
+ else
55
+ @bill_type
56
+ end
57
+ end
58
+
59
+ def credit_amount(arg=nil)
60
+ if arg != nil
61
+ @credit_amount = arg
62
+ else
63
+ @credit_amount
64
+ end
65
+ end
66
+
67
+ def currency_type(arg=nil)
68
+ if arg != nil
69
+ @currency_type = arg
70
+ else
71
+ @currency_type
72
+ end
73
+ end
74
+
75
+ def created_at(arg=nil)
76
+ if arg != nil
77
+ @created_at = arg
78
+ else
79
+ @created_at
80
+ end
81
+ end
82
+
83
+ def some_msg(arg=nil)
84
+ if arg != nil
85
+ @some_msg = arg
86
+ else
87
+ @some_msg
88
+ end
89
+ end
90
+
91
+ def error?
92
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
93
+ end
94
+
95
+ # Transform the ruby obj -> to a Hash
96
+ def to_hash
97
+ index_hash = Hash.new
98
+ index_hash["json_claz"] = self.class.name
99
+ index_hash["id"] = id
100
+ index_hash["accounts_id"] = accounts_id
101
+ index_hash["bill_type"] = bill_type
102
+ index_hash["credit_amount"] = credit_amount
103
+ index_hash["currency_type"] = currency_type
104
+ index_hash["created_at"] = created_at
105
+ index_hash["some_msg"] = some_msg
106
+ index_hash
107
+ end
108
+
109
+ # Serialize this object as a hash: called from JsonCompat.
110
+ # Verify if this called from JsonCompat during testing.
111
+ def to_json(*a)
112
+ for_json.to_json(*a)
113
+ end
114
+
115
+ def for_json
116
+ result = {
117
+ "id" => id,
118
+ "accounts_id" => accounts_id,
119
+ "bill_type" => bill_type,
120
+ "credit_amount" => credit_amount,
121
+ "currency_type" => currency_type,
122
+ "created_at" => created_at
123
+ }
124
+ result
125
+ end
126
+
127
+ #
128
+ def self.json_create(o)
129
+ cts = new
130
+ cts.id(o["id"]) if o.has_key?("id")
131
+ cts.accounts_id(o["accounts_id"]) if o.has_key?("accounts_id")
132
+ cts.bill_type(o["bill_type"]) if o.has_key?("bill_type")
133
+ cts.credit_amount(o["credit_amount"]) if o.has_key?("credit_amount")
134
+ cts.currency_type(o["currency_type"]) if o.has_key?("currency_type")
135
+
136
+ cts.created_at(o["created_at"]) if o.has_key?("created_at")
137
+ #success or error
138
+ cts.some_msg[:code] = o["code"] if o.has_key?("code")
139
+ cts.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
140
+ cts.some_msg[:msg]= o["msg"] if o.has_key?("msg")
141
+ cts.some_msg[:links] = o["links"] if o.has_key?("links")
142
+ cts
143
+ end
144
+
145
+ def self.from_hash(o,tmp_email=nil, tmp_api_key=nil)
146
+ cts = self.new(tmp_email,tmp_api_key)
147
+ cts.from_hash(o)
148
+ cts
149
+ end
150
+
151
+ def from_hash(o)
152
+ @id = o[:id] if o.has_key?(:id)
153
+ @accounts_id = o[:accounts_id] if o.has_key?(:accounts_id)
154
+ @bill_type = o[:bill_type] if o.has_key?(:bill_type)
155
+ @credit_amount = o[:credit_amount] if o.has_key?(:credit_amount)
156
+ @currency_type = o[:currency_type] if o.has_key?(:currency_type)
157
+ @created_at = o[:created_at] if o.has_key?(:created_at)
158
+ self
159
+ end
160
+
161
+ def self.create(o,tmp_email=nil, tmp_api_key=nil)
162
+ acct = from_hash(o,tmp_email, tmp_api_key)
163
+ acct.create
164
+ end
165
+
166
+ # Create the credit histories via the REST API
167
+ def create
168
+ megam_rest.post_credithistories(to_hash)
169
+ end
170
+
171
+ # Load all credithistories -
172
+ # returns a credithistoriesCollection
173
+ # don't return self. check if the Megam::ccredithistoriesCollection is returned.
174
+ def self.list(tmp_email=nil, tmp_api_key=nil)
175
+ cts = self.new(tmp_email, tmp_api_key)
176
+ cts.megam_rest.get_credithistories
177
+ end
178
+
179
+ # Show a particular credithistories by name,
180
+ # Megam::credithistories
181
+ def self.show(p_name,tmp_email=nil, tmp_api_key=nil)
182
+ pre = self.new(tmp_email, tmp_api_key)
183
+ pre.megam_rest.get_credithistories(p_name)
184
+ end
185
+
186
+ def to_s
187
+ Megam::Stuff.styled_hash(to_hash)
188
+ end
189
+
190
+ end
191
+ end