koa-utils 0.0.11 → 0.0.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28698360221534e0bd5c1b86b6979352f3f8876c
4
- data.tar.gz: 7e30b74a6c131b38f072f37fe7af282b850ef495
3
+ metadata.gz: b27262d0499b1b32d45013f7d96a090162540731
4
+ data.tar.gz: 496f38a0cdf9e89002918fd6bd0162c06cbe1aec
5
5
  SHA512:
6
- metadata.gz: 044ba256ce989617459fb8e57e42f98b876513c55eb312f503f077e87fa5ce461d25fa177640e18ab756d4405de07f6496793b89efda685ee5a26e555df31eac
7
- data.tar.gz: 11c03c3b2b21bcf57942c7867002f22e3638c7505d5d59c0af7fdd15075ef69a971cbc287168146b98e5d25e538302f9b9ac2c6f7272432ba55b95bb3eb375e2
6
+ metadata.gz: d9a7999177cb1f06023f652d11dd1b91d570cb3d5de0c030dd176c95250485593739121ddcf7e1b448345d1219b65044d5ec6eb591bdfc22cd655fc1947a9694
7
+ data.tar.gz: db8600399e2bdb9168cc87181570b26f1ba3fcb3b56961272669af92b536db8cfab7dced48a398b402df6c26f8e875df951b5fe0c2f15086e62d3f143625797a
@@ -0,0 +1,13 @@
1
+ module KOAUtils
2
+ module Conf
3
+
4
+ def self.env(k)
5
+ ENV[k]
6
+ end
7
+
8
+ def self.env!(k)
9
+ env(k) || raise("Must set #{k}")
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ require 'json'
2
+ require 'securerandom'
3
+
4
+ module KOAUtils
5
+ module LeaderboardClient
6
+ URL = Conf.env!('LEADERBOARD_URL')
7
+ GAME_ID = Conf.env!('KOA_GAME_ID')
8
+
9
+ def self.get_leaders(type, limit)
10
+ response = KOAUtils::Request.make(
11
+ type: :get,
12
+ url: URL + "/boards/#{type}",
13
+ data: {limit: limit, game_id: GAME_ID}
14
+ )
15
+ if response.successful?
16
+ JSON.parse(response.body)
17
+ else
18
+ []
19
+ end
20
+ end
21
+
22
+ def self.get_user_scores(user_id)
23
+ response = KOAUtils::Request.make(
24
+ type: :get,
25
+ url: URL + "/users_scores",
26
+ data: {game_id: GAME_ID, user_id: user_id}
27
+ )
28
+ if response.successful?
29
+ user_scores = JSON.parse(response.body)
30
+ end
31
+ end
32
+
33
+ def self.add_score(user_id, score)
34
+ Logger.measure_block("leaderboard-client.add-score") do
35
+ KOAUtils::Request.make(
36
+ type: :put,
37
+ url: URL + "/score",
38
+ data: {
39
+ user_id: user_id,
40
+ score: score,
41
+ request_id: SecureRandom.uuid,
42
+ game_id: GAME_ID
43
+ },
44
+ tries: 3,
45
+ timeout: 5
46
+ )
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -7,82 +7,82 @@ require 'uri'
7
7
 
8
8
  module KOAUtils
9
9
  class TimeoutResponse
10
-
10
+
11
11
  def successful?
12
12
  false
13
13
  end
14
-
14
+
15
15
  def code
16
16
  '0'
17
17
  end
18
-
18
+
19
19
  def self.body_permitted?
20
20
  false
21
21
  end
22
-
22
+
23
23
  def body
24
24
  nil
25
25
  end
26
26
  end
27
-
27
+
28
28
  class ResponseDecorator < SimpleDelegator
29
29
  def successful?
30
30
  code.to_i / 100 == 2
31
31
  end
32
32
  end
33
-
33
+
34
34
  module Request
35
35
  def self.make(opts)
36
36
  opts[:tries] ||= 1
37
37
  opts[:timeout] ||= 10
38
-
38
+
39
39
  opts[:url] = build_url(opts)
40
40
  opts[:body] = build_body(opts)
41
-
41
+
42
42
  execute(opts)
43
43
  end
44
-
44
+
45
45
  private
46
-
46
+
47
47
  def self.execute(opts)
