class Ana::Command

Public Instance Methods

download(gem) click to toggle source
# File lib/ana/command.rb, line 150
def download(gem)
  return if gem_does_not_exist?(gem)
  open gem, 'lib'
end
find_version(gem, ver='no-input') click to toggle source
# File lib/ana/command.rb, line 100
def find_version(gem, ver='no-input')
  return if gem_does_not_exist?(gem)
  if ver == 'no-input'
    say 'Please specify a version'
    return
  end
  gem_hash = get_gem_json!(gem, type: 'versions')
  versions = gem_hash.collect { |x| x['number'] }
  if versions.include? ver
    gem_infos gem
  else
    print_gem_version_not_found!
  end
end
gem_dependencies(gem, type='runtime') click to toggle source
# File lib/ana/command.rb, line 62
def gem_dependencies(gem, type='runtime')
  return if gem_does_not_exist?(gem)
  gem_hash = get_gem_json!(gem, type: 'gems')
  gem_hash['dependencies'][type].each do |arr|
    puts "#{arr['name'].ljust(20)} #{arr['requirements']}"
  end
end
gem_infos(gem) click to toggle source
# File lib/ana/command.rb, line 44
def gem_infos(gem)
  return if gem_does_not_exist?(gem)
  gem_hash = get_gem_json!(gem, type: 'gems')
  say gem_hash['info']
  say "Has been downloaded for #{colorize(gem_hash['downloads'], RED)} times!"
  say "The Latest version is #{colorize(gem_hash['version'], BLUE)}."
  say "Respectful Author(s): #{colorize(gem_hash['authors'], YELLOW)}."
  if gem_hash['licenses'][0].nil?
    say "NO LICENSE :P"
  else
    say "LICENSE under #{colorize(gem_hash['licenses'][0], MAGENTA)}"
  end
end
init() click to toggle source
# File lib/ana/command.rb, line 32
def init
  empty_directory full_path('~/.gemjsons')
end
latest_version(gem) click to toggle source
# File lib/ana/command.rb, line 72
def latest_version(gem)
  return if gem_does_not_exist?(gem)
  gem_hash = get_gem_json!(gem, type: 'gems')
  say("Latest version is #{gem_hash['version']}.", :blue) if gem_hash
end
open(gem, open_type='home') click to toggle source
# File lib/ana/command.rb, line 133
def open(gem, open_type='home')
  return if gem_does_not_exist?(gem)
  gem_hash = get_gem_json!(gem, type: 'gems')
  url = if URI_TYPE.keys.include? open_type
          skip = false
          gem_hash[URI_TYPE[open_type]]
        else
          say "#{open_type} is not a valid type :( \n"
          print_valid_uri_open_types!
          skip = true
        end
  Launchy.open(url) if url.present? && skip == false
  say "Nothing to open, this gem probably does not have #{open_type} page" if url.blank? || skip
end
version() click to toggle source
# File lib/ana/command.rb, line 37
def version
  say "#{Ana::VERSION::STRING}"
end
versions(gem, count=10) click to toggle source
# File lib/ana/command.rb, line 81
def versions(gem, count=10)
  if number?(count.to_s) == false
    say "#{count} is not a number :("
    return
  end
  return if gem_does_not_exist?(gem)
  gem_hash = get_gem_json!(gem, type: 'versions')
  if count == 'all' || count.to_i > gem_hash.count
    count = gem_hash.count
  end
  say("Last #{count} versions of #{gem} are...")
  [*0..count.to_i-1].each do |n|
    say("#{gem_hash[n]['built_at'][0..9]} : #{gem_hash[n]['number']}")
  end
end

Private Instance Methods

access_time(file_path) click to toggle source

Return a file’s change time with respect to current time.

# File lib/ana/command.rb, line 256
def access_time(file_path)
  Time.now - File.ctime(file_path)
end
colorize(text, color_code) click to toggle source

Add color to terminal text. Available colors could be found in Ana::Scalars. 033 is 100% POSIX compatible. Use e is also fine.

# File lib/ana/command.rb, line 160
def colorize(text, color_code)
  "\033[#{color_code}m#{text}\033[0m"
end
full_path(relative_path) click to toggle source

Expand the full path of given relative path.

# File lib/ana/command.rb, line 246
def full_path(relative_path)
  File.expand_path(relative_path)
end
gem_does_not_exist?(gem) click to toggle source

Returns true if given gem does not exist

# File lib/ana/command.rb, line 211
def gem_does_not_exist?(gem)
  !gem_exist?(gem)
end
gem_exist?(gem) click to toggle source

Returns true if given gem exists.

# File lib/ana/command.rb, line 191
def gem_exist?(gem)
  uri = URI.parse(URI.encode("https://rubygems.org/api/v1/gems/#{gem}.json", "[]"))
  response = Net::HTTP.get(uri)
  if response.include? 'page not found'
    say "#{gem}, typo?"
    return false
  end
  case response
  when GEM_NOT_FOUND
    print_gem_not_found!
    return false
  when GEM_DOES_NOT_EXIST
    print_gem_does_not_exist!
    return false
  else
    return true
  end
end
get_gem_json!(gem, type: 'versions') click to toggle source

Download json if it hasn’t been downloaded or older than 900s. return Hash parsed from gem’s (gems / version) json

# File lib/ana/command.rb, line 177
def get_gem_json!(gem, type: 'versions')
  gem_json_file_path = full_path("~/.gemjsons/#{gem}/#{type}.json")
  gem_uri = URI.parse(URI.encode("https://rubygems.org/api/v1/#{type}/#{gem}.json", "[]"))
  if !File.exist?(gem_json_file_path)
    create_file(gem_json_file_path, Net::HTTP.get(gem_uri), verbose: false)
  else
    if not_fresh?(gem_json_file_path)
      remove_and_create_file!(gem_json_file_path, Net::HTTP.get(gem_uri))
    end
  end
  return JSON.parse(IO.read(gem_json_file_path)) || nil
end
not_fresh?(file_path) click to toggle source

Check if a file is has been changed within 900s?

# File lib/ana/command.rb, line 251
def not_fresh?(file_path)
  access_time(file_path) > TTL
end
number?(str) click to toggle source
# File lib/ana/command.rb, line 215
def number?(str)
  return false if(str =~ /\A[-+]?[0-9]*\.?[0-9]+\z/).nil?
  true
end
print_gem_does_not_exist!() click to toggle source

Print Gem does not exist message.

print_gem_not_found!() click to toggle source

Print Gem not found message.

print_gem_version_not_found!() click to toggle source

Print Gem version not found message.

print_valid_uri_open_types!() click to toggle source

Print all valid URI types.

remove_and_create_file!(file_path, data) click to toggle source

Remove and create (update) a file.

# File lib/ana/command.rb, line 236
def remove_and_create_file!(file_path, data)
  if File.exist? file_path
    remove_file(file_path, verbose: false)
    create_file(file_path, data, verbose: false)
  else
    create_file(file_path, data, verbose: false)
  end
end