Class: Postwave::Client
- Inherits:
-
Object
- Object
- Postwave::Client
- 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
- #archive(by: "year") ⇒ Object
-
#index(offset: 0, limit: nil) ⇒ Object
returns: an array of PostStub Structs - [<struct PostStub date=Time, title=“”, stub=“”>].
-
#initialize(config_path, preload: false) ⇒ Client
constructor
A new instance of Client.
-
#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>.
-
#post(slug) ⇒ Object
returns: a post - Postwave::Post.
-
#posts(offset: 0, limit: nil, tag: nil) ⇒ Object
returns: an array of posts - [Postwave::Post].
-
#rss ⇒ Object
returns: a string of the xml representing an RSS feed of posts - String.
-
#tag(tag) ⇒ Object
returns: a Tag Struct - <Tag tag: “”, count: Integer, post_slugs: [“post-slug”,..].
-
#tags ⇒ Object
returns: an array of tags - [String].
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.
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
#archive(by: "year") ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/postwave/client.rb', line 33 def archive(by: "year") working_index = @full_index || get_full_index post_hash = posts.group_by { |post| post.date.year }.transform_values { |posts| posts.sort_by(&:date) } if by == "month" post_hash.each do |key, value| post_hash[key] = value.group_by { |post| post.date.month } end end post_hash end |
#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>
86 87 88 89 90 91 92 93 |
# File 'lib/postwave/client.rb', line 86 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
46 47 48 49 50 51 52 |
# File 'lib/postwave/client.rb', line 46 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]
55 56 57 58 59 60 |
# File 'lib/postwave/client.rb', line 55 def posts(offset: 0, limit: nil, tag: nil) posts = @all_posts || get_all_posts posts = posts.select { |post| post..include?(tag) } if tag count = limit || posts.size posts[offset, count] end |
#rss ⇒ Object
returns: a string of the xml representing an RSS feed of posts - String
79 80 81 82 83 |
# File 'lib/postwave/client.rb', line 79 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”,..]
69 70 71 72 73 74 75 76 |
# File 'lib/postwave/client.rb', line 69 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 |
#tags ⇒ Object
returns: an array of tags - [String]
63 64 65 66 |
# File 'lib/postwave/client.rb', line 63 def summary = @blog_summary || get_summary summary[:tags] end |