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:



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

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=“”>]



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

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

#post(slug) ⇒ Object

returns: a post - Postwave::Post

Raises:



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

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]



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

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



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

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:



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

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]



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

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