eme_services_client 0.0.11

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,23 @@
1
+ class RefreshWalletWorker
2
+ include SuckerPunch::Job
3
+ workers 2
4
+
5
+ @available_connections = [:refresh_wallet_worker_1, :refresh_wallet_worker_2, :refresh_wallet_worker_3]
6
+ def self.get_connection
7
+ @available_connections.pop
8
+ end
9
+
10
+ def self.return_connection(conn)
11
+ @available_connections.push(conn)
12
+ end
13
+
14
+ def self.all_connections
15
+ @available_connections
16
+ end
17
+
18
+ def perform(master_account_id, currency = "EMP", opts = {})
19
+ conn = RefreshWalletWorker.get_connection
20
+ EME::Billing.reload_wallet(master_account_id, currency, opts, EME::Billing.connection(conn))
21
+ RefreshWalletWorker.return_connection(conn)
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'api_consumer'
2
+
3
+ module EME
4
+ class BT < APIConsumer
5
+ def self.track(data = {}, conn = connection, opts = {}, reload = false)
6
+ return nil if data[:master_account_id].nil? || data[:action].nil? || data[:label].nil?
7
+ d = { :uid => data[:master_account_id], :act => data[:action], :label => data[:label] }
8
+ d[:game] = data[:game] || settings[:game]
9
+ d[:res] = data[:resource] || settings[:resource]
10
+ d[:md] = data[:metadata] if data[:metadata]
11
+ d[:ip] = data[:ip] if data[:ip]
12
+
13
+ opts[:method] = :post
14
+ #opts[:body] = d.to_json
15
+
16
+ opts[:body] = URI.encode_www_form(d)
17
+ opts[:headers] = {"Content-Type" => "application/x-www-form-urlencoded"}
18
+ return do_request("/cast", conn, opts)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'api_consumer'
2
+
3
+ module EME
4
+ class Lootbox < APIConsumer
5
+ def self.definitions(game_id = nil, conn = connection, opts = {}, reload = false)
6
+ opts[:reload] ||= reload
7
+ opts[:ttl] ||= 900
8
+ path = "/loot_box_definitions"
9
+ path += "?game_id=#{game_id}" if game_id
10
+ return do_request(path, conn, opts)
11
+ end
12
+
13
+ def self.lootboxes(game_account_id, conn = connection, opts = {}, reload = false)
14
+ opts[:reload] ||= reload
15
+ opts[:ttl] ||= 1
16
+ return do_request("/loot_boxes?game_account_id=#{game_account_id}", conn, opts)
17
+ end
18
+
19
+ def self.open(loot_box_id, game_account_id, token, conn = connection, opts = {}, reload = false)
20
+ opts[:reload] ||= reload
21
+ opts[:method] = :post
22
+ opts[:ttl] ||= 1
23
+ opts[:body] = {id: loot_box_id, game_account_id: game_account_id, token: token}.to_json
24
+ return do_request("/loot_boxes/open", conn, opts)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ require 'api_consumer'
2
+
3
+ module EME
4
+ class Shop < APIConsumer
5
+ def self.categories(game_id, conn = connection, opts = {}, reload = false)
6
+ opts[:reload] ||= reload
7
+ return do_request("/shop/categories?game_id=#{game_id}", conn, opts)
8
+ end
9
+
10
+ def self.gumballs(game, conn = connection, opts = {}, reload = false)
11
+ opts[:reload] ||= reload
12
+ return do_request("/shop/gumballs?game=#{game}", conn, opts)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ require 'fast_config'
2
+ require 'json'
3
+
4
+ module EME
5
+ class SSO
6
+ extend FastConfig
7
+
8
+ #http://auth.service.edge.enmasse.com:4567/verify_shop_token/#{token}
9
+ def self.verify_tera_token(token)
10
+ return nil if token == nil
11
+ auth_request("/verify_shop_token/#{token}")
12
+ end
13
+
14
+ def self.verify_sso_token(token)
15
+ auth_request("/sso/ticket/#{token}/verify", :post)
16
+ end
17
+
18
+ def self.verify_insecure_sso_token(token)
19
+ auth_request("/sso/ticket/#{token}/verify?tt=sso_insecure", :post)
20
+ end
21
+
22
+ private
23
+ def self.auth_request(path, http_method = :get, data = nil)
24
+ auth_response = nil
25
+ Net::HTTP.start(EME::SSO.settings[:auth_server], EME::SSO.settings[:auth_server_port]) do |http|
26
+ http.open_timeout = 5
27
+ http.read_timeout = 10
28
+ auth_response = if http_method == :get
29
+ http.get(path)
30
+ elsif http_method == :post
31
+ http.post(path, data)
32
+ else
33
+ raise RuntimeError, "Only :get and :post are allowed."
34
+ end
35
+ end
36
+ # parse the JSON....
37
+ return JSON.parse auth_response.body
38
+ end
39
+ end
40
+ end
41
+ require_relative "sso/token_reader"
@@ -0,0 +1,74 @@
1
+ class EME::SSO::TokenReader
2
+
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ req = Rack::Request.new(env)
9
+ cookies = req.cookies
10
+ session = req.session
11
+ @eat_cookies = false
12
+ @bake_cookies = false
13
+ if cookies["_ssot"] && (!session["account_id"] || cookies["_ssot"] != session[:ssot])
14
+ connect_account(cookies, session)
15
+ elsif !cookies["_ssot"] && session["account_id"]
16
+ @eat_cookies = true
17
+ #disconnect_account(cookies, session)
18
+ end
19
+ @status, @headers, @response = @app.call(env)
20
+ set_cookies(cookies, session) if @eat_cookies || @bake_cookies
21
+ return [@status, @headers, @response]
22
+ end
23
+
24
+ def connect_account(cookies, session)
25
+ auth_response = EME::SSO::TokenReader.get_account_info(cookies["_ssot"])
26
+ code = auth_response.code.to_i
27
+ # {"temp_screen_name"=>false, "screen_name"=>"kaboo", "language"=>"en", "account_status"=>1, "email"=>"chris@chrisreister.com", "id"=>12345}
28
+ if code == 200
29
+ account_info = JSON.parse(auth_response.body)
30
+ EME::SSO::TokenReader.setup_user_session(account_info, session, cookies["_ssot"])
31
+ @bake_cookies = true
32
+ else # delete cookie if it was bad?
33
+ @eat_cookies = true
34
+ end
35
+ return true
36
+ rescue Exception => e
37
+ puts e.inspect
38
+ puts e.backtrace
39
+ #Airbrake.notify(e) # TO DO: setup airbrake?
40
+ return true
41
+ end
42
+
43
+ def self.get_account_info(ssot)
44
+ auth_response = nil
45
+ Net::HTTP.start(EME::SSO.settings[:auth_server], EME::SSO.settings[:auth_server_port]) do |http|
46
+ http.open_timeout = 5
47
+ http.read_timeout = 10
48
+ auth_response = http.post("/sso/ticket/#{ssot}/verify", "tt=sso_insecure")
49
+ end
50
+ return auth_response
51
+ end
52
+
53
+ def self.setup_user_session(user, session, ssot = nil)
54
+ session[:ssot] = ssot
55
+ session[:screen_name] = if user["temp_screen_name"]
56
+ user["email"].split("@")[0]
57
+ else
58
+ user["screen_name"]
59
+ end
60
+ session[:account_id] = user["id"]
61
+ session[:account_info] = user
62
+ end
63
+
64
+ def set_cookies(cookies, session)
65
+ if @eat_cookies
66
+ Rack::Utils.set_cookie_header!(@headers, "screen_name", {:value => "", :path => "/", :domain => ".enmasse.com", :expires => Time.now-86400})
67
+ Rack::Utils.set_cookie_header!(@headers, "_ssot", {:value => "", :path => "/", :domain => ".enmasse.com", :expires => Time.now-86400})
68
+ Rack::Utils.set_cookie_header!(@headers, "serialized", {:value => "", :path => "/", :domain => ".enmasse.com", :expires => Time.now-86400})
69
+ session.clear
70
+ elsif @bake_cookies
71
+ Rack::Utils.set_cookie_header!(@headers, "screen_name", {:value => session[:screen_name], :domain => ".enmasse.com", :path => "/"})
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,246 @@
1
+ require 'api_consumer'
2
+
3
+ module EME
4
+ class Subscription < APIConsumer
5
+ def self.all(page = 1, count = 100, conn = connection, opts = {}, reload = false)
6
+ opts = opts_work(opts)
7
+ opts[:ttl] ||= 10
8
+ opts[:reverse] ||= false
9
+ opts[:orderby] ||= "id"
10
+ opts[:reload] ||= reload
11
+ return do_request("/subscriptions?from=#{(page-1)*count}&count=#{count}&orderby=#{opts[:orderby]}&reverse=#{opts[:reverse]}", conn, opts)
12
+ end
13
+
14
+ def self.fetch_active(master_account_id, conn = connection, opts = {}, reload = false)
15
+ opts = opts_work(opts, "active-m-account-sub-#{master_account_id}")
16
+ opts[:reload] ||= reload
17
+ opts[:ttl] ||= 10
18
+ return do_request("/subscriptions/find_active_for_account/#{master_account_id}", conn, opts )
19
+ end
20
+
21
+ def self.fetch_active_for_game_account(game_account_id, conn = connection, opts = {}, reload = false)
22
+ opts = opts_work(opts, "active-m-game-account-sub-#{game_account_id}")
23
+ opts[:reload] ||= reload
24
+ opts[:ttl] ||= 10
25
+ return do_request("/subscriptions/find_active_for_game_account/#{game_account_id}", conn, opts )
26
+ end
27
+
28
+ def self.fetch_all(master_account_id, conn = connection, opts = {}, reload = false)
29
+ opts = opts_work(opts, "all-m-account-sub-#{master_account_id}")
30
+ opts[:reload] ||= reload
31
+ opts[:ttl] ||= 10
32
+ return do_request("/subscriptions/find_for_account/#{master_account_id}", conn, opts)
33
+ end
34
+
35
+ def self.fetch_all_for_game_account(game_account_id, conn = connection, opts = {}, reload = false)
36
+ opts = opts_work(opts, "all-m-game-account-sub-#{game_account_id}")
37
+ opts[:reload] ||= reload
38
+ return do_request("/subscriptions/find_for_game_account/#{game_account_id}", conn, opts)
39
+ end
40
+
41
+ def self.fetch(subscription_id, conn = connection, opts = {}, reload = false)
42
+ opts = opts_work(opts, "sub-#{subscription_id}")
43
+ opts[:reload] ||= reload
44
+ opts[:ttl] ||= 10
45
+ return do_request("/subscriptions/#{subscription_id}", conn, opts)
46
+ end
47
+
48
+ def self.create(ma_id, game_account_id, game_id, request_opts = {}, conn = connection, opts ={}, reload = false)
49
+ #optional params: :recurring, :started_at, :ended_at, :next_delivery_at, :next_payment_at, :subscription_kind_id
50
+ json = request_opts
51
+ json[:master_account_id] = ma_id
52
+ json[:start] = true if json[:start].nil?
53
+ json[:game_account_id] = game_account_id
54
+ json[:game_id] = game_id
55
+ json[:subscription_kind_name] = "30dayelite" if json[:subscription_kind_name].nil?
56
+ opts = opts_work(opts) # no cache
57
+ opts[:reload] ||= reload
58
+ opts[:method] = :post
59
+ opts[:body] = json.to_json
60
+
61
+ results = do_request("/subscriptions", conn, opts)
62
+ # erase cache here
63
+ clear_cache_for_account(ma_id)
64
+ return results
65
+ end
66
+
67
+ def self.cancel(sub_id, conn = connection, opts ={}, reload = false)
68
+ opts = opts_work(opts)
69
+ opts[:reload] ||= reload
70
+ opts[:method] = :post
71
+ results = do_request("/subscriptions/#{sub_id}/cancel", conn, opts)
72
+ clear_cache_for_subscription(sub_id)
73
+ return results
74
+ end
75
+
76
+ def self.revive(sub_id, conn = connection, opts ={}, reload = false)
77
+ opts = opts_work(opts)
78
+ opts[:reload] ||= reload
79
+ opts[:method] = :post
80
+ results = do_request("/subscriptions/#{sub_id}/revive", conn, opts)
81
+ clear_cache_for_subscription(sub_id)
82
+ return results
83
+ end
84
+
85
+ def self.delete(sub_id, conn = connection, opts ={}, reload = false)
86
+ opts = opts_work(opts)
87
+ opts[:reload] ||= reload
88
+ opts[:method] = :delete
89
+ results = do_request("/subscriptions/#{sub_id}", conn, opts)
90
+ clear_cache_for_subscription(sub_id)
91
+ return results
92
+ end
93
+
94
+ def self.update(sub_id, conn = connection, opts ={}, reload = false)
95
+ opts = opts_work(opts)
96
+ opts[:reload] ||= reload
97
+ opts[:method] = :post
98
+ results = do_request("/subscriptions/#{sub_id}", conn, opts)
99
+ clear_cache_for_subscription(sub_id)
100
+ return results
101
+ end
102
+
103
+ def self.subscription_info_for_game_account(game_account_id, conn = connection, opts ={}, reload = false)
104
+ opts = opts_work(opts)
105
+ opts[:reload] ||= reload
106
+ do_request("/subscriptions/get_subscription_info_by_game_account/#{game_account_id}", conn, opts)
107
+ end
108
+
109
+ def self.add_days(sub_id, days, notes = nil, additional_info = nil, conn = connection, opts ={}, reload = false)
110
+ opts = opts_work(opts)
111
+ opts[:reload] ||= reload
112
+ opts[:method] = :post
113
+ opts[:body] = {:days => days, :notes => notes, :additional_info => additional_info}.to_json
114
+ results = do_request("/subscriptions/#{sub_id}/add_days", conn, opts)
115
+ clear_cache_for_subscription(sub_id)
116
+ return results
117
+ end
118
+
119
+ def self.add_days_with_subscription_creation(ma_id, game_account_id, game_id, days, request_opts = {}, conn = connection, opts ={}, reload = false)
120
+ #optional params: :subscription_kind_name, :notes, :addition_info
121
+ json = request_opts
122
+ json[:master_account_id] = ma_id
123
+ json[:game_account_id] = game_account_id
124
+ json[:game_id] = game_id
125
+ json[:days] = days
126
+ json[:subscription_kind_name] = "30dayelite" if json[:subscription_kind_name].nil?
127
+ opts = opts_work(opts) # no cache
128
+ opts[:reload] ||= reload
129
+ opts[:method] = :post
130
+ opts[:body] = json.to_json
131
+
132
+ results = do_request("/subscriptions/add_days", conn, opts)
133
+ # erase cache here
134
+ clear_cache_for_account(ma_id)
135
+ if results['subscription'] && results['subscription']['id']
136
+ clear_cache_for_subscription(results['subscription']['id'])
137
+ end
138
+ return results
139
+ end
140
+
141
+ def self.remove_days(sub_id, days, notes = nil, additional_info = nil, conn = connection, opts ={}, reload = false)
142
+ opts = opts_work(opts)
143
+ opts[:reload] ||= reload
144
+ opts[:method] = :post
145
+ opts[:body] = {:days => days, :notes => notes, :additional_info => additional_info}.to_json
146
+ results = do_request("/subscriptions/#{sub_id}/remove_days", conn, opts)
147
+ clear_cache_for_subscription(sub_id)
148
+ return results
149
+ end
150
+
151
+ def self.events_for_sub(sub_id, conn = connection, opts ={}, reload = false)
152
+ opts = opts_work(opts)
153
+ opts[:reload] ||= reload
154
+ opts[:count] ||= 100
155
+ opts[:reverse] ||= false
156
+ opts[:orderby] ||= "id"
157
+ return do_request("/subscription_events?orderby=#{opts[:orderby]}&reverse=#{opts[:reverse]}&count=#{opts[:count]}&subscription_id=#{sub_id}", conn, opts)
158
+ end
159
+
160
+ def self.events(sub_id, game_account_id, master_account_id, conn = connection, opts ={}, reload = false)
161
+ opts = opts_work(opts)
162
+ opts[:reload] ||= reload
163
+ opts[:count] ||= 100
164
+ opts[:reverse] ||= false
165
+ opts[:orderby] ||= "id"
166
+ url = "/subscription_events?orderby=#{opts[:orderby]}&reverse=#{opts[:reverse]}&count=#{opts[:count]}"
167
+ url += "&subscription_id=#{sub_id}" if sub_id
168
+ url += "&game_account_id=#{game_account_id}" if game_account_id
169
+ url += "&master_account_id=#{master_account_id}" if master_account_id
170
+ return do_request(url, conn, opts)
171
+ end
172
+
173
+ def self.price_override_logs(game_account_id, master_account_id, conn = connection, opts ={}, reload = false)
174
+ opts = opts_work(opts)
175
+ opts[:reload] ||= reload
176
+ opts[:count] ||= 100
177
+ opts[:reverse] ||= false
178
+ opts[:orderby] ||= "id"
179
+ url = "/subscription_price_override_logs?orderby=#{opts[:orderby]}&reverse=#{opts[:reverse]}&count=#{opts[:count]}"
180
+ url += "&game_account_id=#{game_account_id}" if game_account_id
181
+ url += "&master_account_id=#{master_account_id}" if master_account_id
182
+ return do_request(url, conn, opts)
183
+ end
184
+
185
+ def self.fetch_price_overrides_by_game_account_id(game_account_id, conn = connection, opts ={}, reload = false)
186
+ opts = opts_work(opts)
187
+ opts[:reload] ||= reload
188
+ return do_request("/subscription_price_overrides/find_for_game_account/#{game_account_id}", conn, opts)
189
+ end
190
+
191
+ def self.delete_price_override(master_account_id, game_account_id, game_id, conn = connection, opts ={}, reload = false)
192
+ opts = opts_work(opts)
193
+ opts[:reload] ||= reload
194
+ opts[:method] = :post
195
+ body = {}
196
+ body[:master_account_id] = master_account_id if master_account_id
197
+ body[:game_account_id] = game_account_id if game_account_id
198
+ body[:game_id] = game_id if game_id
199
+ opts[:body] = body.to_json
200
+ return do_request("/subscription_price_overrides/delete", conn, opts)
201
+ end
202
+
203
+ def self.upsert_price_override(master_account_id, game_account_id, game_id, override_price, conn = connection, opts ={}, reload = false)
204
+ opts = opts_work(opts)
205
+ opts[:reload] ||= reload
206
+ opts[:method] = :post
207
+ opts[:body] = {:master_account_id => master_account_id,
208
+ :game_account_id => game_account_id,
209
+ :game_id => game_id,
210
+ :override_price => override_price}.to_json
211
+ return do_request("/subscription_price_overrides/upsert", conn, opts)
212
+ end
213
+
214
+ def self.update_renew_price_by_game_account(game_account_id, price, conn = connection, opts ={}, reload = false)
215
+ opts = opts_work(opts)
216
+ opts[:reload] ||= reload
217
+ opts[:method] = :post
218
+ opts[:body] = {:price => price}.to_json
219
+ return do_request("/subscriptions/update_renew_price_by_game_account/#{game_account_id}", conn, opts)
220
+ end
221
+
222
+ def self.clear_cache_for_account(account)
223
+ cache.clear("active-m-account-sub-#{account}")
224
+ cache.clear("all-m-account-sub-#{account}")
225
+ return true
226
+ end
227
+
228
+ def self.clear_cache_for_subscription(sub_id)
229
+ cache.clear("sub-#{sub_id}")
230
+ return true
231
+ end
232
+
233
+ private
234
+ @@auth_opts = { "Authorization" => "Token token=#{settings[:api_key]}" }
235
+
236
+ def self.opts_work(opts, key = nil)
237
+ if opts[:headers].nil?
238
+ opts[:headers] = @@auth_opts
239
+ else
240
+ opts[:headers].merge(@@auth_opts)
241
+ end
242
+ opts[:key] = key if key
243
+ return opts
244
+ end
245
+ end
246
+ end