Class: Forrst::Post
- Inherits:
-
Object
- Object
- Forrst::Post
- Defined in:
- lib/forrst/post.rb
Overview
Forrst::Post is the class that represents a single post. Posts can be one of the following types:
-
code
-
snap
-
question
-
link
Based on these types certain fields may not be set. For example, the description() method will return a nil value in case the post type is set to “question” as those don’t get an extra description.
Retrieving a single post or multiple posts can be done using Forrst::Post.[] and Forrst::Post.find(). If you want to retrieve multiple posts you’ll have to use find(), if you only want to retrieve a single post you use []():
# Retrieves all code posts.
Forrst::Post.find(:type => :code).each do |post|
puts post.title
end
# Retrieve's the post with ID 123
post = Forrst::Post[123]
puts post.title
Each post will also contain the method comments(). This method sends an authenticated GET request to the Forrst server to retrieve all comments for the specified posts. This works as following:
post.comments.each do |comment|
puts comment.body
end
Note that similar to Forrst::User#posts all comments are lazy-loaded as the API provides no means of eager loading all comments. Use with care or you might risk getting your ass blocked.
Just like the class Forrst::User this class has a few shortcut methods for checking the type of post:
-
code?
-
snap?
-
question?
-
link?
All of these will return true or false depending on the post type.
Constant Summary collapse
- ShowURL =
URL relative to Forrst::URL that contains the details of a single post.
'/posts/show'- ListURL =
URL relative to Forrst::URL that contains a list of all posts.
'/posts/list'- CommentsURL =
URL relative to Forrst::URL that contains all the comments for a given post.
'/posts/comments'
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
The content of the post.
-
#created_at ⇒ Object
readonly
The date and time on which the post was created.
-
#description ⇒ Object
readonly
Field containing the description of the post, not used for questions.
-
#formatted_content ⇒ Object
readonly
The content in HTML.
-
#formatted_description ⇒ Object
readonly
The description in HTML.
-
#post_id ⇒ Object
readonly
The ID of the post.
-
#post_url ⇒ Object
readonly
The URL to the post on Forrst.
-
#public ⇒ Object
readonly
Boolean that indicates if the post is a public post or requires users to log in.
-
#published ⇒ Object
readonly
Boolean that indicates if the post has been published or not.
-
#snaps ⇒ Object
readonly
A hash containing the URLs to various sizes of the snap in case the post type is “snap”.
-
#statistics ⇒ Object
readonly
A hash containing the number of comments, likes, etc.
-
#tags ⇒ Object
readonly
An array of all the tags used in the post.
-
#tiny_id ⇒ Object
readonly
The “slug” that’s added to the URL.
-
#title ⇒ Object
readonly
The title of the post.
-
#type ⇒ Object
readonly
The type of post (“code” for example).
-
#updated_at ⇒ Object
readonly
The date and time on which the post was updated.
-
#url ⇒ Object
readonly
If the post type was a link this field will contain the URL to the external page.
-
#user ⇒ Object
readonly
The author of the post.
Class Method Summary collapse
-
.[](id) ⇒ Forrst::Post
Retrieves a single post by it’s ID or tiny ID.
-
.find(options) ⇒ Array
Given a custom set of options this method will retrieve a list of posts.
Instance Method Summary collapse
-
#code? ⇒ TrueClass/FalseClass
Checks if the post is a code post.
-
#comments ⇒ Array
Retrieves all the comments for the current post.
-
#initialize(response) ⇒ Forrst::Post
constructor
Creates a new instance of Forrst::Post and optionally decodes a JSON response.
-
#link? ⇒ TrueClass/FalseClass
Checks if the post is a link post.
-
#question? ⇒ TrueClass/FalseClass
Checks if the post is a question.
-
#snap? ⇒ TrueClass/FalseClass
Checks if the post is a snap.
Constructor Details
#initialize(response) ⇒ Forrst::Post
Creates a new instance of Forrst::Post and optionally decodes a JSON response. If a response is already decoded it’s data is merely assigned to the current instance.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/forrst/post.rb', line 297 def initialize(response) if response.class != Hash response = JSON.load(response) end @post_id = response['id'].to_i @tiny_id = response['id'] @type = response['post_type'] @post_url = response['post_url'] @created_at = Date.strptime(response['created_at'], Forrst::DateFormat) @updated_at = Date.strptime(response['updated_at'], Forrst::DateFormat) @user = Forrst::User.new(response['user']) @published = response['published'] @public = response['public'] @title = response['title'] @url = response['url'] @content = response['content'] @description = response['description'] @formatted_description = response['formatted_description'] @formatted_content = response['formatted_content'] = response['tags'] @statistics = { :comments => response['comment_count'].to_i, :likes => response['like_count'].to_i } # Get all the snaps @snaps = {} if response.key?('snaps') @snaps[:extra_large] = response['snaps']['mega_url'] @snaps[:keith] = response['snaps']['keith_url'] # The fuck is a keith? @snaps[:large] = response['snaps']['large_url'] @snaps[:medium] = response['snaps']['medium_url'] @snaps[:small] = response['snaps']['small_url'] @snaps[:thumbnail] = response['snaps']['thumb_url'] @snaps[:original] = response['snaps']['original_url'] end end |
Instance Attribute Details
#content ⇒ Object (readonly)
The content of the post. If it’s a question this field contains the question, if it’s a code post it will contain the code attached to the post.
175 176 177 |
# File 'lib/forrst/post.rb', line 175 def content @content end |
#created_at ⇒ Object (readonly)
The date and time on which the post was created. The value of this attribute is an instance of the Date class.
117 118 119 |
# File 'lib/forrst/post.rb', line 117 def created_at @created_at end |
#description ⇒ Object (readonly)
Field containing the description of the post, not used for questions.
183 184 185 |
# File 'lib/forrst/post.rb', line 183 def description @description end |
#formatted_content ⇒ Object (readonly)
The content in HTML.
199 200 201 |
# File 'lib/forrst/post.rb', line 199 def formatted_content @formatted_content end |
#formatted_description ⇒ Object (readonly)
The description in HTML.
191 192 193 |
# File 'lib/forrst/post.rb', line 191 def formatted_description @formatted_description end |
#post_id ⇒ Object (readonly)
The ID of the post.
84 85 86 |
# File 'lib/forrst/post.rb', line 84 def post_id @post_id end |
#post_url ⇒ Object (readonly)
The URL to the post on Forrst.
108 109 110 |
# File 'lib/forrst/post.rb', line 108 def post_url @post_url end |
#public ⇒ Object (readonly)
Boolean that indicates if the post is a public post or requires users to log in.
150 151 152 |
# File 'lib/forrst/post.rb', line 150 def public @public end |
#published ⇒ Object (readonly)
Boolean that indicates if the post has been published or not.
142 143 144 |
# File 'lib/forrst/post.rb', line 142 def published @published end |
#snaps ⇒ Object (readonly)
A hash containing the URLs to various sizes of the snap in case the post type is “snap”.
224 225 226 |
# File 'lib/forrst/post.rb', line 224 def snaps @snaps end |
#statistics ⇒ Object (readonly)
A hash containing the number of comments, likes, etc.
207 208 209 |
# File 'lib/forrst/post.rb', line 207 def statistics @statistics end |
#tags ⇒ Object (readonly)
An array of all the tags used in the post.
215 216 217 |
# File 'lib/forrst/post.rb', line 215 def end |
#tiny_id ⇒ Object (readonly)
The “slug” that’s added to the URL.
92 93 94 |
# File 'lib/forrst/post.rb', line 92 def tiny_id @tiny_id end |
#title ⇒ Object (readonly)
The title of the post.
158 159 160 |
# File 'lib/forrst/post.rb', line 158 def title @title end |
#type ⇒ Object (readonly)
The type of post (“code” for example).
100 101 102 |
# File 'lib/forrst/post.rb', line 100 def type @type end |
#updated_at ⇒ Object (readonly)
The date and time on which the post was updated. The value of this attribute is an instance of the Date class.
126 127 128 |
# File 'lib/forrst/post.rb', line 126 def updated_at @updated_at end |
#url ⇒ Object (readonly)
If the post type was a link this field will contain the URL to the external page.
166 167 168 |
# File 'lib/forrst/post.rb', line 166 def url @url end |
#user ⇒ Object (readonly)
The author of the post.
134 135 136 |
# File 'lib/forrst/post.rb', line 134 def user @user end |
Class Method Details
.[](id) ⇒ Forrst::Post
Retrieves a single post by it’s ID or tiny ID. If the parameter given is a Fixnum this method assumes is the ID, if it’s a string it assumes it’s the tiny ID.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/forrst/post.rb', line 273 def self.[](id) if id.class == Fixnum query_items = {:id => id} elsif id.class == String query_items = {:tiny_id => id} else raise(TypeError, "Got #{id.class} but expected Fixnum or String") end response = Forrst.oauth.request(:get, ShowURL, query_items) response = JSON.load(response) return Post.new(response['resp']) end |
.find(options) ⇒ Array
Given a custom set of options this method will retrieve a list of posts. It’s important to remember that the keys of the hash passed to this method should be symbols and not strings.
Note that this method will always return an array of posts, if you want to retrieve a single post use Forrst::Post[] instead.
the following: code, snap, link or question. following: recent, popular or best. unknown) number of posts.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/forrst/post.rb', line 249 def self.find() # Set the correct keys for the API call query_items = {} query_items[:post_type] = [:type].to_s if .key?(:type) query_items[:sort] = [:sort].to_s if .key?(:sort) query_items[:page] = [:page].to_s if .key?(:page) response = Forrst.oauth.request(:get, ListURL, query_items) response = JSON.load(response) return response['resp']['posts'].map do |post| Post.new(post) end end |
Instance Method Details
#code? ⇒ TrueClass/FalseClass
Checks if the post is a code post.
390 391 392 |
# File 'lib/forrst/post.rb', line 390 def code? return @type === 'code' end |
#comments ⇒ Array
Retrieves all the comments for the current post.
352 353 354 355 356 357 358 359 |
# File 'lib/forrst/post.rb', line 352 def comments response = Forrst.oauth.request(:get, CommentsURL, :id => @post_id) response = JSON.load(response) return response['comments'].map do |comment| Forrst::Comment.new(comment) end end |
#link? ⇒ TrueClass/FalseClass
Checks if the post is a link post.
401 402 403 |
# File 'lib/forrst/post.rb', line 401 def link? return @type === 'link' end |
#question? ⇒ TrueClass/FalseClass
Checks if the post is a question.
368 369 370 |
# File 'lib/forrst/post.rb', line 368 def question? return @type === 'question' end |
#snap? ⇒ TrueClass/FalseClass
Checks if the post is a snap.
379 380 381 |
# File 'lib/forrst/post.rb', line 379 def snap? return @type === 'snap' end |