Skip to content

Commit

Permalink
Merge pull request #3 from apainintheneck/add-versions-sub-view
Browse files Browse the repository at this point in the history
Add versions sub view
  • Loading branch information
apainintheneck authored Dec 7, 2024
2 parents 134ea14 + fbef54b commit 535811f
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/gemview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module Gemview
class Error < StandardError; end

# Internal
autoload :Client, "gemview/client"
autoload :Commands, "gemview/commands"
autoload :Gem, "gemview/gem"
autoload :GitRepo, "gemview/git_repo"
autoload :Number, "gemview/number"
autoload :Terminal, "gemview/terminal"
autoload :Version, "gemview/version"
autoload :View, "gemview/view"
Expand Down
23 changes: 23 additions & 0 deletions lib/gemview/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Gemview
module Client
# Create a client manually so that we don't accidentally pick up credentials.
def self.v1
@client_v1 ||= Gems::V1::Client.new(
username: nil,
password: nil,
key: nil
)
end

# Create a client manually so that we don't accidentally pick up credentials.
def self.v2
@client_v2 ||= Gems::V2::Client.new(
username: nil,
password: nil,
key: nil
)
end
end
end
72 changes: 43 additions & 29 deletions lib/gemview/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,25 @@ def to_str
attribute :runtime, Types::Strict::Array.of(Dependency)
end

class Version < Dry::Struct
transform_keys(&:to_sym)

attribute :number, Types::Strict::String
alias_method :version, :number

attribute :downloads_count, Types::Strict::Integer
alias_method :downloads, :downloads_count

attribute :created_at, Types::Params::Time

# @return [Date]
def release_date = created_at.to_date
end

# Ex. 1234567890 -> "1,234,567,890"
# @return [String]
def humanized_downloads
downloads
.to_s
.chars
.reverse
.each_slice(3)
.map(&:join)
.join(",")
.reverse
Number.humanized_integer(downloads)
end

# @return [String]
Expand Down Expand Up @@ -124,6 +132,23 @@ def dependencies_str
Terminal.prettify_markdown(dependencies)
end

def versions_str
rows = self.class.versions(name: name).map do |version|
pretty_downloads = Number.humanized_integer(version.downloads)
"| #{version.release_date} | #{version.version} | #{pretty_downloads} |"
end

table = <<~TABLE
## [Versions]
| *Release Date* | *Version* | *Downloads* |
|----------------|-----------|-------------|
#{rows.join("\n")}
TABLE

Terminal.prettify_markdown(table)
end

# @return [Array<String>]
def urls
[
Expand Down Expand Up @@ -152,44 +177,33 @@ def self.find(name:, version: nil)
@find ||= {}
@find[[name, version]] ||= new case version
when String
client_v2.info(name, version)
Client.v2.info(name, version)
else
client_v1.info(name)
Client.v1.info(name)
end
end

# @param term [String] search term
# @return [Array<Gemview::Gem>]
def self.search(term:)
client_v1.search(term).map { |gem_hash| new gem_hash }
Client.v1.search(term).map { |gem_hash| new gem_hash }
end

# @return [Array<Gemview::Gem>]
def self.latest
client_v1.latest.map { |gem_hash| new gem_hash }
Client.v1.latest.map { |gem_hash| new gem_hash }
end

# @return [Array<Gemview::Gem>]
def self.just_updated
client_v1.just_updated.map { |gem_hash| new gem_hash }
end

# Create a client manually so that we don't accidentally pick up credentials.
def self.client_v1
@client_v1 ||= Gems::V1::Client.new(
username: nil,
password: nil,
key: nil
)
Client.v1.just_updated.map { |gem_hash| new gem_hash }
end

# Create a client manually so that we don't accidentally pick up credentials.
def self.client_v2
@client_v2 ||= Gems::V2::Client.new(
username: nil,
password: nil,
key: nil
)
# @param name [String] gem name
# @return [Array<Gemview::Gem::Version>]
def self.versions(name:)
@versions ||= {}
@versions[name] ||= Client.v1.versions(name).map { |gem_hash| Version.new gem_hash }
end
end
end
19 changes: 19 additions & 0 deletions lib/gemview/number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Gemview
module Number
# Ex. 1234567890 -> "1,234,567,890"
# @param integer [Integer]
# @return [String]
def self.humanized_integer(integer)
integer
.to_s
.chars
.reverse
.each_slice(3)
.map(&:join)
.join(",")
.reverse
end
end
end
4 changes: 3 additions & 1 deletion lib/gemview/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ def self.info(gem:)
More info:
PROMPT

Terminal.choose(prompt, %w[Readme Changelog Dependencies]) do |choice|
Terminal.choose(prompt, %w[Readme Changelog Dependencies Versions]) do |choice|
case choice
when "Readme"
Terminal.page([gem.header_str, gem.fetch_readme].join("\n"))
when "Changelog"
Terminal.page([gem.header_str, gem.fetch_changelog].join("\n"))
when "Dependencies"
Terminal.page([gem.header_str, gem.dependencies_str].join("\n"))
when "Versions"
Terminal.page([gem.header_str, gem.versions_str].join("\n"))
else
raise ArgumentError, "Unknown choice: #{choice}"
end
Expand Down
Loading

0 comments on commit 535811f

Please sign in to comment.