Class: ButterCMS::Post

Inherits:
Resource show all
Defined in:
lib/butter_cms/post.rb

Defined Under Namespace

Classes: RecordNotFound

Class Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from ButterCMS::Resource

Class Method Details

.allButterCMS::Post

Returns post for given slug if available otherwise raises error RecordNotFound

Returns:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/butter_cms/post.rb', line 7

def self.all
  posts = []
  request_options = { page_size: 10, page: 0 }
  more_posts = true

  while more_posts
    request_options = request_options.merge(page: request_options[:page] + 1)
    fetch_service = ButterCMS::PostsFetchService.new(request_options)
    posts = posts | fetch_service.posts
    more_posts = fetch_service.more_posts?
  end

  posts
end

.find(slug) ⇒ ButterCMS::Post

Returns all available posts from the API

Returns:



25
26
27
28
29
30
31
# File 'lib/butter_cms/post.rb', line 25

def self.find(slug)
  response = ::ButterCMS::Requests::Get.call("posts/#{slug}")
  post_attributes = ::ButterCMS::Parsers::Post.new(response).post
  ::ButterCMS::Parsers::PostObject.call(post_attributes)
rescue RestClient::NotFound
  raise RecordNotFound
end