Class: Stacked::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/stacked/base.rb

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  records(path, options)
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, options={})
  self.new(request(singular(id), options)[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, options = {})
  parse(request(p, options)[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, options = {})
  get(p, :query => { :key => key }.merge!(options))
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

.statsObject

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 parse_tags(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

#postObject

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