megam_api 1.5.rc7 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ module Megam
2
+ class Addons < Megam::RestAdapter
3
+ def initialize(o)
4
+ @id = nil
5
+ @provider_id = nil
6
+ @account_id = nil
7
+ @provider_name = nil
8
+ @options = []
9
+ @created_at = nil
10
+ @some_msg = {}
11
+ super(o)
12
+ end
13
+
14
+ def addons
15
+ self
16
+ end
17
+
18
+ def id(arg=nil)
19
+ if arg != nil
20
+ @id = arg
21
+ else
22
+ @id
23
+ end
24
+ end
25
+
26
+ def provider_id(arg=nil)
27
+ if arg != nil
28
+ @provider_id= arg
29
+ else
30
+ @provider_id
31
+ end
32
+ end
33
+
34
+ def account_id(arg=nil)
35
+ if arg != nil
36
+ @account_id= arg
37
+ else
38
+ @account_id
39
+ end
40
+ end
41
+
42
+ def provider_name(arg=nil)
43
+ if arg != nil
44
+ @provider_name = arg
45
+ else
46
+ @provider_name
47
+ end
48
+ end
49
+
50
+ def options(arg=[])
51
+ if arg != []
52
+ @options = arg
53
+ else
54
+ @options
55
+ end
56
+ end
57
+
58
+ def created_at(arg=nil)
59
+ if arg != nil
60
+ @created_at = arg
61
+ else
62
+ @created_at
63
+ end
64
+ end
65
+
66
+ def some_msg(arg=nil)
67
+ if arg != nil
68
+ @some_msg = arg
69
+ else
70
+ @some_msg
71
+ end
72
+ end
73
+
74
+ def error?
75
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
76
+ end
77
+
78
+ # Transform the ruby obj -> to a Hash
79
+ def to_hash
80
+ index_hash = Hash.new
81
+ index_hash["json_claz"] = self.class.name
82
+ index_hash["id"] = id
83
+ index_hash["provider_id"] = provider_id
84
+ index_hash["account_id"] = account_id
85
+ index_hash["provider_name"] = provider_name
86
+ index_hash["options"] = options
87
+ index_hash["created_at"] = created_at
88
+ index_hash
89
+ end
90
+
91
+ # Serialize this object as a hash: called from JsonCompat.
92
+ # Verify if this called from JsonCompat during testing.
93
+ def to_json(*a)
94
+ for_json.to_json(*a)
95
+ end
96
+
97
+ def for_json
98
+ result = {
99
+ "id" => id,
100
+ "provider_id" => provider_id,
101
+ "account_id" => account_id,
102
+ "provider_name" => provider_name ,
103
+ "options" => options,
104
+ "created_at" => created_at
105
+ }
106
+ result
107
+ end
108
+
109
+ #
110
+ def self.json_create(o)
111
+ adn = new({})
112
+ adn.id(o["id"]) if o.has_key?("id")
113
+ adn.provider_id(o["provider_id"]) if o.has_key?("provider_id")
114
+ adn.account_id(o["account_id"]) if o.has_key?("account_id")
115
+ adn.provider_name(o["provider_name"]) if o.has_key?("provider_name")
116
+ adn.options(o["options"]) if o.has_key?("options")
117
+ adn.created_at(o["created_at"]) if o.has_key?("created_at")
118
+ #success or error
119
+ adn.some_msg[:code] = o["code"] if o.has_key?("code")
120
+ adn.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
121
+ adn.some_msg[:msg]= o["msg"] if o.has_key?("msg")
122
+ adn.some_msg[:links] = o["links"] if o.has_key?("links")
123
+ adn
124
+ end
125
+
126
+ def self.from_hash(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
127
+ adn = self.new(tmp_email, tmp_api_key, tmp_host)
128
+ adn.from_hash(o)
129
+ adn
130
+ end
131
+
132
+ def from_hash(o)
133
+ @id = o[:id] if o.has_key?(:id)
134
+ @provider_id = o[:provider_id] if o.has_key?(:provider_id)
135
+ @account_id = o[:account_id] if o.has_key?(:account_id)
136
+ @provider_name = o[:provider_name] if o.has_key?(:provider_name)
137
+ @options = o[:options] if o.has_key?(:options)
138
+ @created_at = o[:created_at] if o.has_key?(:created_at)
139
+ self
140
+ end
141
+
142
+ def self.create(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
143
+ acct = from_hash(o,tmp_email, tmp_api_key, tmp_host)
144
+ acct.create
145
+ end
146
+
147
+ # Create the addons via the REST API
148
+ def create
149
+ megam_rest.post_addons(to_hash)
150
+ end
151
+
152
+ # Load all addons -
153
+ # returns a AddonsCollection
154
+ # don't return self. check if the Megam::AddonsCollection is returned.
155
+
156
+ # Show a particular addon by provider_name,
157
+ # Megam::Addon
158
+ def self.show(p_name,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
159
+ pre = self.new(tmp_email,tmp_api_key, tmp_host)
160
+ pre.megam_rest.get_addon(p_name)
161
+ end
162
+
163
+
164
+ def to_s
165
+ Megam::Stuff.styled_hash(to_hash)
166
+ end
167
+
168
+ end
169
+ end
@@ -0,0 +1,121 @@
1
+ module Megam
2
+ class AddonsCollection
3
+ include Enumerable
4
+
5
+ attr_reader :iterator
6
+ def initialize
7
+ @addons = Array.new
8
+ @addons_by_name = Hash.new
9
+ @insert_after_idx = nil
10
+ end
11
+
12
+ def all_addons
13
+ @addons
14
+ end
15
+
16
+ def [](index)
17
+ @addons[index]
18
+ end
19
+
20
+ def []=(index, arg)
21
+ is_megam_addons(arg)
22
+ @addons[index] = arg
23
+ @addons_by_name[arg.account_id] = index
24
+ end
25
+
26
+ def <<(*args)
27
+ args.flatten.each do |a|
28
+ is_megam_addons(a)
29
+ @addons << a
30
+ @addons_by_name[a.account_id] =@addons.length - 1
31
+ end
32
+ self
33
+ end
34
+
35
+ # 'push' is an alias method to <<
36
+ alias_method :push, :<<
37
+
38
+ def insert(addons)
39
+ is_megam_addons(addons)
40
+ if @insert_after_idx
41
+ # in the middle of executing a run, so any predefs inserted now should
42
+ # be placed after the most recent addition done by the currently executing
43
+ # addons
44
+ @addons.insert(@insert_after_idx + 1, addons)
45
+ # update name -> location mappings and register new addons
46
+ @addons_by_name.each_key do |key|
47
+ @addons_by_name[key] += 1 if@addons_by_name[key] > @insert_after_idx
48
+ end
49
+ @addons_by_name[addons.account_id] = @insert_after_idx + 1
50
+ @insert_after_idx += 1
51
+ else
52
+ @addons << addons
53
+ @addons_by_name[addons.account_id] =@addons.length - 1
54
+ end
55
+ end
56
+
57
+ def each
58
+ @addons.each do |addons|
59
+ yield addons
60
+ end
61
+ end
62
+
63
+ def each_index
64
+ @addons.each_index do |i|
65
+ yield i
66
+ end
67
+ end
68
+
69
+ def empty?
70
+ @addons.empty?
71
+ end
72
+
73
+ def lookup(addons)
74
+ lookup_by = nil
75
+ if Addons.kind_of?(Megam::Addons)
76
+ lookup_by = addons.account_id
77
+ elsif addons.kind_of?(String)
78
+ lookup_by = addons
79
+ else
80
+ raise ArgumentError, "Must pass a Megam::Addons or String to lookup"
81
+ end
82
+ res =@addons_by_name[lookup_by]
83
+ unless res
84
+ raise ArgumentError, "Cannot find a addons matching #{lookup_by} (did you define it first?)"
85
+ end
86
+ @addons[res]
87
+ end
88
+
89
+ def to_s
90
+ @addons.join(", ")
91
+ end
92
+
93
+ def for_json
94
+ to_a.map { |item| item.to_s }
95
+ end
96
+
97
+ def to_json(*a)
98
+ Megam::JSONCompat.to_json(for_json, *a)
99
+ end
100
+
101
+ def self.json_create(o)
102
+ collection = self.new()
103
+ o["results"].each do |addons_list|
104
+ addons_array = addons_list.kind_of?(Array) ? addons_list : [ addons_list ]
105
+ addons_array.each do |addons|
106
+ collection.insert(addons)
107
+ end
108
+ end
109
+ collection
110
+ end
111
+
112
+ private
113
+
114
+ def is_megam_addons(arg)
115
+ unless arg.kind_of?(Megam::Addons)
116
+ raise ArgumentError, "Members must be Megam::addons's"
117
+ end
118
+ true
119
+ end
120
+ end
121
+ end
@@ -48,6 +48,10 @@ module Megam
48
48
  MEGAM_EVENTSBILLINGCOLLECTION = 'Megam::EventsBillingCollection'.freeze
49
49
  MEGAM_EVENTSSTORAGE = 'Megam::EventsStorage'.freeze
50
50
  MEGAM_EVENTSSTORAGECOLLECTION = 'Megam::EventsStorageCollection'.freeze
51
+ MEGAM_SUBSCRIPTIONS = 'Megam::Subscriptions'.freeze
52
+ MEGAM_SUBSCRIPTIONSCOLLECTION = 'Megam::SubscriptionsCollection'.freeze
53
+ MEGAM_ADDONS = 'Megam::Addons'.freeze
54
+ MEGAM_ADDONSCOLLECTION = 'Megam::AddonsCollection'.freeze
51
55
  MEGAM_PROMOS = 'Megam::Promos'.freeze
52
56
 
53
57
  class <<self
@@ -203,7 +207,15 @@ module Megam
203
207
  Megam::Billingtransactions
204
208
  when MEGAM_BILLINGTRANSACTIONSCOLLECTION
205
209
  Megam::BillingtransactionsCollection
206
- when MEGAM_PROMOS
210
+ when MEGAM_SUBSCRIPTIONS
211
+ Megam::Subscriptions
212
+ when MEGAM_SUBSCRIPTIONSCOLLECTION
213
+ Megam::SubscriptionsCollection
214
+ when MEGAM_ADDONS
215
+ Megam::Addons
216
+ when MEGAM_ADDONSCOLLECTION
217
+ Megam::AddonsCollection
218
+ when MEGAM_PROMOS
207
219
  Megam::Promos
208
220
  else
209
221
  fail JSON::ParserError, "Unsupported `json_class` type '#{json_class}'"
@@ -3,7 +3,9 @@ module Megam
3
3
  attr_reader :email
4
4
  attr_reader :api_key
5
5
  attr_reader :host
6
- attr_reader :password
6
+ # the name :password is used as attr_accessor in accounts,
7
+ # hence we use gpassword
8
+ attr_reader :gpassword
7
9
  attr_reader :org_id
8
10
  attr_reader :headers
9
11
 
@@ -13,7 +15,7 @@ module Megam
13
15
  @email = o[:email]
14
16
  @api_key = o[:api_key] || nil
15
17
  @host = o[:host]
16
- @password = o[:password] || nil
18
+ @gpassword = o[:password] || nil
17
19
  @org_id = o[:org_id]
18
20
  @headers = o[:headers]
19
21
  end
@@ -27,7 +29,7 @@ module Megam
27
29
  :email => email,
28
30
  :api_key => api_key,
29
31
  :org_id => org_id,
30
- :password => password,
32
+ :password => gpassword,
31
33
  :host => host
32
34
  }
33
35
  if headers
@@ -0,0 +1,167 @@
1
+ module Megam
2
+ class Subscriptions < Megam::RestAdapter
3
+ def initialize(o)
4
+ @id = nil
5
+ @account_id = nil
6
+ @model = nil
7
+ @license = nil
8
+ @trial_ends = nil
9
+ @created_at = nil
10
+ @some_msg = {}
11
+ super(o)
12
+ end
13
+
14
+ def subscriptions
15
+ self
16
+ end
17
+
18
+ def id(arg=nil)
19
+ if arg != nil
20
+ @id = arg
21
+ else
22
+ @id
23
+ end
24
+ end
25
+
26
+ def account_id(arg=nil)
27
+ if arg != nil
28
+ @account_id= arg
29
+ else
30
+ @account_id
31
+ end
32
+ end
33
+
34
+ def model(arg=nil)
35
+ if arg != nil
36
+ @model = arg
37
+ else
38
+ @model
39
+ end
40
+ end
41
+
42
+ def license(arg=nil)
43
+ if arg != nil
44
+ @license= arg
45
+ else
46
+ @license
47
+ end
48
+ end
49
+
50
+ def trial_ends(arg=nil)
51
+ if arg != nil
52
+ @trial_ends = arg
53
+ else
54
+ @trial_ends
55
+ end
56
+ end
57
+
58
+ def created_at(arg=nil)
59
+ if arg != nil
60
+ @created_at = arg
61
+ else
62
+ @created_at
63
+ end
64
+ end
65
+
66
+ def some_msg(arg=nil)
67
+ if arg != nil
68
+ @some_msg = arg
69
+ else
70
+ @some_msg
71
+ end
72
+ end
73
+
74
+ def error?
75
+ crocked = true if (some_msg.has_key?(:msg_type) && some_msg[:msg_type] == "error")
76
+ end
77
+
78
+ # Transform the ruby obj -> to a Hash
79
+ def to_hash
80
+ index_hash = Hash.new
81
+ index_hash["json_claz"] = self.class.name
82
+ index_hash["id"] = id
83
+ index_hash["account_id"] = account_id
84
+ index_hash["model"] = model
85
+ index_hash["license"] = license
86
+ index_hash["trial_ends"] = trial_ends
87
+ index_hash["created_at"] = created_at
88
+ index_hash
89
+ end
90
+
91
+ # Serialize this object as a hash: called from JsonCompat.
92
+ # Verify if this called from JsonCompat during testing.
93
+ def to_json(*a)
94
+ for_json.to_json(*a)
95
+ end
96
+
97
+ def for_json
98
+ result = {
99
+ "id" => id,
100
+ "account_id" => account_id,
101
+ "model" => model,
102
+ "license" => license,
103
+ "trial_ends" => trial_ends,
104
+ "created_at" => created_at
105
+ }
106
+ result
107
+ end
108
+
109
+ #
110
+ def self.json_create(o)
111
+ sbs = new({})
112
+ sbs.id(o["id"]) if o.has_key?("id")
113
+ sbs.account_id(o["account_id"]) if o.has_key?("account_id")
114
+ sbs.model(o["model"]) if o.has_key?("model")
115
+ sbs.license(o["license"]) if o.has_key?("license")
116
+ sbs.trial_ends(o["trial_ends"]) if o.has_key?("trial_ends")
117
+ sbs.created_at(o["created_at"]) if o.has_key?("created_at")
118
+ #success or error
119
+ sbs.some_msg[:code] = o["code"] if o.has_key?("code")
120
+ sbs.some_msg[:msg_type] = o["msg_type"] if o.has_key?("msg_type")
121
+ sbs.some_msg[:msg]= o["msg"] if o.has_key?("msg")
122
+ sbs.some_msg[:links] = o["links"] if o.has_key?("links")
123
+ sbs
124
+ end
125
+
126
+ def self.from_hash(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
127
+ sbs = self.new(tmp_email, tmp_api_key, tmp_host)
128
+ sbs.from_hash(o)
129
+ sbs
130
+ end
131
+
132
+ def from_hash(o)
133
+ @id = o[:id] if o.has_key?(:id)
134
+ @account_id = o[:account_id] if o.has_key?(:account_id)
135
+ @model = o[:model] if o.has_key?(:model)
136
+ @license = o[:license] if o.has_key?(:license)
137
+ @trial_ends = o[:trial_ends] if o.has_key?(:trial_ends)
138
+ @created_at = o[:created_at] if o.has_key?(:created_at)
139
+ self
140
+ end
141
+
142
+ def self.create(o,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
143
+ acct = from_hash(o,tmp_email, tmp_api_key, tmp_host)
144
+ acct.create
145
+ end
146
+
147
+ # Create the subscriptions via the REST API
148
+ def create
149
+ megam_rest.post_subscriptions(to_hash)
150
+ end
151
+
152
+ # Load all subscriptions -
153
+ # returns a SubscriptionsCollection
154
+ # don't return self. check if the Megam::SubscriptionsCollection is returned.
155
+
156
+ def self.show(p_name,tmp_email=nil, tmp_api_key=nil, tmp_host=nil)
157
+ pre = self.new(tmp_email,tmp_api_key, tmp_host)
158
+ pre.megam_rest.get_subscription
159
+ end
160
+
161
+
162
+ def to_s
163
+ Megam::Stuff.styled_hash(to_hash)
164
+ end
165
+
166
+ end
167
+ end