<commit_before>module Jail  
  # This is a wrapper around ::Github 
  class Github
    attr_accessor :github, :name, :repo, :path, :spec
    cattr_accessor :githublist

    def self.add_githublist(yaml_path)
      hash = YAML.load_file(yaml_path).to_hash
      self.githublist= self.githublist.merge(hash)
    end

    self.githublist={}
    add_githublist(Jail::Engine.root.join("config", "jail.jqueryplugins.yml"))

    # TODO : raise if missing descr:
    def self.all
      githublist.group_by {|k, v| v[:descr].match(/\w+:/).to_s if v[:descr]}
    end

    # Use this to initialize Github
    def self.find(aname, arepo, apath = nil)
      self.new.tap do |yo|
        yo.name= aname
        yo.repo= arepo
        yo.path= apath
        yo.spec= githublist["#{yo.name}/#{yo.repo}"]
      end
    end

    # Sets github attr which holds the Github (API) instance
    # used by repo, contents, readme etc
    def initialize
      @github = defined?(Jail::LOGIN) ? 
                  ::Github.new({ :basic_auth => Jail::LOGIN }) :
                  ::Github.new
    end

    def repos
      github.repos.get(name, repo)
    end

    def contents
      self.path ||= "/"
      github.repos.contents.get(name, repo, path)
    end

    # TODO : split in 2 methods for consistency
    def readme
      text = Base64.decode64 github.repos.contents.readme(name, repo).content
      github.markdown.render :text => text
    end

    # TODO : raise error if path's not a file
    def read(apath)
      self.path= apath
      text = Base64.decode64 contents.content
    end

    def install
      #contents._links[:git]
      download(:js)
      download(:css)
      download(:img)
    end

    def remove
      delete_file(:js)
      delete_file(:css)
      delete_file(:img)
    end

    private
    def download(type = :js)
      return if spec[type].blank?
      text = read(spec[type])
      target(type).open('w') {|f| f.write(text) }
    end

    def delete_file(type = :js)
      return if spec[type].blank?
      self.path = spec[type]
      t = target(type) and t.exist? and t.delete
    end      

    def target(type)
      filename= Pathname(path).basename.to_s
      case type
      when :js
        Rails.root.join("vendor/assets/javascripts/#{filename}")
      when :css
        Rails.root.join("vendor/assets/stylesheets/#{filename}")
      when :img
        Rails.root.join("vendor/assets/images/#{filename}")
      end
    end

  end
end<commit_msg>one Github instance per repo + #where convenient method<commit_after>module Jail  
  # This is a wrapper around ::Github 
  class Github
    attr_reader    :github, :name, :repo, :spec
    attr_accessor  :path
    cattr_accessor :githublist

    def self.add_githublist(yaml_path)
      hash = YAML.load_file(yaml_path).to_hash
      self.githublist= self.githublist.merge(hash)
    end

    self.githublist={}
    add_githublist( Jail::Engine.root.join("config", "jail.jqueryplugins.yml") )

    # TODO : raise if missing descr:
    def self.all
      githublist.group_by {|k, v| v[:descr].match(/\w+:/).to_s if v[:descr]}
    end

    # Uhh useless ? Or AR Sexy ?
    def self.find(name, repo, path = nil)
      new(name, repo).tap do |yo|
        yo.path= path
      end
    end

    def initialize(name, repo)
      @name= name
      @repo= repo
      @spec= self.class.githublist["#{@name}/#{@repo}"]
      @github = defined?(Jail::LOGIN) ?
                  ::Github.new({ :basic_auth => Jail::LOGIN }) :
                  ::Github.new
    end

    # another AR alike for chaining
    def where(path)
      self.path= path
      self
    end

    def repos
      github.repos.get(name, repo)
    end

    def contents
      self.path ||= "/"
      github.repos.contents.get(name, repo, path)
    end

    # TODO : split in 2 methods for consistency
    def readme
      text = Base64.decode64 github.repos.contents.readme(name, repo).content
      github.markdown.render :text => text
    end

    def read
      #raise "blah" unless contents.type == "file"
      text = Base64.decode64 contents.content
    end

    def install
      #contents._links[:git]
      download(:js)
      download(:css)
      download(:img)
    end

    def remove
      delete_file(:js)
      delete_file(:css)
      delete_file(:img)
    end

    private
    def download(type = :js)
      return if spec[type].blank?
      text = where(spec[type]).read
      target(type).open('w') {|f| f.write(text) }
    end

    def delete_file(type = :js)
      return if spec[type].blank?
      self.path = spec[type]
      t = target(type) and t.exist? and t.delete
    end      

    def target(type)
      filename= Pathname(path).basename.to_s
      case type
      when :js
        Rails.root.join("vendor/assets/javascripts/#{filename}")
      when :css
        Rails.root.join("vendor/assets/stylesheets/#{filename}")
      when :img
        Rails.root.join("vendor/assets/images/#{filename}")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12099859069861083, 'fraction_numerical': 0.0036239178578618887, 'mean_word_length': 2.25130890052356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2067324', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1639, 'n_words': 389}