Skip to content

Commit

Permalink
change for github api v3
Browse files Browse the repository at this point in the history
  • Loading branch information
eitoball committed Sep 4, 2012
1 parent dfd380d commit 218f504
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lib/generators/kaminari/views_generator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Kaminari
module Generators
SHOW_API = 'http://github.com/api/v2/json/blob/show/amatsuda/kaminari_themes'
ALL_API = 'http://github.com/api/v2/json/blob/all/amatsuda/kaminari_themes/master'

class ViewsGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../../../../app/views/kaminari', __FILE__)
Expand Down Expand Up @@ -37,13 +35,8 @@ def copy_or_fetch #:nodoc:
private
def self.themes
begin
@themes ||= open ALL_API do |json|
# @themes ||= open(File.join(File.dirname(__FILE__), '../../../spec/generators/sample.json')) do |json|
files = ActiveSupport::JSON.decode(json)['blobs']
hash = files.group_by {|fn, _| fn[0...(fn.index('/') || 0)]}.delete_if {|fn, _| fn.blank?}
hash.map do |name, files|
Theme.new name, files
end
@themes ||= GitHubApiHelper.get_files_in_master.group_by {|fn, _| fn[0...(fn.index('/') || 0)]}.delete_if {|fn, _| fn.blank?}.map do |name, files|
Theme.new name, files
end
rescue SocketError
[]
Expand All @@ -53,7 +46,7 @@ def self.themes
def download_templates(theme)
theme.templates_for(template_engine).each do |template|
say " downloading #{template.name} from kaminari_themes..."
get "#{SHOW_API}/#{template.sha}", template.name
create_file template.name, GitHubApiHelper.get_content_for("#{theme.name}/#{template.name}")
end
end

Expand Down Expand Up @@ -92,12 +85,34 @@ def initialize(name, templates) #:nodoc:
def description #:nodoc:
file = @templates.detect(&:description?)
return "#{' ' * 12}#{name}" unless file
open("#{SHOW_API}/#{file.sha}").read.chomp.gsub /^/, ' ' * 12
GitHubApiHelper.get_content_for("#{@name}/#{file.name}").chomp.gsub(/^/, ' ' * 12)
end

def templates_for(template_engine) #:nodoc:
@templates.select {|t| !t.description?}.select {|t| !t.view? || (t.engine == template_engine)}
end
end

module GitHubApiHelper
def get_files_in_master
master_tree_sha = open('https://api.github.com/repos/amatsuda/kaminari_themes/git/refs/heads/master') do |json|
ActiveSupport::JSON.decode(json)['object']['sha']
end
open('https://api.github.com/repos/amatsuda/kaminari_themes/git/trees/' + master_tree_sha + '?recursive=1') do |json|
blobs = ActiveSupport::JSON.decode(json)['tree'].find_all {|i| i['type'] == 'blob' }
blobs.map do |blob|
[blob['path'], blob['sha']]
end
end
end
module_function :get_files_in_master

def get_content_for(path)
open('https://api.github.com/repos/amatsuda/kaminari_themes/contents/' + path) do |json|
Base64.decode64(ActiveSupport::JSON.decode(json)['content'])
end
end
module_function :get_content_for
end
end
end

0 comments on commit 218f504

Please sign in to comment.