Class: ReadIt::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/read_it/post.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Post

Returns a new instance of Post.



9
10
11
# File 'lib/read_it/post.rb', line 9

def initialize data
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



25
26
27
# File 'lib/read_it/post.rb', line 25

def method_missing name, *args, &block
  @data[name.to_s]
end

Class Method Details

.parse(response) ⇒ Object



3
4
5
6
7
# File 'lib/read_it/post.rb', line 3

def self.parse response
  response = JSON.parse response.body
  posts = response.fetch("data", {}).fetch "children", []
  posts.map { |post| new post["data"] }
end

Instance Method Details

#imageObject



17
18
19
# File 'lib/read_it/post.rb', line 17

def image
  url if image?
end

#image?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/read_it/post.rb', line 13

def image?
  image_types.include? url.split('.').last.downcase
end

#nsfw?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/read_it/post.rb', line 21

def nsfw?
  over_18
end