srl-api 0.4.0 → 0.4.1

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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -0
  3. data/lib/srl.rb +1 -1
  4. data/lib/srl/api.rb +26 -15
  5. data/lib/srl/query.rb +62 -0
  6. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 129f173ed7e75ec70eb66b7582d56135d59aa1ac
4
- data.tar.gz: f4b0e43b26a8517314aea1d7d251592ebe01056f
3
+ metadata.gz: 70a7c1a020ccd31a7c8b88363cf9bda2643d825b
4
+ data.tar.gz: 1d00bca316857d5e38ae19181d9634b7123b4621
5
5
  SHA512:
6
- metadata.gz: f397545db33cf4277bb85431c6229d3e600d19741ef7dbdb4d83b930bd2e4695837fa42fe11bc44cc31f2fdd02e73d9f5ea36be2221172427fafbe526a74d9ae
7
- data.tar.gz: 3f5e031e123c04e7d32b3273a15b8d050e35945ac866cefbc159f520a8e5ff5a3389ebcab001c14bef13e3b1f22237b2849e7e40b26229500d68117695f38412
6
+ metadata.gz: d162b873960d5b3da920b5e00f4cd84a8573564edc5fb80c23771bb58644b1f274c610f99f6cef700c6d4932aced7076b2d8da50eb1c6014ce66ffaf7f6c8c85
7
+ data.tar.gz: 15c69d309ed6ab202726aaedc592755614a6112c2bd0d43ac870e757f605bf2417a567b12d653ea8e7b014e1aae04027ef9bb15740db03366f846c001a3193c1
data/README.md CHANGED
@@ -43,3 +43,14 @@ will not.
43
43
  ## Maintainer
44
44
 
45
45
  Brian Edmonds "Artea" <[brian@bedmonds.net](mailto:brian@bedmonds.net)>
46
+
47
+ ## Disclaimer
48
+
49
+ I am not affiliated in any way with SpeedRunsLive.com; I'm just a runner
50
+ and viewer that likes the convenience of CLI tools. I wrote this small
51
+ library for myself and my tools, but thought other people might like to
52
+ use it as well.
53
+
54
+ Do try to not be a twat; servers and bandwidth cost money, so don't go
55
+ around writing a cron job that requests outrageous amounts of data every
56
+ five seconds.
data/lib/srl.rb CHANGED
@@ -2,7 +2,7 @@ require_relative 'srl/api'
2
2
 
3
3
  module SRL
4
4
  # The current version of srl-api.
5
- RELEASE = '0.4.0'.freeze
5
+ RELEASE = '0.4.1'.freeze
6
6
 
7
7
  # Return the current release version as a dotted string.
8
8
  def self.release
@@ -11,6 +11,7 @@ require 'srl/unmarshalable'
11
11
  require 'srl/game'
12
12
  require 'srl/past_race'
13
13
  require 'srl/player'
14
+ require 'srl/query'
14
15
  require 'srl/race'
15
16
  require 'srl/result_set'
16
17
 
@@ -61,15 +62,30 @@ module SRL
61
62
 
62
63
  # Return an array of PastRace objects for completed races.
63
64
  #
64
- # call-seq: current_races -> array
65
+ # You may filter the results by providing a `player` or `game`
66
+ # argument, to limit results to a specific player or game
67
+ # abbreviation.
68
+ #
69
+ # # To fetch only the six million LTTP Rando races.
70
+ # completed_races(game: 'alttphacks')
71
+ #
72
+ # # To only retrieve Edgeworth's FF Randomizer races.
73
+ # completed_races(player: 'Edgeworth', game: 'ffhacks')
74
+ #
75
+ # Results are paginated at `page_size` records per page,
76
+ # starting at 1. An upper limit to this number has not
77
+ # been tested, though I'd recommend that you not be a twat
78
+ # and limit your requests to something that will not murder
79
+ # the server.
80
+ #
81
+ # call-seq: current_races -> obj
65
82
  def completed_races(args = {})
66
- res = query('pastraces', rewrite_args(args))
67
- ResultSet.new(
68
- SRL::Utils.collection(res.fetch('pastraces'), PastRace),
69
- count: res.fetch('count'),
70
- page: args.fetch(:page, 1),
71
- page_size: args.fetch(:pageSize, 25)
72
- )
83
+ query =
84
+ Query.new('pastraces', PastRace, args.merge({ pkey: 'pastraces' }))
85
+
86
+ return query.page unless block_given?
87
+
88
+ yield query
73
89
  end
74
90
  alias past_races completed_races
75
91
 
@@ -87,14 +103,9 @@ module SRL
87
103
  raise NetworkError, res unless res.is_a?(Net::HTTPSuccess)
88
104
 
89
105
  JSON.parse(res.body)
90
- end
91
-
92
- # Alias camelCase argument names to snake_case. :nodoc:
93
- def rewrite_args(args)
94
- { pageSize: args.fetch(:page_size, 25) }.merge(args)
95
- end
96
-
106
+ end
97
107
  end
108
+
98
109
  # Raised when an HTTP request to the SRL API server fails,
99
110
  # whether due to a malformed request or the server being down.
100
111
  #
@@ -0,0 +1,62 @@
1
+ module SRL
2
+ class Query # :nodoc:
3
+ def initialize(url, klass, args = {})
4
+ @url = url
5
+ @klass = klass
6
+ @args = rewrite_args(args)
7
+
8
+ @page = @args.fetch(:page, 1)
9
+ @per_page = @args.fetch(:pageSize, 20)
10
+ end
11
+
12
+ def each_page
13
+ return enum_for(:each_page) unless block_given?
14
+
15
+ loop do
16
+ res = fetch_page
17
+ break if res.records.empty?
18
+
19
+ yield res.records
20
+
21
+ break if res.last_page?
22
+ end
23
+ end
24
+ alias each each_page
25
+
26
+ def page
27
+ fetch_page
28
+ end
29
+
30
+ private
31
+
32
+ # Return a hash with the results of a query to the SRL API. :nodoc:
33
+ def fetch_page
34
+ url = URI([API, @url].join) # *hiss* "URI" has been wrong for years!
35
+
36
+ unless @args.empty?
37
+ url.query = URI.encode_www_form(@args.merge({ page: @page }))
38
+ end
39
+
40
+ res = Net::HTTP.get_response(url)
41
+ raise SRL::NetworkError, res unless res.is_a?(Net::HTTPSuccess)
42
+
43
+ data = JSON.parse(res.body)
44
+ @page += 1
45
+
46
+ ResultSet.new(
47
+ SRL::Utils.collection(data.fetch(@args[:pkey]), @klass),
48
+ count: data.fetch('count', data.fetch(@args[:pkey]).size),
49
+ page: (@page - 1),
50
+ page_size: @args.fetch(:pageSize, 25)
51
+ )
52
+ end
53
+
54
+ # SpeedRunsLive API URL. :nodoc:
55
+ API = 'http://api.speedrunslive.com/'.freeze
56
+
57
+ # Alias camelCase argument names to snake_case. :nodoc:
58
+ def rewrite_args(args)
59
+ { pageSize: args.fetch(:page_size, 25) }.merge(args)
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srl-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian 'Artea' Edmonds
@@ -68,6 +68,7 @@ files:
68
68
  - lib/srl/game.rb
69
69
  - lib/srl/past_race.rb
70
70
  - lib/srl/player.rb
71
+ - lib/srl/query.rb
71
72
  - lib/srl/race.rb
72
73
  - lib/srl/result_set.rb
73
74
  - lib/srl/unmarshalable.rb