gem-check 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0cd75e58071bfb04722ffc292166cb654ea869f
4
- data.tar.gz: d9145f2303bf8e2e7752d86d04d6f49b4a239f77
3
+ metadata.gz: 7cd38462be68a3f95d24c40ac3750a1384d4452e
4
+ data.tar.gz: f8f1c6e79201f42d897004513d6b046d7265ef2d
5
5
  SHA512:
6
- metadata.gz: 6090011f1a3972b0cd847e55269cd50f40b603f98a3c47e6633c19754f9e6c3fac45c7bbf9cbd141ef3ff60a754badf9cb501e05a0c09fe24092ca8684398528
7
- data.tar.gz: 2e237ebb9ff07d67eb0f25c44402fe7fc554b292c1aecfb1b6c12f6dbba41bd15407869f9e1041ad89dc8110946f8b4b0622c9c69cd2e725325402f7a100c648
6
+ metadata.gz: 67cfb2c72b1dd273d1058423a84f520764710b60bff70b84d1446b629f17b8cfef8bf0eae2c1f11ab9b167c064314c682f9a03c0f77cf95825e7a78227bc87ef
7
+ data.tar.gz: 02627224be8dcb473e01c0f711a28bb5b1583bd292264dc1255e277045f66c403b14dbf994addc0bb365fc30438edbb383d5d2428ace239aff68cad521685ffc
data/README.md CHANGED
@@ -9,11 +9,31 @@ A simple ruby utility to see the download count for your owned gems.
9
9
 
10
10
  ## Usage
11
11
 
