Class: Write::GG
- Inherits:
-
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.
46
47
48
49
50
51
52
53
54
55
|
# File 'app/models/write/gg.rb', line 46
def initialize attributes
@attributes = attributes
@code = attributes["description"].downcase.gsub(/[^a-z0-9]/, "-")
@qcode = @code.squeeze
@code = @code.squeeze("-").chomp("-")
@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
81
82
83
84
85
|
# File 'app/models/write/gg.rb', line 81
def method_missing method, *args, &block
attribute = @attributes[method.to_s]
return attribute if attribute
super
end
|
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
44
45
46
|
# File 'app/models/write/gg.rb', line 44
def code
@code
end
|
#qcode ⇒ Object
Returns the value of attribute qcode.
44
45
46
|
# File 'app/models/write/gg.rb', line 44
def qcode
@qcode
end
|
Class Method Details
.all ⇒ Object
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
27
28
29
|
# File 'app/models/write/gg.rb', line 27
def self.empty?
all.length == 0
end
|
.fetch ⇒ Object
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(year, code) ⇒ Object
36
37
38
|
# File 'app/models/write/gg.rb', line 36
def self.find year, code
all.find { |p| p.year == year.to_i && (p.code == code || p.qcode == code) }
end
|
.last_update ⇒ Object
40
41
42
|
# File 'app/models/write/gg.rb', line 40
def self.last_update
all.sort_by(&:updated_date).last.updated_date
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
#author_name ⇒ Object
73
74
75
|
# File 'app/models/write/gg.rb', line 73
def author_name
@attributes["user"]["login"]
end
|
#content ⇒ Object
77
78
79
|
# File 'app/models/write/gg.rb', line 77
def content
Rails.cache.read "write.post-#{id}"
end
|
#id ⇒ Object
57
58
59
|
# File 'app/models/write/gg.rb', line 57
def id
@attributes["id"]
end
|
#updated? ⇒ Boolean
65
66
67
|
# File 'app/models/write/gg.rb', line 65
def updated?
@attributes["created_at"][1..12] != @attributes["updated_at"][1..12]
end
|
#updated_date ⇒ Object
69
70
71
|
# File 'app/models/write/gg.rb', line 69
def updated_date
@attributes["updated_at"].to_date
end
|
#year ⇒ Object
61
62
63
|
# File 'app/models/write/gg.rb', line 61
def year
@attributes["created_at"].to_date.year
end
|