Class: Forrst::Post

Inherits:
Object
  • Object
show all
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.

Author:

  • Yorick Peterse

Since:

  • 0.1a

Constant Summary collapse

ShowURL =

URL relative to Forrst::URL that contains the details of a single post.

Author:

  • Yorick Peterse

Since:

  • 0.1a

'/posts/show'
ListURL =

URL relative to Forrst::URL that contains a list of all posts.

Author:

  • Yorick Peterse

Since:

  • 0.1

'/posts/list'
CommentsURL =

URL relative to Forrst::URL that contains all the comments for a given post.

Author:

  • Yorick Peterse

Since:

  • 0.1a

'/posts/comments'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • response (String/Hash)

    The API response in either JSON format or a hash.

Author:

  • Yorick Peterse

Since:

  • 0.1a



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']
  @tags                  = 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

#contentObject (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.

Author:

  • Yorick Peterse

Since:

  • 0.1a



175
176
177
# File 'lib/forrst/post.rb', line 175

def content
  @content
end

#created_atObject (readonly)

The date and time on which the post was created. The value of this attribute is an instance of the Date class.

Author:

  • Yorick Peterse

Since:

  • 0.1a



117
118
119
# File 'lib/forrst/post.rb', line 117

def created_at
  @created_at
end

#descriptionObject (readonly)

Field containing the description of the post, not used for questions.

Author:

  • Yorick Peterse

Since:

  • 0.1a



183
184
185
# File 'lib/forrst/post.rb', line 183

def description
  @description
end

#formatted_contentObject (readonly)

The content in HTML.

Author:

  • Yorick Peterse

Since:

  • 0.1a



199
200
201
# File 'lib/forrst/post.rb', line 199

def formatted_content
  @formatted_content
end

#formatted_descriptionObject (readonly)

The description in HTML.

Author:

  • Yorick Peterse

Since:

  • 0.1a



191
192
193
# File 'lib/forrst/post.rb', line 191

def formatted_description
  @formatted_description
end

#post_idObject (readonly)

The ID of the post.

Author:

  • Yorick Peterse

Since:

  • 0.1a



84
85
86
# File 'lib/forrst/post.rb', line 84

def post_id
  @post_id
end

#post_urlObject (readonly)

The URL to the post on Forrst.

Author:

  • Yorick Peterse

Since:

  • 0.1a



108
109
110
# File 'lib/forrst/post.rb', line 108

def post_url
  @post_url
end

#publicObject (readonly)

Boolean that indicates if the post is a public post or requires users to log in.

Author:

  • Yorick Peterse

Since:

  • 0.1a



150
151
152
# File 'lib/forrst/post.rb', line 150

def public
  @public
end

#publishedObject (readonly)

Boolean that indicates if the post has been published or not.

Author:

  • Yorick Peterse

Since:

  • 0.1a



142
143
144
# File 'lib/forrst/post.rb', line 142

def published
  @published
end

#snapsObject (readonly)

A hash containing the URLs to various sizes of the snap in case the post type is “snap”.

Author:

  • Yorick Peterse

Since:

  • 0.1a



224
225
226
# File 'lib/forrst/post.rb', line 224

def snaps
  @snaps
end

#statisticsObject (readonly)

A hash containing the number of comments, likes, etc.

Author:

  • Yorick Peterse

Since:

  • 0.1a



207
208
209
# File 'lib/forrst/post.rb', line 207

def statistics
  @statistics
end

#tagsObject (readonly)

An array of all the tags used in the post.

Author:

  • Yorick Peterse

Since:

  • 0.1a



215
216
217
# File 'lib/forrst/post.rb', line 215

def tags
  @tags
end

#tiny_idObject (readonly)

The “slug” that’s added to the URL.

Author:

  • Yorick Peterse

Since:

  • 0.1a



92
93
94
# File 'lib/forrst/post.rb', line 92

def tiny_id
  @tiny_id
end

#titleObject (readonly)

The title of the post.

Author:

  • Yorick Peterse

Since:

  • 0.1a



158
159
160
# File 'lib/forrst/post.rb', line 158

def title
  @title
end

#typeObject (readonly)

The type of post (“code” for example).

Author:

  • Yorick Peterse

Since:

  • 0.1a



100
101
102
# File 'lib/forrst/post.rb', line 100

def type
  @type
end

#updated_atObject (readonly)

The date and time on which the post was updated. The value of this attribute is an instance of the Date class.

Author:

  • Yorick Peterse

Since:

  • 0.1a



126
127
128
# File 'lib/forrst/post.rb', line 126

def updated_at
  @updated_at
end

#urlObject (readonly)

If the post type was a link this field will contain the URL to the external page.

Author:

  • Yorick Peterse

Since:

  • 0.1a



166
167
168
# File 'lib/forrst/post.rb', line 166

def url
  @url
end

#userObject (readonly)

The author of the post.

Author:

  • Yorick Peterse

Since:

  • 0.1a



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.

Parameters:

  • id (Fixnum/String)

    The ID or the tiny ID.

Returns:

Author:

  • Yorick Peterse

Since:

  • 0.1a



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.

Examples:

Forrst::Post.find(:type => :question)
Forrst::Post.find(:type => :code, :sort => :recent)

Parameters:

Options Hash (options):

  • :type (Symbol)

    The type of posts to retrieve. Can be one of

  • :sort (Symbol)

    The sort order to use, can be any of the

  • :page (Fixnum)

    The page to use. Each page contains a (currently

Returns:

  • (Array)

Author:

  • Yorick Peterse

Since:

  • 0.1a



249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/forrst/post.rb', line 249

def self.find(options)
  # Set the correct keys for the API call
  query_items             = {}
  query_items[:post_type] = options[:type].to_s if options.key?(:type)
  query_items[:sort]      = options[:sort].to_s if options.key?(:sort)
  query_items[:page]      = options[:page].to_s if options.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.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1a



390
391
392
# File 'lib/forrst/post.rb', line 390

def code?
  return @type === 'code'
end

#commentsArray

Retrieves all the comments for the current post.

Examples:

post = Forrst::Post[10]
post.comments.each do |comment|
  puts comment.body
end

Returns:

  • (Array)

Author:

  • Yorick Peterse

Since:

  • 0.1a



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.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1a



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.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1a



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.

Returns:

  • (TrueClass/FalseClass)

Author:

  • Yorick Peterse

Since:

  • 0.1a



379
380
381
# File 'lib/forrst/post.rb', line 379

def snap?
  return @type === 'snap'
end