Class: Stacked::Base
Direct Known Subclasses
Answer, Badge, Comment, Posttimeline, Question, Reputation, Tag, User, Usertimeline
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
All the first group (depends on pagesize) of records for current class.
-
.association(assoc) ⇒ Object
Defines association methods for things such as comments on questions.
-
.collection(*names) ⇒ Object
Define collection methods, such as newest.
-
.find(id, options = {}) ⇒ Object
A single record belonging to the current class.
-
.records(p = path, options = {}) ⇒ Object
All records for a given request path.
-
.request(p = path, options = {}) ⇒ Object
Raw Hash of request.
-
.singular(id) ⇒ Object
The path to the singular resource.
-
.stats ⇒ Object
Return the stats provided by the API.
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Base
constructor
Creates a new object of the given class based on the attributes passed in.
-
#parse_answers(result) ⇒ Object
Convert an answers result into a collection of Stacked::Answer objects.
-
#parse_badges(result) ⇒ Object
Convert a badges result into a collection of Stacked::Badge objects.
-
#parse_comments(result) ⇒ Object
Convert a comments result into a collection of Stacked::Comment objects.
-
#parse_post_timeline(result) ⇒ Object
Convert a post timeline result into a collection of Stacked::Posttimeline objects.
-
#parse_questions(result) ⇒ Object
Convert a questions result into a collection of Stacked::Question objects.
-
#parse_reputations(result) ⇒ Object
Convert a reputation result into a collection of Stacked::Reputation objects.
-
#parse_tags(result) ⇒ Object
Convert a tags result into a collection of Stacked::Tag objects.
-
#parse_type(result, type) ⇒ Object
Converts the specified result into objects of the
type
class. -
#parse_user_timeline(result) ⇒ Object
Convert a user timeline result into a collection of Stacked::Usertimeline objects.
-
#post ⇒ Object
Finds a post based on the
post_type
andpost_id
.
Constructor Details
#initialize(attributes) ⇒ Base
Creates a new object of the given class based on the attributes passed in.
151 152 153 154 155 156 157 |
# File 'lib/stacked/base.rb', line 151 def initialize(attributes) # p self # p attributes.keys.sort.map { |t| t.to_sym } attributes.each do |k, v| self.send("#{k}=", v) end end |
Class Method Details
.all(options = {}) ⇒ Object
All the first group (depends on pagesize) of records for current class.
18 19 20 |
# File 'lib/stacked/base.rb', line 18 def all( = {}) records(path, ) end |
.association(assoc) ⇒ Object
Defines association methods for things such as comments on questions.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/stacked/base.rb', line 50 def association(assoc) instance_eval do assoc = assoc.to_s define_method("#{assoc}=") do |records| instance_variable_set("@#{assoc}", records.map { |record| "Stacked::#{assoc.classify}".constantize.new(record) }) end define_method(assoc) { instance_variable_get("@#{assoc}") } end end |
.collection(*names) ⇒ Object
Define collection methods, such as newest.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/stacked/base.rb', line 38 def collection(*names) # Forgive me Matz for I have sinned. for name in names eval <<-EVAL def self.#{name}(options = {}) records(path + "#{name}", options) end EVAL end end |
.find(id, options = {}) ⇒ Object
A single record belonging to the current class.
23 24 25 |
# File 'lib/stacked/base.rb', line 23 def find(id, ={}) self.new(request(singular(id), )[resource.singularize]) end |
.records(p = path, options = {}) ⇒ Object
All records for a given request path.
28 29 30 |
# File 'lib/stacked/base.rb', line 28 def records(p = path, = {}) parse(request(p, )[resource]) end |
.request(p = path, options = {}) ⇒ Object
Raw Hash of request.
33 34 35 |
# File 'lib/stacked/base.rb', line 33 def request(p = path, = {}) get(p, :query => { :key => key }.merge!()) end |
.singular(id) ⇒ Object
The path to the singular resource.
62 63 64 |
# File 'lib/stacked/base.rb', line 62 def singular(id) path + id.to_s end |
.stats ⇒ Object
Return the stats provided by the API.
13 14 15 |
# File 'lib/stacked/base.rb', line 13 def stats request(base + "stats")["stats"] end |
Instance Method Details
#parse_answers(result) ⇒ Object
Convert an answers result into a collection of Stacked::Answer objects.
99 100 101 |
# File 'lib/stacked/base.rb', line 99 def parse_answers(result) parse_type(result, "answer") end |
#parse_badges(result) ⇒ Object
Convert a badges result into a collection of Stacked::Badge objects.
104 105 106 |
# File 'lib/stacked/base.rb', line 104 def parse_badges(result) parse_type(result, "badge") end |
#parse_comments(result) ⇒ Object
Convert a comments result into a collection of Stacked::Comment objects.
109 110 111 |
# File 'lib/stacked/base.rb', line 109 def parse_comments(result) parse_type(result, "comment") end |
#parse_post_timeline(result) ⇒ Object
Convert a post timeline result into a collection of Stacked::Posttimeline objects.
114 115 116 |
# File 'lib/stacked/base.rb', line 114 def parse_post_timeline(result) parse_type(result, "posttimeline") end |
#parse_questions(result) ⇒ Object
Convert a questions result into a collection of Stacked::Question objects.
119 120 121 |
# File 'lib/stacked/base.rb', line 119 def parse_questions(result) parse_type(result, "question") end |
#parse_reputations(result) ⇒ Object
Convert a reputation result into a collection of Stacked::Reputation objects.
124 125 126 |
# File 'lib/stacked/base.rb', line 124 def parse_reputations(result) parse_type(result, "reputation") end |
#parse_tags(result) ⇒ Object
Convert a tags result into a collection of Stacked::Tag objects.
129 130 131 |
# File 'lib/stacked/base.rb', line 129 def (result) parse_type(result, "tag") end |
#parse_type(result, type) ⇒ Object
Converts the specified result into objects of the type
class.
139 140 141 |
# File 'lib/stacked/base.rb', line 139 def parse_type(result, type) parse(result[type.pluralize], "Stacked::#{type.classify}".constantize) end |
#parse_user_timeline(result) ⇒ Object
Convert a user timeline result into a collection of Stacked::Usertimeline objects.
134 135 136 |
# File 'lib/stacked/base.rb', line 134 def parse_user_timeline(result) parse_type(result, "usertimeline") end |
#post ⇒ Object
Finds a post based on the post_type
and post_id
146 147 148 |
# File 'lib/stacked/base.rb', line 146 def post "Stacked::#{post_type.classify}".constantize.find(post_id) end |