random_gem 0.1.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: 6ea23e4a9b59d65128f8a1487088fe525a9195278a9dbdb4b8a8ee82aeca70f1
4
+ data.tar.gz: fbf8357006710a7348ad53e72de80df23dea7837c577f5a2fc96bad53b1abc64
5
+ SHA512:
6
+ metadata.gz: 354131a5a96af92f50f40cfa2a0b86981cadfaedad317c2ba77e45ae8fed3d3ada1b9bc34fc1447be3429b1ddf7d4d1c3e5d13a3813ec1cc56ca06b967e4f046
7
+ data.tar.gz: ee9094414f0cd3ff7589c6875a8626f12bfb8a9bec2df9aed1be58e2ad662725dff7bdd62ef24a303f7bfe3a9b75b3e49127d52c871ff190a6e40493e31a96ea
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.8
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sh07e1916@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in random_gem.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ random_gem (0.1.0)
5
+ thor
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.5.0)
11
+ rake (12.3.3)
12
+ rspec (3.12.0)
13
+ rspec-core (~> 3.12.0)
14
+ rspec-expectations (~> 3.12.0)
15
+ rspec-mocks (~> 3.12.0)
16
+ rspec-core (3.12.1)
17
+ rspec-support (~> 3.12.0)
18
+ rspec-expectations (3.12.2)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.12.0)
21
+ rspec-mocks (3.12.3)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.12.0)
24
+ rspec-support (3.12.0)
25
+ thor (1.2.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ rake (~> 12.0)
32
+ random_gem!
33
+ rspec (~> 3.0)
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 a5-stable
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,59 @@
1
+ # RandomGem
2
+
3
+ RandomGem is a Ruby gem that selects a random gem from RubyGems and returns its name.
4
+ Also, it provides a command to add a random gem to your Gemfile.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'random_gem'
12
+ ```
13
+
14
+
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install random_gem
23
+
24
+ ## Usage
25
+
26
+ ### Selecting a random gem
27
+
28
+ `RandomGem.pick` returns the info of a random gem.
29
+
30
+ ```ruby
31
+ require 'random_gem'
32
+
33
+ RandomGem.pick
34
+ #=> { "documentation_uri"=>"XXXXX", ..., "name"=>"XXXX", "version_downloads"=>0, "info"=>"XXXX", "authors"=>"XXXX" }
35
+ ```
36
+ ### Adding a gem to your Gemfile
37
+ This command will add a random gem to your Gemfile on a new line.
38
+ ```sh
39
+ bundle exec random_gem add
40
+ ```
41
+
42
+ ## Development
43
+
44
+ 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.
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/a5-stable/random_gem.
49
+
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ ## Warning
56
+ When installing an unknown gem, it is important to verify that it is safe to use. While RandomGem selects random gems from RubyGems, it is not responsible for any issues that may arise from installing these gems. Please use this gem at your own risk and carefully consider the potential risks of installing unknown gems.
57
+
58
+ ## Notes
59
+ RandomGem relies on the RubyGems API, so please ensure that you have a stable internet connection before using this gem.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "random_gem"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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/exe/random_gem ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "random_gem"
4
+
5
+ RandomGem::CLI.start
@@ -0,0 +1,21 @@
1
+ require 'thor'
2
+ require 'random_gem'
3
+
4
+ module RandomGem
5
+ class CLI < Thor
6
+ include Thor::Actions
7
+ desc "add", "add a gem to your Gemfile randomly"
8
+
9
+ def add
10
+ gem = RandomGem.pick
11
+ gem_name = gem["name"]
12
+
13
+ File.open("Gemfile", "a") do |file|
14
+ file.puts("gem '#{gem_name}'")
15
+ end
16
+
17
+ puts "Yes! Gem '#{gem_name}' has been added to your Gemfile. For more information, see #{gem["documentation_uri"]}."
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,39 @@
1
+ require "random_gem/request"
2
+ require 'timeout'
3
+
4
+ module RandomGem
5
+ class Randomizer
6
+ class Error < Exception; end
7
+ TIMEOUT = 30.freeze
8
+ PAGE = 100.freeze
9
+
10
+ def perform
11
+ gems = Timeout.timeout(TIMEOUT, RandomGem::Randomizer::Error) { random_pick_loop }
12
+ pick_single_gem(gems: gems)
13
+ end
14
+
15
+ private
16
+
17
+ def random_pick_loop
18
+ loop do
19
+ request = RandomGem::Request.new(keyword: random_letter, page: random_index(max: PAGE))
20
+ gems = request.do
21
+
22
+ break gems if gems.length > 0
23
+ end
24
+ end
25
+
26
+ def pick_single_gem(gems:)
27
+ index = random_index(max: gems.length)
28
+ gems[index]
29
+ end
30
+
31
+ def random_letter
32
+ ('a'..'z').to_a.sample
33
+ end
34
+
35
+ def random_index(max:)
36
+ rand(max) - 1
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+ require 'rubygems'
3
+ require 'open-uri'
4
+ require 'json'
5
+
6
+ module RandomGem
7
+ class Request
8
+ HOST = "https://rubygems.org"
9
+ PATH = "/api/v1/search"
10
+ attr_reader :keyword, :page
11
+
12
+ def initialize(keyword:, page:)
13
+ @keyword = keyword
14
+ @page = page
15
+ end
16
+
17
+ def do
18
+ uri = URI("#{HOST}#{PATH}")
19
+ params = { query: keyword, page: page }
20
+ uri.query = URI.encode_www_form(params)
21
+
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.use_ssl = true
24
+
25
+ res = http.get(uri.request_uri)
26
+ respond(res: res)
27
+ end
28
+
29
+ private
30
+
31
+ def respond(res:)
32
+ JSON.parse(res.body)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module RandomGem
2
+ VERSION = "0.1.0"
3
+ end
data/lib/random_gem.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "random_gem/cli"
2
+ require "random_gem/randomizer"
3
+ require "random_gem/version"
4
+
5
+ module RandomGem
6
+ class Error < StandardError
7
+ def initialize(msg: nil)
8
+ msg ||= "An unexpected error occured. Please try again later."
9
+ super(msg)
10
+ end
11
+ end
12
+
13
+ def self.pick
14
+ Randomizer.new.perform
15
+ rescue Randomizer::Error
16
+ raise Error.new
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/random_gem/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "random_gem"
5
+ spec.version = RandomGem::VERSION
6
+ spec.authors = ["a5-stable"]
7
+ spec.email = ["sh07e1916@gmail.com"]
8
+
9
+ spec.summary = "pick up a random gem from rubygems"
10
+ spec.description = "RandomGem is a Ruby gem that selects a random gem from RubyGems and returns its name. Also, it provides a command to add a random gem to your Gemfile."
11
+ spec.homepage = "https://github.com/a5-stable/random_gem"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/a5-stable/random_gem"
17
+ spec.metadata["changelog_uri"] = "https://github.com/a5-stable/random_gem"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "thor"
29
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - a5-stable
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: RandomGem is a Ruby gem that selects a random gem from RubyGems and returns
28
+ its name. Also, it provides a command to add a random gem to your Gemfile.
29
+ email:
30
+ - sh07e1916@gmail.com
31
+ executables:
32
+ - random_gem
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".travis.yml"
39
+ - CODE_OF_CONDUCT.md
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/setup
47
+ - exe/random_gem
48
+ - lib/random_gem.rb
49
+ - lib/random_gem/cli.rb
50
+ - lib/random_gem/randomizer.rb
51
+ - lib/random_gem/request.rb
52
+ - lib/random_gem/version.rb
53
+ - random_gem.gemspec
54
+ homepage: https://github.com/a5-stable/random_gem
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/a5-stable/random_gem
59
+ source_code_uri: https://github.com/a5-stable/random_gem
60
+ changelog_uri: https://github.com/a5-stable/random_gem
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.3.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.0.3.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: pick up a random gem from rubygems
80
+ test_files: []