koa-utils 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/koa-utils/kik.rb +83 -0
  3. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29e86ef60c9042b8d95dad6dc228ed7865540adc
4
- data.tar.gz: fe6d48df750aef711a105f5238430d1f94dbc00f
3
+ metadata.gz: 4afd70513f66c1a65a15fc3f5f7fd9048de6115a
4
+ data.tar.gz: 3f6c9164c2e69e4b9fd03f03d8539d8513800598
5
5
  SHA512:
6
- metadata.gz: 287095e706aefd667bced362a67244911d71c53818d4661a523794d4d03a6270d3e57d946f27fa5ba337483a436b95fb1fe5a0d28524f92605af8489b0adebba
7
- data.tar.gz: 1c253e164914b65744e48aee02c9b0c319d8e5e1fb0ab26b50ccbdf4804aaee267ed18120831986b6df48832f262f7b5c8350244ec69046681a30b9f1e284c07
6
+ metadata.gz: 5ecf5ee95d92a31f764470bfcd2bee351d0a1c31ced0327ae40125f20986afd6846618d453bbcabcb0ca9736be830160cfd988115343380b5a7926ed2a0107bc
7
+ data.tar.gz: c5b29c405082c54ab133daaea6a01799312185de5394e9ede48940cfa1b7b0cb7fd040536379da6260969f8ce0b01d00143e628d7187b297c20e45a5781a42ff
@@ -0,0 +1,83 @@
1
+ module KOAUtils::Kik
2
+ def self.purchase(signedData, username, host)
3
+ KOAUtils::Logger.measure_block("kikpurchase") do
4
+ url = "https://purchase.kik.com/verification/v1/check?u=#{username}&d=#{host}"
5
+ response = request_with_retry(url, signedData, 5)
6
+ if response.nil? or response.code.to_i != 200
7
+ code = (response and response.code)
8
+ code ||= "nil"
9
+ KOAUtils::Logger.count("kikpurchase.fail", 1,
10
+ {source: code, username: username, signed_data: signedData})
11
+ return false
12
+ end
13
+ JSON.parse(response.body)
14
+ end
15
+ end
16
+
17
+ def self.push(token, message, data)
18
+ KOAUtils::Logger.measure_block("kikpush") do
19
+ url = "https://api.kik.com/push/v1/send"
20
+ body = {
21
+ token: token,
22
+ ticker: message,
23
+ data: data
24
+ }
25
+ response = request_with_retry(url, body.to_json, 1, 1)
26
+ response_code = (response && response.code)
27
+ response_code ||= "nil"
28
+ PushResponse.new(response).tap do |pr|
29
+ if pr.success?
30
+ KOAUtils::Logger.count("notification_sent", 1)
31
+ else
32
+ KOAUtils::Logger.count("notification_fail", 1, {source: response_code})
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def self.verify(signedData, username, host)
39
+ KOAUtils::Logger.measure_block("kikverify") do
40
+ url = "https://auth.kik.com/verification/v1/check?u=#{username}&d=#{host}"
41
+ request_with_retry(url, signedData, 5).body
42
+ end
43
+ end
44
+
45
+ def self.request_with_retry(url, body, tries, seconds = 2)
46
+ tries.times do
47
+ begin
48
+ Timeout::timeout(seconds) do
49
+ return request(url, body)
50
+ end
51
+ rescue Timeout::Error
52
+ KOAUtils::Logger.count("kiktimeout", 1)
53
+ nil
54
+ end
55
+ end
56
+ end
57
+
58
+ def self.request(url, body)
59
+ uri = URI.parse(url)
60
+ http = Net::HTTP.new(uri.host, uri.port)
61
+ http.use_ssl = true
62
+ request = Net::HTTP::Post.new(uri.request_uri)
63
+ request.add_field('Content-Type', 'application/json')
64
+ request.add_field('Content-Length', body.size)
65
+
66
+ request.body = body
67
+ http.request(request)
68
+ end
69
+
70
+ class PushResponse
71
+ def initialize(response)
72
+ @response = response
73
+ end
74
+
75
+ def success?
76
+ @response and @response.code.to_i == 200
77
+ end
78
+
79
+ def bad_token?
80
+ @response and @response.code.to_i == 403
81
+ end
82
+ end
83
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koa-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Rykwalder
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/koa-utils/health-check.rb
20
+ - lib/koa-utils/kik.rb
20
21
  - lib/koa-utils/logger.rb
21
22
  - lib/koa-utils/rack-request-timer.rb
22
23
  - lib/koa-utils.rb