Class: Gemirro::Server
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Gemirro::Server
- Defined in:
- lib/gemirro/server.rb
Overview
Launch Sinatra server to easily download gems.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#* ⇒ nil
Try to get all request and download files if files aren’t found.
- #configuration ⇒ Object
-
#display_directory(resource) ⇒ Array
Display directory on the current sesion.
-
#fetch_gem(resource) ⇒ Indexer
Try to fetch gem and download its if it’s possible, and build and install indicies.
-
#generate_index ⇒ Indexer
Generate index and install indicies.
- #logger ⇒ Logger
Instance Attribute Details
#gems_fetcher ⇒ Gemirro::GemsFetcher
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gemirro/server.rb', line 14 class Server < Sinatra::Base attr_accessor :versions_fetcher, :gems_fetcher ## # Configure server # configure do config = Gemirro.configuration config.server_host = 'localhost' if config.server_host.nil? config.server_port = '2000' if config.server_port.nil? set :port, config.server_port set :bind, config.server_host set :destination, config.destination.gsub(/\/$/, '') end ## # Try to get all request and download files # if files aren't found. # # @return [nil] # get('*') do |path| resource = "#{settings.destination}#{path}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 return not_found unless File.exist?(resource) if File.directory?(resource) display_directory(resource) else send_file resource end end ## # Try to fetch gem and download its if it's possible, and # build and install indicies. # # @param [String] resource # @return [Indexer] # def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ result = name.match(regexp) return unless result gem_name, gem_version = result.captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end ## # Generate index and install indicies. # # @return [Indexer] # def generate_index indexer = Indexer.new(settings.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end ## # Display directory on the current sesion # # @param [String] resource # @return [Array] # def display_directory(resource) base_dir = Dir.new(resource) base_dir.entries.sort.map do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(settings.destination, '') "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" \ unless ['.', '..'].include?(File.basename(resource_path)) end.compact end ## # @see Gemirro::Configuration#logger # @return [Logger] # def logger configuration.logger end ## # @see Gemirro.configuration # def configuration Gemirro.configuration end ## # @see Gemirro::VersionsFetcher.fetch # def versions_fetcher @versions_fetcher ||= Gemirro::VersionsFetcher.new(configuration.source).fetch end ## # @return [Gemirro::GemsFetcher] # def gems_fetcher @gems_fetcher ||= Gemirro::GemsFetcher.new( configuration.source, versions_fetcher) end end |
#versions_fetcher ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gemirro/server.rb', line 14 class Server < Sinatra::Base attr_accessor :versions_fetcher, :gems_fetcher ## # Configure server # configure do config = Gemirro.configuration config.server_host = 'localhost' if config.server_host.nil? config.server_port = '2000' if config.server_port.nil? set :port, config.server_port set :bind, config.server_host set :destination, config.destination.gsub(/\/$/, '') end ## # Try to get all request and download files # if files aren't found. # # @return [nil] # get('*') do |path| resource = "#{settings.destination}#{path}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 return not_found unless File.exist?(resource) if File.directory?(resource) display_directory(resource) else send_file resource end end ## # Try to fetch gem and download its if it's possible, and # build and install indicies. # # @param [String] resource # @return [Indexer] # def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ result = name.match(regexp) return unless result gem_name, gem_version = result.captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end ## # Generate index and install indicies. # # @return [Indexer] # def generate_index indexer = Indexer.new(settings.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end ## # Display directory on the current sesion # # @param [String] resource # @return [Array] # def display_directory(resource) base_dir = Dir.new(resource) base_dir.entries.sort.map do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(settings.destination, '') "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" \ unless ['.', '..'].include?(File.basename(resource_path)) end.compact end ## # @see Gemirro::Configuration#logger # @return [Logger] # def logger configuration.logger end ## # @see Gemirro.configuration # def configuration Gemirro.configuration end ## # @see Gemirro::VersionsFetcher.fetch # def versions_fetcher @versions_fetcher ||= Gemirro::VersionsFetcher.new(configuration.source).fetch end ## # @return [Gemirro::GemsFetcher] # def gems_fetcher @gems_fetcher ||= Gemirro::GemsFetcher.new( configuration.source, versions_fetcher) end end |
Instance Method Details
#* ⇒ nil
Try to get all request and download files if files aren’t found.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gemirro/server.rb', line 35 get('*') do |path| resource = "#{settings.destination}#{path}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 return not_found unless File.exist?(resource) if File.directory?(resource) display_directory(resource) else send_file resource end end |
#configuration ⇒ Object
122 123 124 |
# File 'lib/gemirro/server.rb', line 122 def configuration Gemirro.configuration end |
#display_directory(resource) ⇒ Array
Display directory on the current sesion
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/gemirro/server.rb', line 98 def display_directory(resource) base_dir = Dir.new(resource) base_dir.entries.sort.map do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(settings.destination, '') "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" \ unless ['.', '..'].include?(File.basename(resource_path)) end.compact end |
#fetch_gem(resource) ⇒ Indexer
Try to fetch gem and download its if it’s possible, and build and install indicies.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gemirro/server.rb', line 57 def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ result = name.match(regexp) return unless result gem_name, gem_version = result.captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end |
#generate_index ⇒ Indexer
Generate index and install indicies.
84 85 86 87 88 89 90 |
# File 'lib/gemirro/server.rb', line 84 def generate_index indexer = Indexer.new(settings.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end |
#logger ⇒ Logger
115 116 117 |
# File 'lib/gemirro/server.rb', line 115 def logger configuration.logger end |