require_relative 'exceptions' module Rocktumbler class Gem def initialize(gem_dependency) @gem_dependency = gem_dependency end def print(prepend="") gem_info = retrieve_gem_information(@gem_dependency.name) str = "#{prepend}# #{gem_info.summary}\n" str += "#{prepend}# #{gem_info.homepage}\n" str += "#{prepend}gem '#{@gem_dependency.name}'" str += ",'#{@gem_dependency.requirement}'" unless @gem_dependency.requirement.none? str += generate_gem_source(@gem_dependency.source.options) if @gem_dependency.source str += generate_require(@gem_dependency.autorequire) if @gem_dependency.autorequire str += "\n\n" end private def generate_require(autorequire) if autorequire.count > 1 # Array.to_s will output double quotes so we gsub them to singles. return ", require: #{autorequire.to_s.gsub(/"/,'\'')}" elsif autorequire.first.is_a?(String) return ", require: '#{autorequire.first}'" else return ", require: #{!autorequire.empty?}" end end def generate_gem_source(source_options) ['git','github','git_source','source','path','gist','bitbucket'].each do | source_type | return ", #{source_type}: '#{source_options[source_type]}'" if source_options[source_type] end end def retrieve_gem_information(gem_name) bundled_gem = Bundler.definition.specs.select{|g| g.name == gem_name}.first if bundled_gem OpenStruct.new(summary: bundled_gem.summary.slice(0,80), homepage: bundled_gem.homepage) else rubygems_gem_information(gem_name) end end def rubygems_gem_information(gem_name) begin json = open("https://rubygems.org/api/v1/gems/#{gem_name}.json").read gem_info = JSON.parse(json) OpenStruct.new(summary: gem_info['info'].slice(0,80), homepage: gem_info['homepage_uri']) rescue OpenURI::HTTPError => e if e.message == '404 Not Found' raise GemInformationNotFoundError, "No details found locally or on rubygems for '#{gem_name}'" else raise e end end end end end