get_xkcd 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c5301e5087e8c893019aa4138e3f0c92899ee205e9c3e486e1de247b4457b4a3
4
+ data.tar.gz: 1eb13ba9e7b41c303079978fd3918ce8ddb8b357382e68936da034c6617ed4db
5
+ SHA512:
6
+ metadata.gz: 2c234f460d68739ccbc3b428b7e073d790822683c50be67dc784a29ab138fa067c0ed3758cf4ea8b4e1f490c73eb3a0fc1700cc48de21c480a7d4e0763db8105
7
+ data.tar.gz: 676fc885ae33537beb0ee2d713e9b845115a593dc3291555a4bf738f5919eb1ba666bd1c6593507a273c139374464368f2fb2fab5d28a187a526836a398bb92a
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rake_tasks~ ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,166 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Layout/BeginEndAlignment: # (new in 0.91)
13
+ Enabled: true
14
+
15
+ Layout/EmptyLinesAroundAttributeAccessor: # (new in 0.83)
16
+ Enabled: true
17
+
18
+ Layout/SpaceAroundMethodCallOperator: # (new in 0.82)
19
+ Enabled: true
20
+
21
+ Lint/BinaryOperatorWithIdenticalOperands: # (new in 0.89)
22
+ Enabled: true
23
+
24
+ Lint/ConstantDefinitionInBlock: # (new in 0.91)
25
+ Enabled: true
26
+
27
+ Lint/DeprecatedOpenSSLConstant: # (new in 0.84)
28
+ Enabled: true
29
+
30
+ Lint/DuplicateElsifCondition: # (new in 0.88)
31
+ Enabled: true
32
+
33
+ Lint/DuplicateRequire: # (new in 0.90)
34
+ Enabled: true
35
+
36
+ Lint/DuplicateRescueException: # (new in 0.89)
37
+ Enabled: true
38
+
39
+ Lint/EmptyConditionalBody: # (new in 0.89)
40
+ Enabled: true
41
+
42
+ Lint/EmptyFile: # (new in 0.90)
43
+ Enabled: true
44
+
45
+ Lint/FloatComparison: # (new in 0.89)
46
+ Enabled: true
47
+
48
+ Lint/HashCompareByIdentity: # (new in 0.93)
49
+ Enabled: true
50
+
51
+ Lint/IdentityComparison: # (new in 0.91)
52
+ Enabled: true
53
+
54
+ Lint/MissingSuper: # (new in 0.89)
55
+ Enabled: true
56
+
57
+ Lint/MixedRegexpCaptureTypes: # (new in 0.85)
58
+ Enabled: true
59
+
60
+ Lint/OutOfRangeRegexpRef: # (new in 0.89)
61
+ Enabled: true
62
+
63
+ Lint/RaiseException: # (new in 0.81)
64
+ Enabled: true
65
+
66
+ Lint/RedundantSafeNavigation: # (new in 0.93)
67
+ Enabled: true
68
+
69
+ Lint/SelfAssignment: # (new in 0.89)
70
+ Enabled: true
71
+
72
+ Lint/StructNewOverride: # (new in 0.81)
73
+ Enabled: true
74
+
75
+ Lint/TopLevelReturnWithArgument: # (new in 0.89)
76
+ Enabled: true
77
+
78
+ Lint/TrailingCommaInAttributeDeclaration: # (new in 0.90)
79
+ Enabled: true
80
+
81
+ Lint/UnreachableLoop: # (new in 0.89)
82
+ Enabled: true
83
+
84
+ Lint/UselessMethodDefinition: # (new in 0.90)
85
+ Enabled: true
86
+
87
+ Lint/UselessTimes: # (new in 0.91)
88
+ Enabled: true
89
+
90
+ Style/AccessorGrouping: # (new in 0.87)
91
+ Enabled: true
92
+
93
+ Style/BisectedAttrAccessor: # (new in 0.87)
94
+ Enabled: true
95
+
96
+ Style/CaseLikeIf: # (new in 0.88)
97
+ Enabled: true
98
+
99
+ Style/ClassEqualityComparison: # (new in 0.93)
100
+ Enabled: true
101
+
102
+ Style/CombinableLoops: # (new in 0.90)
103
+ Enabled: true
104
+
105
+ Style/ExplicitBlockArgument: # (new in 0.89)
106
+ Enabled: true
107
+
108
+ Style/ExponentialNotation: # (new in 0.82)
109
+ Enabled: true
110
+
111
+ Style/GlobalStdStream: # (new in 0.89)
112
+ Enabled: true
113
+
114
+ Style/HashAsLastArrayItem: # (new in 0.88)
115
+ Enabled: true
116
+
117
+ Style/HashEachMethods: # (new in 0.80)
118
+ Enabled: true
119
+
120
+ Style/HashLikeCase: # (new in 0.88)
121
+ Enabled: true
122
+
123
+ Style/HashTransformKeys: # (new in 0.80)
124
+ Enabled: true
125
+
126
+ Style/HashTransformValues: # (new in 0.80)
127
+ Enabled: true
128
+
129
+ Style/KeywordParametersOrder: # (new in 0.90)
130
+ Enabled: true
131
+
132
+ Style/OptionalBooleanParameter: # (new in 0.89)
133
+ Enabled: true
134
+
135
+ Style/RedundantAssignment: # (new in 0.87)
136
+ Enabled: true
137
+
138
+ Style/RedundantFetchBlock: # (new in 0.86)
139
+ Enabled: true
140
+
141
+ Style/RedundantFileExtensionInRequire: # (new in 0.88)
142
+ Enabled: true
143
+
144
+ Style/RedundantRegexpCharacterClass: # (new in 0.85)
145
+ Enabled: true
146
+
147
+ Style/RedundantRegexpEscape: # (new in 0.85)
148
+ Enabled: true
149
+
150
+ Style/RedundantSelfAssignment: # (new in 0.90)
151
+ Enabled: true
152
+
153
+ Style/SingleArgumentDig: # (new in 0.89)
154
+ Enabled: true
155
+
156
+ Style/SlicingWithRange: # (new in 0.83)
157
+ Enabled: true
158
+
159
+ Style/SoleNestedConditional: # (new in 0.89)
160
+ Enabled: true
161
+
162
+ Style/StringConcatenation: # (new in 0.89)
163
+ Enabled: true
164
+
165
+ Metrics/BlockLength:
166
+ ExcludedMethods: ['describe', 'context']
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ * First version!
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in get_xkcd.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 0.80"
11
+ gem "webmock", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ get_xkcd (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ ast (2.4.2)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.5.0)
15
+ hashdiff (1.0.1)
16
+ parallel (1.22.1)
17
+ parser (3.1.2.1)
18
+ ast (~> 2.4.1)
19
+ public_suffix (4.0.7)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.5.0)
23
+ rexml (3.2.5)
24
+ rspec (3.11.0)
25
+ rspec-core (~> 3.11.0)
26
+ rspec-expectations (~> 3.11.0)
27
+ rspec-mocks (~> 3.11.0)
28
+ rspec-core (3.11.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-expectations (3.11.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-mocks (3.11.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-support (3.11.0)
37
+ rubocop (0.93.1)
38
+ parallel (~> 1.10)
39
+ parser (>= 2.7.1.5)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8)
42
+ rexml
43
+ rubocop-ast (>= 0.6.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 2.0)
46
+ rubocop-ast (1.21.0)
47
+ parser (>= 3.1.1.0)
48
+ ruby-progressbar (1.11.0)
49
+ unicode-display_width (1.8.0)
50
+ webmock (3.17.1)
51
+ addressable (>= 2.8.0)
52
+ crack (>= 0.3.2)
53
+ hashdiff (>= 0.4.0, < 2.0.0)
54
+
55
+ PLATFORMS
56
+ x86_64-linux
57
+
58
+ DEPENDENCIES
59
+ get_xkcd!
60
+ rake (~> 13.0)
61
+ rspec (~> 3.0)
62
+ rubocop (~> 0.80)
63
+ webmock (~> 3.0)
64
+
65
+ BUNDLED WITH
66
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 superchilled
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # GetXkcd
2
+
3
+ This library provides methods to get the JSON data for either the most recent [XKCD](https://xkcd.com/) comic or a random one, and then return the JSON data as a Ruby Hash.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'get_xkcd'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install get_xkcd
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Require the gem in your application file if necessary (e.g. if using Rails, this won't be necessary as Rails takes care of this for you):
28
+
29
+ ```ruby
30
+ require 'get_xkcd'
31
+ ```
32
+
33
+ Get data for the latest comic:
34
+
35
+ ```ruby
36
+ latest_issue_data = GetXkcd::Comic.fetch_latest_issue_data
37
+ ```
38
+
39
+ Get data for the a random comic:
40
+
41
+ ```ruby
42
+ random_issue_data = GetXkcd::Comic.fetch_random_issue_data
43
+ ```
44
+
45
+ ## Development
46
+
47
+ 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.
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/superchilled/get-xkcd.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "get_xkcd"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/get_xkcd.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/get_xkcd/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "get_xkcd"
7
+ spec.version = GetXkcd::VERSION
8
+ spec.authors = ["superchilled"]
9
+ spec.email = ["karl@superchilled.co.uk"]
10
+
11
+ spec.summary = "A small Ruby library to interact with the XKCD API"
12
+ # rubocop:disable Layout/LineLength
13
+ spec.description = "This library provides methods to get the JSON data for either the most recent XKCD comic or a random one, and then return the JSON data as a Ruby Hash."
14
+ # rubocop:enable Layout/LineLength
15
+ spec.homepage = "https://github.com/superchilled/get-xkcd"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
18
+
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/superchilled/get-xkcd"
23
+ spec.metadata["changelog_uri"] = "https://github.com/superchilled/get-xkcd/CHANGELOG.md"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, checkout our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+
6
+ module GetXkcd
7
+ # Class that exposes methods for fetching XKCD comic data
8
+ class Comic
9
+ # Get the json for the most recent xkcd comic
10
+ def self.fetch_latest_issue_data
11
+ uri = URI("https://xkcd.com/info.0.json")
12
+ req = Net::HTTP::Get.new(uri)
13
+ req["Accept"] = "application/json"
14
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
15
+ http.request(req)
16
+ end
17
+
18
+ JSON.parse(res.body)
19
+ end
20
+
21
+ # Get the json for the xkcd comic of a specific number
22
+ def self.fetch_random_issue_data
23
+ uri = URI("https://xkcd.com/#{generate_random_issue_number}/info.0.json")
24
+
25
+ req = Net::HTTP::Get.new(uri)
26
+ req["Accept"] = "application/json"
27
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
28
+ http.request(req)
29
+ end
30
+
31
+ JSON.parse(res.body)
32
+ end
33
+
34
+ # Generate a random number up to the latest issue number (excluding 404)
35
+ def self.generate_random_issue_number
36
+ latest_issue_number = fetch_latest_issue_data["num"]
37
+ generate_random_number_excluding_404(latest_issue_number)
38
+ end
39
+
40
+ # Generate a random number from 1 up to input argument (excluding 404)
41
+ def self.generate_random_number_excluding_404(max)
42
+ number = rand(max) + 1
43
+ return number unless number == 404
44
+
45
+ generate_random_number_excluding_404(max)
46
+ end
47
+
48
+ private_class_method :generate_random_issue_number, :generate_random_number_excluding_404
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GetXkcd
4
+ VERSION = "0.1.0"
5
+ end
data/lib/get_xkcd.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "get_xkcd/version"
4
+ require "get_xkcd/comic"
5
+
6
+ module GetXkcd
7
+ class Error < StandardError; end
8
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: get_xkcd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - superchilled
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This library provides methods to get the JSON data for either the most
14
+ recent XKCD comic or a random one, and then return the JSON data as a Ruby Hash.
15
+ email:
16
+ - karl@superchilled.co.uk
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".github/workflows/main.yml"
22
+ - ".gitignore"
23
+ - ".rake_tasks~"
24
+ - ".rspec"
25
+ - ".rubocop.yml"
26
+ - CHANGELOG.md
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/console
33
+ - bin/setup
34
+ - get_xkcd.gemspec
35
+ - lib/get_xkcd.rb
36
+ - lib/get_xkcd/comic.rb
37
+ - lib/get_xkcd/version.rb
38
+ homepage: https://github.com/superchilled/get-xkcd
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ allowed_push_host: https://rubygems.org
43
+ homepage_uri: https://github.com/superchilled/get-xkcd
44
+ source_code_uri: https://github.com/superchilled/get-xkcd
45
+ changelog_uri: https://github.com/superchilled/get-xkcd/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.4.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.2.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A small Ruby library to interact with the XKCD API
65
+ test_files: []