hottest_gems 0.1.6

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
+ SHA1:
3
+ metadata.gz: 7cef6540d3fb08da9dc21d8ab1c8d3cc67fa14c3
4
+ data.tar.gz: 4e988a70df3f5c21001d8fafdc4d102a1b2e3c8e
5
+ SHA512:
6
+ metadata.gz: 9362a9d3baeae8258a02c33b1e14ad4bba0f13880c849536eb9f689cd4277b0a779b253c356a2b654311074989a02ffdc9f4075cc0a44ddc3525cc500286d276
7
+ data.tar.gz: c5329c2a349952f7260fafc5dbbdcd4bb5a0a5d7c527b6089929e4927526add66ebb29162b4ff647553caa1a1e9f6ad639cec8de946c1dcca4aec49ec1b4d719
data/bin/hottest_gems ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/hottest_gems'
4
+
5
+ HottestGems::CLI.new.call
@@ -0,0 +1,11 @@
1
+ require 'pry'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'colorize'
5
+ require 'openssl'
6
+
7
+ module HottestGems
8
+ end
9
+
10
+ require_relative 'hottest_gems/cli'
11
+ require_relative 'hottest_gems/gem'
@@ -0,0 +1,35 @@
1
+ class HottestGems::CLI
2
+ def call
3
+ main
4
+ end
5
+
6
+ def main
7
+ print_list
8
+ input = nil
9
+ until input == "exit"
10
+ puts ""
11
+ puts "Which hottest gem do you want to meet?"
12
+ puts ""
13
+ puts "Enter list to see the gems again."
14
+ puts "Enter exit to end the program."
15
+ puts ""
16
+ input = gets.strip
17
+ if input == "list"
18
+ print_list
19
+ elsif input.to_i > 0
20
+ HottestGems::Gem.gem_at(input.to_i - 1).print_details
21
+ end
22
+ end
23
+ end
24
+
25
+ def print_list
26
+ puts ""
27
+ puts "ALL TIME MOST DOWNLOADED".red
28
+ puts ""
29
+ HottestGems::Gem.all.each_with_index do |gem, i|
30
+ puts " #{i + 1}. #{gem.name.capitalize}".blue
31
+ puts " Total Downloads: #{gem.total_downloads}".red
32
+ end
33
+ puts ""
34
+ end
35
+ end
@@ -0,0 +1,95 @@
1
+ class HottestGems::Gem
2
+ attr_reader :name, :version, :total_downloads, :version_downloads
3
+
4
+ def initialize(name, url)
5
+ @name = name
6
+ @url = url
7
+ end
8
+
9
+ def self.all
10
+ @@all ||= scrape_list
11
+ end
12
+
13
+ def self.total_gems
14
+ scrape_now_data(0)
15
+ end
16
+
17
+ def self.total_users
18
+ scrape_now_data(1)
19
+ end
20
+
21
+ def self.total_downloads
22
+ scrape_now_data(2)
23
+ end
24
+
25
+ def self.gem_at(index)
26
+ self.all[index]
27
+ end
28
+
29
+ def print_details
30
+ puts ""
31
+ puts "#{self.name.capitalize}".blue
32
+ puts " Version: #{self.version}"
33
+ puts " Total Downloads: #{self.total_downloads}"
34
+ puts " Version Downloads: #{self.version_downloads}"
35
+ puts ""
36
+ end
37
+
38
+ def version
39
+ @version ||= scrape_data(".page__subheading")
40
+ end
41
+
42
+ def total_downloads
43
+ @total_downloads ||= scrape_data(".gem__downloads")
44
+ end
45
+
46
+ def version_downloads
47
+ @version_downloads ||= scrape_data(".gem__downloads", 1)
48
+ end
49
+
50
+ private
51
+ def self.scrape_list
52
+ status = [0, "false"]
53
+ unless status[1] == "true" || status[0] > 3
54
+ begin
55
+ doc = Nokogiri::HTML(open('https://rubygems.org/stats', ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE))
56
+ status[1] == "true"
57
+ rescue
58
+ puts "Load fail, retry..."
59
+ status[0] += 1
60
+ end
61
+ end
62
+ doc.css(".stats__graph__gem__name a").collect do |a|
63
+ new(a.text.strip, "https://rubygems.org" + a.attribute("href").value.strip)
64
+ end
65
+ end
66
+
67
+ def self.scrape_now_data(index)
68
+ status = [0, "false"]
69
+ unless status[1] == "true" || status[0] > 3
70
+ begin
71
+ result = Nokogiri::HTML(open('https://rubygems.org/stats', ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE)).css(".stat .stat__count")[index].text.strip
72
+ status[1] == "true"
73
+ rescue
74
+ puts "Load fail, retry..."
75
+ status[0] += 1
76
+ end
77
+ end
78
+ result
79
+ end
80
+
81
+ def scrape_data(css_selector, index = 0)
82
+ status = [0, "false"]
83
+ unless status[1] == "true" || status[0] > 3
84
+ begin
85
+ result = Nokogiri::HTML(open(@url, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE)).css(css_selector)[index].text.strip
86
+ status[1] == "true"
87
+ rescue
88
+ puts "Load fail, retry..."
89
+ status[0] += 1
90
+ end
91
+ end
92
+ result
93
+ end
94
+
95
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hottest_gems
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Mondo Gao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple CLI displaying the hottest gems on https://rubygems.org/stats
14
+ email: mondogao@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - bin/hottest_gems
20
+ - lib/hottest_gems.rb
21
+ - lib/hottest_gems/cli.rb
22
+ - lib/hottest_gems/gem.rb
23
+ homepage:
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.8
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: I want to marry with the HOTTEST GEM!
47
+ test_files: []