Sha256: d0eba3a4d23cb71bf79c9435314eb8d22710519dc00961389a3eaa895ac280c1

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Write
  class GG
    def self.log message
      Rails.logger.info "******WRITE******: #{message}"
    end

    def self.url path
      "https://api.github.com" + path
    end

    def self.fetch
      posts = []
      Write.accounts.each do |username, password|
        JSON.parse(open(url "/users/#{username}/gists").read).each do |p|
          next unless p["files"]["write.md"]
          post = new(p)
          posts.push post
        end
      end
      Rails.cache.write "write.posts", posts
    end

    def self.all
      Rails.cache.read "write.posts"
    end

    def self.empty?
      all.length == 0
    end

    def self.clear!
      log "Clearing all cached posts"
      Rails.cache.write "write.posts", []
    end

    def self.find code
      all.find { |p| p.code == code }
    end

    attr_reader :code

    def initialize attributes
      @attributes = attributes
      @code = attributes["description"].downcase.gsub(/[^a-z0-9]/, "-").squeeze
      @raw_url = files["write.md"]["raw_url"]
      GG.log "Caching post #{@code}"
      @raw_data = open(@raw_url).read
      Rails.cache.write "write.post-#{id}", @raw_data
    end

    def id
      @attributes["id"]
    end

    def year
      @attributes["created_at"].to_date.year
    end

    def content
      Rails.cache.read "write.post-#{id}"
    end

    def method_missing method, *args, &block
      attribute = @attributes[method.to_s]
      return attribute if attribute
      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
write-0.2.0 app/models/write/gg.rb