Class: Postwave::Client

Inherits:
Object
  • Object
show all
Includes:
BlogUtilities
Defined in:
lib/postwave/client.rb

Constant Summary

Constants included from BlogUtilities

BlogUtilities::CONFIG_FILE_NAME, BlogUtilities::INDEX_FILE_NAME, BlogUtilities::META_DIR, BlogUtilities::POSTS_DIR, BlogUtilities::RSS_FILE_NAME, BlogUtilities::SUMMARY_FILE_NAME, BlogUtilities::TAGS_DIR

Instance Method Summary collapse

Methods included from BlogUtilities

#config_values, #directory_paths, #file_paths, #find_missing_paths, #is_set_up?

Constructor Details

#initialize(config_path, preload: false) ⇒ Client

Returns a new instance of Client.

Raises:



17
18
19
20
21
22
23
24
# File 'lib/postwave/client.rb', line 17

def initialize(config_path, preload: false)
  raise MissingConfigError unless is_valid_config?(config_path)
  
  @blog_root = File.dirname(config_path)
  raise InvalidBlogError unless is_set_up?(@blog_root)

  @all_posts = get_all_posts if preload
end

Instance Method Details

#index(offset: 0, limit: nil) ⇒ Object

returns: an array of PostStub Structs - [<struct PostStub date=Time, title=“”, stub=“”>]



27
28
29
30
31
# File 'lib/postwave/client.rb', line 27

def index(offset: 0, limit: nil)
  working_index = @full_index || get_full_index
  count = limit || working_index.size
  working_index[offset, count]
end

#pagination(current_page: 1, per_page: 10) ⇒ Object

reuturns: a Pagination Struct - <struct Pagination current_page=3, prev_page=2, next_page=4, total_pages=20>



74
75
76
77
78
79
80
81
# File 'lib/postwave/client.rb', line 74

def pagination(current_page: 1, per_page: 10)
  summary = @blog_summary || get_summary
  total_pages = (summary[:post_count].to_f / per_page).ceil
  in_bound_current = current_page.clamp(1, total_pages)
  prev_page = in_bound_current > 1 ? in_bound_current - 1 : nil
  next_page = in_bound_current < total_pages ? in_bound_current + 1 : nil
  Postwave::Pagination.new(in_bound_current, prev_page, next_page, total_pages)
end

#post(slug) ⇒ Object

returns: a post - Postwave::Post

Raises:



34
35
36
37
38
39
40
# File 'lib/postwave/client.rb', line 34

def post(slug)
  post_file_path = Dir["#{File.join(@blog_root, POSTS_DIR)}/*#{slug}.md"].first
  
  raise PostNotFoundError unless post_file_path && File.exist?(post_file_path)

  Postwave::Post.new_from_file_path(post_file_path)
end

#posts(offset: 0, limit: nil, tag: nil) ⇒ Object

returns: an array of posts - [Postwave::Post]



43
44
45
46
47
48
# File 'lib/postwave/client.rb', line 43

def posts(offset: 0, limit: nil, tag: nil)
  posts = @all_posts || get_all_posts
  posts = posts.select { |post| post.tags.include?(tag) } if tag
  count = limit || posts.size
  posts[offset, count]
end

#rssObject

returns: a string of the xml representing an RSS feed of posts - String



67
68
69
70
71
# File 'lib/postwave/client.rb', line 67

def rss
  rss_file_path = File.join(@blog_root, POSTS_DIR, META_DIR, RSS_FILE_NAME)
  rss = File.open(rss_file_path)
  rss.read
end

#tag(tag) ⇒ Object

returns: a Tag Struct - <Tag tag: “”, count: Integer, post_slugs: [“post-slug”,..]

Raises:



57
58
59
60
61
62
63
64
# File 'lib/postwave/client.rb', line 57

def tag(tag)
  tag_file_path = File.join(@blog_root, POSTS_DIR, META_DIR, TAGS_DIR, "#{tag}.yaml")
  raise TagNotFoundError unless File.exist?(tag_file_path)

  tag_info = YAML.load_file(tag_file_path)

  Postwave::Tag.new(tag, tag_info[:count], tag_info[:post_slugs])
end

#tagsObject

returns: an array of tags - [String]



51
52
53
54
# File 'lib/postwave/client.rb', line 51

def tags
  summary = @blog_summary || get_summary
  summary[:tags]
end