The purpose of this document is to compare the current Ruby interfaces to the g5_hentry_consumer gem and the microformats2 gem.
require "g5_hentry_consumer"
collection = G5HentryConsumer.parse(document, last_modified_at: last_modified_at)
#=> G5HentryConsumer::HFeed instance
document
must be a String containg a URL or a filepathlast_modified_at
must be a DateTime object
require "microformats2"
collection = Microformats2.parse(document) #=> Microformats2::Collection instance
document
must be a String containing HTML, a URL, or a filepath.
collection.entries #=> Array of G5HentryConsumer::HEntry instances
# plural
collection.entries #=> Array of Microformats2::HEntry instances
# singular
collection.entry #=> collection.entries.first
HEntry
is automagically defined when an.h-entry
is foundCollection#enties
andCollection#entry
are automagically defined when an.h-entry
is found.
?
# plural
collection.cards #=> Array of Microformats2::HCard instances
# singular
collection.card #=> collection.cards.first
HCard
is automagically defined when an.h-card
is foundCollection#cards
andCollection#card
are automagically defined when an.h-card
is found.
collection.entries.first.name #=> Array
collection.entries.first.name.first #=> "Jessica"
# plurarl
collection.entries.first.names #=> Array
collection.entries.first.names.first #=> Microformats::Property::Text instance
collection.entries.first.names.first.to_s #=> "Jessica"
# singular
collection.entry.name #=> Microformats::Property::Text instance
collection.entry.name.to_s #=> "Jessica"
HEntry#names
andHEntry#name
are automagically defined when an.h-entry .p-name
is found.
collection.entries.first.author.first #=> G5HentryConsumer::HCard instance
# plural
collection.entries.first.authors #=> Array
collection.entries.first.authors.first #=> Microformats::Property::Text instance
collection.entries.first.authors.first.formats #=> Array
collection.entries.first.authors.first.formats.first #=> Microformats2::HCard instace
# singular
collection.entry.author #=> Microformats::Property::Text instance
collection.entry.author.format #=> Microformats2::HCard instace
HEntry#authors
andHEntry#author
are automagically defined when an.h-entry .p-author
is found.Property::Text#formats
andProperty::Text#format
are manually defined and assigned when a.p-x.h-x
is found (e.g.,<span class="p-author h-card">…</span>
).
Paraphrased from g5-configurator
class Entry < ActiveRecord::Base
FEED_URL = "http://g5-hub.herokuapp.com/"
class << self
def feed(file_or_url=FEED_URL)
G5HentryConsumer.parse(file_or_url)
end
def consume_feed(file_or_url=FEED_URL)
feed(file_or_url).entries.each do |hentry|
find_or_create_from_hentry(hentry)
end
end
def find_or_create_from_hentry(hentry)
find_or_create_by_uid(hentry.bookmark) do |entry|
entry.client_uid = hentry.content.first.uid # with nil handling
entry.client_name = hentry.content.first.name.first # with nil handling
end
end
end # class << self
end
class Entry < ActiveRecord::Base
FEED_URL = "http://g5-hub.herokuapp.com/"
class << self
def feed(html_or_file_or_url=FEED_URL)
Microformats2.parse(file_or_url)
end
def consume_feed(html_or_file_or_url=FEED_URL)
feed(file_or_url).entry.each do |hentry|
find_or_create_from_hentry(hentry)
end
end
def find_or_create_from_hentry(hentry)
find_or_create_by_uid(hentry.uid) do |entry|
# have to parse .e-content for microformats
collection = Microformats2.parse(hentry.content)
# collection.g5_client if we use vendor extension
# collection.card if we use HCard
entry.client_uid = collection.g5_client.uid.to_s # with nil handling
entry.client_name = collection.g5_client.name.to_s # with nil handling
end
end
end # class << self
end