Permalink
Browse files

Jail::Cdnjs basic model

  • Loading branch information...
1 parent 3191d59 commit 75ab174e4a0fc1a4e130129cc826e3e1c14e5311 @charly committed Oct 4, 2012
Showing with 98 additions and 0 deletions.
  1. +61 −0 app/models/jail/cdnjs.rb
  2. +37 −0 spec/models/cdnjs_spec.rb
View
@@ -0,0 +1,61 @@
+module Jail
+ class Cdnjs
+ attr_reader :github, :root_path, :package_path
+ delegate :name, :filename, :version, :description,
+ :homepage, :repositories, :maintainers, :to => :package #etc
+
+ def self.github
+ @github ||= Jail::Github.find("cdnjs", "cdnjs", "ajax/libs")
+ end
+
+ def self.libs
+ @libs ||= github.contents
+ end
+ libs # load & memoize cdnjs libraries
+
+ def self.lib(path)
+ github.where(path).contents
+ end
+
+ def self.find_path(name)
+ hash = libs.find { |hashie| hashie.path.split("/").last == name }
+ hash and hash.path # raise if not found ?
+ end
+
+ def self.find(name)
+ path = find_path(name)
+ lib = lib(path)
+
+ self.new(path, lib)
+ end
+
+ def initialize(path, lib)
+ @github = self.class.github
+ @root_path = path
+ @package_path = lib.find {|h| h.type == "file"}.path
+ end
+
+ def package
+ @package ||= Hashie::Mash.new ActiveSupport::JSON.decode( github.where(package_path).read )
+ end
+
+ # gets the version from the package.json & returns a path
+ def version_path
+ Pathname(root_path).join(version)
+ end
+
+ def files
+ @files ||= github.where(version_path).contents
+ end
+
+ def file(name)
+ github.where(version_path.join(name))
+ end
+
+ def mapped_files
+ # or => files.group_by {|file| Pathname(file.name).extname }
+ files.map(&:name).group_by {|name| Pathname(name).extname }
+ end
+
+ end
+end
View
@@ -0,0 +1,37 @@
+require "spec_helper"
+
+# TODO : TAFT
+decribe Jail::Cdnjs do
+
+ describe "#package" do
+ it "holds a Hashie of parsed package.json" do
+ true
+ end
+ end
+
+ describe "#version_path" do
+ it "it has the pathname of latest version" do
+ true
+ end
+ end
+
+ describe "#files" do
+ it "github API resp listing the (latest) version folder" do
+ true
+ end
+ end
+
+ describe "#file" do
+ it "github API resp of a file in the version folder" do
+ true
+ end
+ end
+
+ describe "#mapped_files" do
+ it "groups files by their extension" do
+ true
+ end
+ end
+
+
+end

0 comments on commit 75ab174

Please sign in to comment.