12
- Create your gem credentials file as described on [rubygems.org](https://rubygems.org/profile/edit)
12
+ First, create your gem credentials file as described on [rubygems.org](https://rubygems.org/profile/edit)
13
13
  ```
14
14
  $ gem-check
15
15
  ```
16
- ![gem-check](./screenshot-gem-check.png)
16
+ ![gem-check](./gem-check.png)
17
+
18
+ ### Options
19
+
20
+ | Flag | Description|
21
+ | :--- | :---: |
22
+ | -f | Don't save the updated stats for this run. |
23
+ | -h | Display the help screen. |
24
+
25
+ ### Note
26
+ Gem stats data stored in ~/gem-check.json
27
+
28
+ ### Display
29
+
30
+ | Column | Description |
31
+ | :--- | :---: |
32
+ | Gem Version | Latest available gem version |
33
+ | Version DLs | Download count for the displayed version of the gem |
34
+ | All DLs | Total gem download count |
35
+ | (+number) | New downloads since last recorded program run |
36
+
17
37
 
18
38
  ## Dependencies
19
39
  gems ~> 0.8.3
data/bin/gem-check CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  require_relative '../lib/gem-check'
3
3
  include GemCheck
4
-
5
- print_table
4
+ if ARGV.include?("-h")
5
+ Checker.show_help
6
+ else
7
+ Checker.new(ARGV).check
8
+ end
data/gem-check.png ADDED
Binary file
@@ -0,0 +1,56 @@
1
+ module GemCheck
2
+ class Checker
3
+ def self.show_help
4
+ puts
5
+ puts "GemCheck v#{GemCheck::VERSION} - Help"
6
+ puts "\n Flags:\n"
7
+ puts " -f Don't save the updated stats for this run."
8
+ puts " -h Display this help screen."
9
+ puts
10
+ end
11
+
12
+ private
13
+
14
+ def commafy(x)
15
+ i,d = x.to_s.split('.')
16
+ i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (d ? ('.'+d) : '')
17
+ end
18
+
19
+ def decommafy(x)
20
+ return x unless x.class.eql?(String)
21
+ x.delete(',')
22
+ end
23
+
24
+ def update_status_string
25
+ update_available? ? "\n [ Update available: v#{@update_version} ]" : nil
26
+ end
27
+
28
+ def header_string
29
+ "GemCheck v#{GemCheck::VERSION}#{update_status_string}\n"+
30
+ "#{current_date_string}\n"+
31
+ "#{last_run}"
32
+ end
33
+
34
+ # adapted from https://stackoverflow.com/a/195894
35
+ def last_run
36
+ time_diff = @previous_date ? (Time.now - @previous_date).to_i : nil
37
+ time_words =
38
+ case time_diff
39
+ when nil then nil
40
+ when 0 then 'just now'
41
+ when 1 then 'a second ago'
42
+ when 2..59 then time_diff.to_s+' seconds ago'
43
+ when 60..119 then 'a minute ago' #120 = 2 minutes
44
+ when 120..3540 then (time_diff/60).to_i.to_s+' minutes ago'
45
+ when 3541..7100 then 'an hour ago' # 3600 = 1 hour
46
+ when 7101..82800 then ((time_diff+99)/3600).to_i.to_s+' hours ago'
47
+ when 82801..172000 then 'a day ago' # 86400 = 1 day
48
+ when 172001..518400 then ((time_diff+800)/(60*60*24)).to_i.to_s+' days ago'
49
+ when 518400..1036800 then 'a week ago'
50
+ else ((time_diff+180000)/(60*60*24*7)).to_i.to_s+' weeks ago'
51
+ end
52
+
53
+ time_words ? "#{commafy(@new_downloads)} downloads since #{time_words}" : ''
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,64 @@
1
+ module GemCheck
2
+ class Checker
3
+ private
4
+
5
+ # File I/O
6
+ def clean_stat_line(line)
7
+ chars_to_filter = %w(\\\" [ ])
8
+ line.chomp!
9
+ chars_to_filter.each { |c| line.delete!(c) }
10
+ line.split(',')
11
+ end
12
+
13
+ def current_date_string
14
+ Time.now.strftime("%b %d, %H:%M:%S")
15
+ end
16
+
17
+ # Gems API
18
+ def current_gem_info
19
+ @current_stats = []
20
+ Gems.gems.map do |x|
21
+ x = OpenStruct.new(x)
22
+ val = [x.name, x.version, commafy(x.version_downloads), commafy(x.downloads)]
23
+ @current_stats << val
24
+ val
25
+ end
26
+ end
27
+
28
+ # Features
29
+ def add_new_download_info(latest_info)
30
+ return latest_info if @previous_stats.empty?
31
+ latest_info.map do |row|
32
+ prev_info = search_by_column(@previous_stats, COL_NAME, row[COL_NAME])
33
+ prev_info = search_by_column(prev_info, COL_VER, row[COL_VER])
34
+ return row if prev_info.empty?
35
+ prev_info = prev_info.first
36
+ calc_new_downloads(COL_TDL, row, prev_info)
37
+ calc_new_downloads(COL_VDL, row, prev_info)
38
+ row
39
+ end
40
+ end
41
+
42
+ def calc_new_downloads(idx, row, info)
43
+ new_dls = decommafy(row[idx]).to_i - decommafy(info[idx]).to_i
44
+ @new_downloads += new_dls
45
+ row[idx] = new_dls > 0 ? "#{row[idx]} ( +#{commafy(new_dls)} )" : row[idx]
46
+ end
47
+
48
+ # Utils
49
+ def search_by_column(table, column, search_s)
50
+ return table if table.empty?
51
+ table.select{ |r| r[column].eql?(search_s) }
52
+ end
53
+
54
+ # Maintanence
55
+ def update_available?
56
+ versions = open(URL_GEMCHECK_INFO_JSON).read
57
+ @update_version = JSON.parse(versions, object_class: OpenStruct).first.number
58
+ @update_version > GemCheck::VERSION
59
+ end
60
+
61
+
62
+
63
+ end
64
+ end
@@ -1,3 +1,3 @@
1
1
  module GemCheck
2
- VERSION = "0.1.2"
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/gem-check.rb CHANGED
@@ -2,35 +2,78 @@ require 'rubygems'
2
2
  require 'ostruct'
3
3
  require 'gems'
4
4
  require 'terminal-table'
5
- require_relative 'version'
5
+ require 'open-uri'
6
+ require 'json'
7
+ require 'date'
8
+ require_relative 'gem-check/version'
9
+ require_relative 'gem-check/cli'
10
+ require_relative 'gem-check/helpers'
6
11
 
7
12
  module GemCheck
8
- HEADINGS = ['Gem Name', 'Version', 'Downloads', 'Total']
9
13
 
10
- def commafy(x)
11
- i,d = x.to_s.split('.')
12
- i.gsub(/(\d)(?=\d{3}+$)/,'\\1,') + (d ? ('.'+d) : '')
13
- end
14
+ class Checker
15
+ URL_GEMCHECK_INFO_JSON = 'https://rubygems.org/api/v1/versions/gem-check.json'
16
+ INFO_FILENAME = 'gem-check.json'
17
+
18
+ REPORT_HEADINGS = ['Gem Name', 'Gem Version', 'Version DLs', 'All DLs']
19
+ COL_NAME = 0 # Gem Name
20
+ COL_VER = 1 # Version
21
+ COL_VDL = 2 # Version DLs
22
+ COL_TDL = 3 # Total DLs
14
23
 
15
- def gem_info
16
- Gems.gems.map do |x|
17
- x = OpenStruct.new(x)
18
- [x.name, x.version, commafy(x.version_downloads), commafy(x.downloads)]
24
+ def initialize(args=[])
25
+ @new_downloads = 0
26
+ @update_version = nil
27
+ @current_stats = nil
28
+ @previous_stats = []
29
+ @previous_date = Time.now
30
+ @f_previous_stats = File.join(Dir.home, INFO_FILENAME)
31
+ @args = args
19
32
  end
20
- end
21
33
 
22
- def build_table
23
- rows = gem_info.sort!{|a, b| b[2].to_i <=> a[2].to_i}
24
- table = Terminal::Table.new(rows: rows)
25
- table.headings = GemCheck::HEADINGS
26
- [1].each{ |col| table.align_column(col, :center) }
27
- [2,3].each{ |col| table.align_column(col, :right) }
28
- table
29
- end
34
+ def check
35
+ read_previous_info
36
+ print_table(build_table)
37
+ store_new_info unless @args.include?('-f')
38
+ end
39
+
40
+ # Read stored gem info
41
+ def read_previous_info
42
+ return unless File.exist? @f_previous_stats
43
+ f = File.read(@f_previous_stats)
44
+ @previous_date = Time.parse(f.lines.first.chomp) || nil
45
+ @previous_stats = []
46
+ f.lines.drop(1).each { |line| @previous_stats << clean_stat_line(line)}
47
+ @previous_stats
48
+ end
49
+
50
+ def build_table
51
+ rows = current_gem_info.sort!{|a, b| b[2].to_i <=> a[2].to_i}
52
+ rows = add_new_download_info(rows)
53
+ table = Terminal::Table.new(rows: rows)
54
+ table.title = header_string
55
+ table.headings = REPORT_HEADINGS
56
+ [1 ].each{ |col| table.align_column(col, :left) }
57
+ [2,3].each{ |col| table.align_column(col, :center) }
58
+ table
59
+ end
30
60
 
31
- def print_table
32
- puts
33
- puts build_table
34
- puts
61
+ # Display
62
+ def print_table(table)
63
+ puts "\n#{table}\n\n"
64
+ end
65
+
66
+ # Save newest gem info to file @f_previous_stats
67
+ def store_new_info
68
+ File.open(@f_previous_stats, 'w') do |file|
69
+ file.write "#{current_date_string}\n"
70
+ @current_stats.each do |row|
71
+ row.each_with_index do |col, idx|
72
+ row[idx] = decommafy(row[idx]).to_i if idx > 1
73
+ end
74
+ file.write row.to_json + "\n"
75
+ end
76
+ end
77
+ end
35
78
  end
36
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Meissa Dia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-04 00:00:00.000000000 Z
11
+ date: 2017-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gems
@@ -48,9 +48,11 @@ extra_rdoc_files: []
48
48
  files:
49
49
  - README.md
50
50
  - bin/gem-check
51
+ - gem-check.png
51
52
  - lib/gem-check.rb
52
- - lib/version.rb
53
- - screenshot-gem-check.png
53
+ - lib/gem-check/cli.rb
54
+ - lib/gem-check/helpers.rb
55
+ - lib/gem-check/version.rb
54
56
  homepage: https://github.com/meissadia/gem-check
55
57
  licenses:
56
58
  - MIT
@@ -71,9 +73,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
73
  version: '0'
72
74
  requirements: []
73
75
  rubyforge_project:
74
- rubygems_version: 2.4.5.1
76
+ rubygems_version: 2.6.10
75
77
  signing_key:
76
78
  specification_version: 4
77
79
  summary: See the download counts of your owned gems.
78
80
  test_files: []
79
- has_rdoc:
Binary file