Sha256: 4a9f339c57b1a9f8ef5af33dff51523532c15bf7a4a3ad338d7f1a312b22772b

Contents?: true

Size: 1.03 KB

Versions: 9

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

# Retrieve GemInfo from RubyGems.org
module RailsAppGenerator
  # GemQuery queries RubyGems for GemEntry.
  class GemQuery
    class << self
      def get(name)
        gem_info = query_ruby_gems(name)

        return gem_info if gem_info

        return query_ruby_gems(name.gsub('_', '-')) if name.include?('_')
        return query_ruby_gems(name.gsub('-', '_')) if name.include?('-')

        abort "Cannot find GEM (#{name}) on RubyGems.org"
      end

      private

      def query_ruby_gems(name)
        link = "https://rubygems.org/api/v1/gems/#{name.downcase}.json"
        info = Net::HTTP.get(URI.parse(link))
        json = JSON.parse(info)

        comment = (json['description'] || json['info'] || '').gsub(/(\r?\n|\r)/, ' ').squeeze.strip

        Rails::Generators::AppBase::GemfileEntry.new(json['name'], json['version'], comment)
      rescue SocketError
        abort 'Internet connection cannot be established to RubyGems.org'
      rescue JSON::ParserError
        nil
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails_app_generator-0.3.6 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.3.5 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.3.4 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.3.3 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.3.2 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.3.1 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.3.0 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.2.43 lib/rails_app_generator/gem_query.rb
rails_app_generator-0.2.42 lib/rails_app_generator/gem_query.rb