jservice 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f53336cab276a74062a123630cc3103f5742aa1d079312b2a2930d01f9141df3
4
+ data.tar.gz: 7d06d26403e0c67b131ffad3161e21d49b3c65e0e0151bfe1bc27723e6006862
5
+ SHA512:
6
+ metadata.gz: 1d89290313cf883e68e32da4637a860c07a8dc24687a2957d2b4852cc813c9314c3c3cbc3194afc2b27406a6ba58d101790ec44b15625221eca5ef9d35073adb
7
+ data.tar.gz: 175a528755c07a062780da3a46d33baa09ceeca8541a2435b08a94433d6a3e2a24558b6eb7e870803d866ad2cd283e06bf6156ea1b24e32f6dcd4b82ef295c2c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-09
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jservice.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ jservice (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ parallel (1.22.1)
13
+ parser (3.2.0.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.6.2)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.44.0)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.2.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.24.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.24.1)
43
+ parser (>= 3.1.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.4.2)
46
+
47
+ PLATFORMS
48
+ x86_64-linux
49
+
50
+ DEPENDENCIES
51
+ jservice!
52
+ rake (~> 13.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 1.21)
55
+
56
+ BUNDLED WITH
57
+ 2.3.7
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2023 Alex Craik.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
8
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # Jservice
2
+
3
+ This gem provides bindings to the HTTP REST API hosted at [jService](https://jservice.io). This is a simple 1 to 1 API binding, nothing extra like caching is provided.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jservice'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jservice
20
+
21
+ ## Usage
22
+
23
+ ### 1. Create Jservice class
24
+ ```ruby
25
+ js = Jservice.new(
26
+ # base URL of API
27
+ url: 'jservice.io',
28
+ # use https
29
+ use_ssl: true,
30
+ # port to use for API (`nil` for default)
31
+ port: nil
32
+ )
33
+ ```
34
+ All hash options are optional and will use the default values if not given. 99% of the time, you just want `Jservice.new()`.
35
+
36
+ ### 2. Get a page of clues
37
+ ```ruby
38
+ clues = js.clues(
39
+ # value of clue (100-1000 or nil for Final Jeopardy)
40
+ value: 100,
41
+ # ID of category you want
42
+ # you'll likely want to use `categories` to find these
43
+ category: 0,
44
+ # specify a range of dates for clues
45
+ min_date: "1993-11-17T20:00:00.000Z"
46
+ max_date: "1993-11-20T20:00:00.000Z"
47
+ # offset of returned clues. used for paginating results
48
+ offset: 0
49
+ )
50
+ ```
51
+ `Jservice#clues` returns an array of clue objects as Hashes (with String keys.) As before, all options are optional and if not given the server will use whatever defaults. The clues are returned in a fixed order (evidently most recently added to the database.)
52
+
53
+ Example clue output (JSON):
54
+
55
+ ```json
56
+ {
57
+ "id": 35885,
58
+ "answer": "Madagascar",
59
+ "question": "Extinct for hundreds of years, the elephant bird weighed 1,000 pounds & lived on this east African island",
60
+ "value": 500,
61
+ "airdate": "1993-11-17T20:00:00.000Z",
62
+ "created_at": "2022-12-30T18:52:26.957Z",
63
+ "updated_at": "2022-12-30T18:52:26.957Z",
64
+ "category_id": 109,
65
+ "game_id": 1411,
66
+ "invalid_count": null,
67
+ "category": {
68
+ "id": 109,
69
+ "title": "animals",
70
+ "created_at": "2022-12-30T18:37:49.289Z",
71
+ "updated_at": "2022-12-30T18:37:49.289Z",
72
+ "clues_count": 536
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### 3. Get random clues
78
+ ```ruby
79
+ clues = js.random_clues(5)
80
+ ```
81
+ `Jservice#random_clues` returns `count` random clues from the API. Notably, this endpoint does **not** support any kind of search parameters.
82
+
83
+ ### 4. Get random Final Jeopardies
84
+ ```ruby
85
+ clues = js.random_finals(5)
86
+ ```
87
+ `Jservice#random_finals` returns `count` random Final Jeopardies from the API.
88
+ Notably, this endpoint does **not** support any kind of search parameters.
89
+
90
+ ### 5. Get a list of categories
91
+ ```ruby
92
+ cats = js.categories(
93
+ # number of categories
94
+ count: 1,
95
+ # offset of returned categories. used for paginating results
96
+ offset: 0
97
+ )
98
+ ```
99
+ `Jservice#categories` is similar to `clues`. It returns a list of categories from the server (seemingly in most-recently-added order.) Both options are optional.
100
+
101
+ Category object example (JSON):
102
+
103
+ ```json
104
+ {
105
+ "id":68,
106
+ "title": "gardens",
107
+ "clues_count": 18
108
+ }
109
+ ```
110
+
111
+ ### 6. Get a specific category by ID
112
+ ```ruby
113
+ cat = js.category(68)
114
+ ```
115
+ `Jservice#category` returns a specific category given its ID
116
+
117
+ ## Development
118
+
119
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
120
+
121
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
122
+
123
+ ## Contributing
124
+
125
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ajc2/jservice-gem.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+ class Jservice
5
+ VERSION = "1.0.0"
6
+ end
data/lib/jservice.rb ADDED
@@ -0,0 +1,141 @@
1
+ # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
4
+ require 'json'
5
+ require 'net/http'
6
+
7
+ require_relative "jservice/version"
8
+
9
+ class Jservice
10
+ class Error < StandardError; end
11
+
12
+ def initialize(**config)
13
+ @config = {
14
+ # base URL of API
15
+ url: 'jservice.io',
16
+ # use https
17
+ use_ssl: true,
18
+ # port to use for API (`nil` for default)
19
+ port: nil,
20
+ # ignore clues above the given invalid_count (`nil` ignored)
21
+ invalid_filter: nil
22
+ }
23
+ @config.merge!(config)
24
+ @config[:port] ||= @config[:use_ssl] ? 443 : 80
25
+
26
+ @http = Net::HTTP.new(@config[:url], @config[:port])
27
+ @http.use_ssl = @config[:use_ssl]
28
+ @http.start()
29
+ end
30
+
31
+ # request a page of clues
32
+ # = options =
33
+ # value ( int): the value of the clue in dollars
34
+ # category ( int): the id of the category you want to return
35
+ # min_date (date): earliest date to show, based on original air date
36
+ # max_date (date): latest date to show, based on original air date
37
+ # offset ( int): offsets the returned clues. Useful in pagination
38
+ def clues(**options)
39
+ # validate input
40
+ filter_keys(options, :value, :category, :min_date, :max_date, :offset, :page)
41
+
42
+ result = nil
43
+ begin
44
+ result = api_request('clues', options)
45
+ rescue ex
46
+ raise ex
47
+ else
48
+ return result
49
+ end
50
+ end
51
+
52
+ # request random clues
53
+ # count: number of clues to return
54
+ def random_clues(count = 1)
55
+ result = nil
56
+ begin
57
+ result = api_request('random', count: count)
58
+ rescue ex
59
+ raise ex
60
+ else
61
+ return result
62
+ end
63
+ end
64
+
65
+ # request random final jeopardy clues
66
+ # count: number of clues to return
67
+ def random_finals(count = 1)
68
+ result = nil
69
+ begin
70
+ result = api_request('final', count: count)
71
+ rescue ex
72
+ raise ex
73
+ else
74
+ return result
75
+ end
76
+ end
77
+
78
+ # request a list/page of categories
79
+ # = options =
80
+ # count (int): amount of categories to return, limited to 100 at a time
81
+ # offset (int): offsets the starting id of categories returned. Useful in pagination.
82
+ def categories(**options)
83
+ # validate input
84
+ filter_keys(options, :count, :offset)
85
+
86
+ result = nil
87
+ begin
88
+ result = api_request('categories', options)
89
+ rescue ex
90
+ raise ex
91
+ else
92
+ return result
93
+ end
94
+ end
95
+
96
+ # request a specific category
97
+ # id: integer ID of category
98
+ def category(id)
99
+ result = nil
100
+ begin
101
+ result = api_request('category', id: id)
102
+ rescue ex
103
+ raise ex
104
+ else
105
+ return result
106
+ end
107
+ end
108
+
109
+
110
+ private def filter_keys(options, *keys)
111
+ bad_keys = options.except(*keys).keys
112
+ unless bad_keys.empty?
113
+ # build error message
114
+ msg = "Unrecognized options: #{bad_keys.join(', ')}"
115
+ raise Jservice::Error.new(msg)
116
+ end
117
+ end
118
+
119
+ private def api_request(endpoint, options)
120
+ uri_hash = {
121
+ host: @config[:url],
122
+ path: "/api/#{endpoint}",
123
+ port: @config[:port],
124
+ query: URI.encode_www_form(options)
125
+ }
126
+ uri = nil
127
+ if @config[:use_ssl]
128
+ uri = URI::HTTPS.build(uri_hash)
129
+ else
130
+ uri = URI::HTTP.build(uri_hash)
131
+ end
132
+
133
+ req = @http.get(uri)
134
+
135
+ unless req.code == '200'
136
+ raise Jservice::Error.new("HTTP response #{req.code}")
137
+ end
138
+
139
+ JSON.parse(req.body)
140
+ end
141
+ end
data/sig/jservice.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Jservice
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jservice
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Craik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - contact@ajc2.xyz
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - lib/jservice.rb
29
+ - lib/jservice/version.rb
30
+ - sig/jservice.rbs
31
+ homepage: https://github.com/ajc2/jservice-gem
32
+ licenses:
33
+ - BSD-3-Clause
34
+ metadata:
35
+ homepage_uri: https://github.com/ajc2/jservice-gem
36
+ source_code_uri: https://github.com/ajc2/jservice-gem
37
+ changelog_uri: https://github.com/ajc2/jservice-gem
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.3.7
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Bindings to the jService REST API for Jeopardy questions.
57
+ test_files: []