48
48
  opts[:tries].times do
49
-
49
+
50
50
  begin
51
51
  ::Timeout::timeout(opts[:timeout]) do
52
52
  return ResponseDecorator.new(net_request(opts))
53
53
  end
54
54
  rescue ::Timeout::Error
55
55
  end
56
-
56
+
57
57
  end
58
58
  KOAUtils::TimeoutResponse.new
59
59
  end
60
-
60
+
61
61
  def self.net_request(opts)
62
62
  uri = ::URI.parse(opts[:url])
63
63
  http = ::Net::HTTP.new(uri.host, uri.port)
64
64
  http.use_ssl = uri.scheme == "https"
65
65
  request = request_factory(opts[:type]).new(uri.request_uri)
66
-
66
+
67
67
  request.basic_auth uri.user, uri.password if uri.user
68
68
  request.basic_auth opts[:auth][:user], opts[:auth][:pass] if opts[:auth]
69
-
69
+
70
70
  if opts[:type] == :post || opts[:type] == :put
71
71
  request.add_field('Content-Type', 'application/json')
72
72
  request.add_field('Content-Length', opts[:body].size)
73
73
  request.body = opts[:body]
74
74
  end
75
-
75
+
76
76
  http.request(request)
77
77
  end
78
-
78
+
79
79
  def self.request_factory(type)
80
80
  return ::Net::HTTP::Post if type == :post
81
81
  return ::Net::HTTP::Put if type == :put
82
82
  return ::Net::HTTP::Delete if type == :delete
83
83
  return ::Net::HTTP::Get if type == :get
84
84
  end
85
-
85
+
86
86
  def self.build_body(opts)
87
87
  body = ""
88
88
  if opts[:type] == :post || opts[:type] == :put
@@ -90,7 +90,7 @@ module KOAUtils
90
90
  end
91
91
  body
92
92
  end
93
-
93
+
94
94
  def self.build_url(opts)
95
95
  url = opts[:url]
96
96
  if opts[:type] == :get || opts[:type] == :delete
@@ -98,9 +98,9 @@ module KOAUtils
98
98
  end
99
99
  url
100
100
  end
101
-
101
+
102
102
  def self.hash_to_query(hash)
103
103
  hash.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&")
104
104
  end
105
105
  end
106
- end
106
+ end
data/lib/koa-utils.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  module KOAUtils
2
2
  end
3
3
 
4
+ require 'koa-utils/conf'
4
5
  require 'koa-utils/health-check'
5
6
  require 'koa-utils/logger'
6
7
  require 'koa-utils/kik'
7
8
  require 'koa-utils/rack-request-timer'
8
9
  require 'koa-utils/request'
9
- require 'koa-utils/sequel-logger'
10
+ require 'koa-utils/leaderboard-client'
11
+ require 'koa-utils/sequel-logger'
metadata CHANGED
@@ -1,29 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koa-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Rykwalder
8
+ - Ryan Smith
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
  date: 2013-12-03 00:00:00.000000000 Z
12
13
  dependencies: []
13
- description: Gem of tools for KOA
14
+ description: Gem of tools for Koala
14
15
  email: eric@koa.la
15
16
  executables: []
16
17
  extensions: []
17
18
  extra_rdoc_files: []
18
19
  files:
20
+ - lib/koa-utils/conf.rb
19
21
  - lib/koa-utils/health-check.rb
20
22
  - lib/koa-utils/kik.rb
23
+ - lib/koa-utils/leaderboard-client.rb
21
24
  - lib/koa-utils/logger.rb
22
25
  - lib/koa-utils/rack-request-timer.rb
23
26
  - lib/koa-utils/request.rb
24
27
  - lib/koa-utils/sequel-logger.rb
25
28
  - lib/koa-utils.rb
26
- homepage: http://github.com/albumatic
29
+ homepage: http://koa.la
27
30
  licenses:
28
31
  - MIT
29
32
  metadata: {}
@@ -43,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
46
  version: '0'
44
47
  requirements: []
45
48
  rubyforge_project:
46
- rubygems_version: 2.0.6
49
+ rubygems_version: 2.0.3
47
50
  signing_key:
48
51
  specification_version: 4
49
52
  summary: KOA tools