Class: Write::GG

Inherits:
Object
  • Object
show all
Defined in:
app/models/write/gg.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ GG

Returns a new instance of GG.



42
43
44
45
46
47
48
49
# File 'app/models/write/gg.rb', line 42

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



63
64
65
66
67
# File 'app/models/write/gg.rb', line 63

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

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



40
41
42
# File 'app/models/write/gg.rb', line 40

def code
  @code
end

Class Method Details

.allObject



23
24
25
# File 'app/models/write/gg.rb', line 23

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

.clear!Object



31
32
33
34
# File 'app/models/write/gg.rb', line 31

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

.empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/write/gg.rb', line 27

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

.fetchObject



11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/write/gg.rb', line 11

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

.find(code) ⇒ Object



36
37
38
# File 'app/models/write/gg.rb', line 36

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

.log(message) ⇒ Object



3
4
5
# File 'app/models/write/gg.rb', line 3

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

.url(path) ⇒ Object



7
8
9
# File 'app/models/write/gg.rb', line 7

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

Instance Method Details

#contentObject



59
60
61
# File 'app/models/write/gg.rb', line 59

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

#idObject



51
52
53
# File 'app/models/write/gg.rb', line 51

def id
  @attributes["id"]
end

#yearObject



55
56
57
# File 'app/models/write/gg.rb', line 55

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