Sha256: 51bd7ee484a51b971fb0e5aba36488a0d8e4dcc5db42bb74afffc379b1662e82

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

require "fileutils"
require 'uri'
require "open-uri"
require "rubygems"
require "rio"

module AndParcel
	class Catalog
		attr_reader :nick, :path
		
		def initialize(path)
			@path=path
			@nick=File.basename(path, '.json')
		end
	end
	
	class CatalogSet
		def initialize(root)
			@root=root
		end
		
		def size
			roster.size
		end
	
		def roster_file
			File.join(@root, 'catalogs.json')
		end
		
		def roster
			fn=roster_file
			
			if File.exists?(fn)
				return(JSON.parse(open(fn) {|f| f.read}))
			end
			
			{}
		end
		
		def add(name, url)
			roster_hash=roster

			roster_hash[name]={'url'=>url}

			open(roster_file, "w") {|f| f.puts(roster_hash.to_json)}
		end
		
		def remove(name)
			roster_hash=roster

			roster_hash.delete(name)

			open(roster_file, "w") {|f| f.puts(roster_hash.to_json)}
		end
		
		def names
			roster.keys
		end
		
		def find(name)
			roster.each_value do |cat|
				packages=JSON.parse(open(cat['url']) {|f| f.read})
				package=packages['parcels'][name]

				if package
					if (urls=package['url'])
						if urls.kind_of?(Enumerable)
							return(urls.first)
						end
						
						return(urls)
					elsif (path=package['path'])
						uri=URI.parse cat['url']
						
						return(uri+path)
					end
				end
			end
			
			nil
		end
	end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
andparcel-0.4.1 lib/andparcel/catalog.rb
andparcel-0.4.0 lib/andparcel/catalog.rb
andparcel-0.3.2 lib/andparcel/catalog.rb
andparcel-0.3.0 lib/andparcel/catalog.rb
andparcel-0.2.1 lib/andparcel/catalog.rb
andparcel-0.2.0 lib/andparcel/catalog.rb