class WordpressClient::PostParser

@private

Attributes

data[R]
embedded[R]

Public Class Methods

new(data) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 10
def initialize(data)
  @data = data
  @embedded = data.fetch("_embedded", {})
end
parse(data) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 6
def self.parse(data)
  new(data).to_post
end

Public Instance Methods

to_post() click to toggle source
# File lib/wordpress_client/post_parser.rb, line 15
def to_post
  post = Post.new
  assign_basic(post)
  assign_dates(post)
  assign_rendered(post)
  assign_categories(post)
  assign_tags(post)
  assign_featured_media(post)
  post
end

Private Instance Methods

assign_basic(post) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 29
def assign_basic(post)
  post.id = data["id"]
  post.slug = data["slug"]
  post.url = data["link"]
  post.status = data["status"]
  post.meta = data["meta"]
  post.category_ids = data["categories"]
  post.tag_ids = data["tags"]
  post.featured_media_id = data["featured_media"]
end
assign_categories(post) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 52
def assign_categories(post)
  post.categories = embedded_terms("category").map do |category|
    Category.parse(category)
  end
end
assign_dates(post) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 40
def assign_dates(post)
  post.updated_at = read_date("modified")
  post.date = read_date("date")
end
assign_rendered(post) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 45
def assign_rendered(post)
  post.guid = rendered("guid")
  post.title_html = rendered("title")
  post.excerpt_html = rendered("excerpt")
  post.content_html = rendered("content")
end
assign_tags(post) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 58
def assign_tags(post)
  post.tags = embedded_terms("post_tag").map do |tag|
    Tag.parse(tag)
  end
end
embedded_terms(type) click to toggle source
# File lib/wordpress_client/post_parser.rb, line 75
def embedded_terms(type)
  term_collections = embedded["wp:term"] || embedded["https://api.w.org/term"] || []

  # term_collections is an array of arrays with terms in them. We can see
  # the type of the "collection" by inspecting the first child's taxonomy.
  term_collections.detect { |terms|
    terms.size > 0 && terms.is_a?(Array) && terms.first["taxonomy"] == type
  } || []
end