hentry_consumer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,7 +21,7 @@ serialized h-entry objects. The returned Object structure looks something like t
21
21
 
22
22
  ## Current Version
23
23
 
24
- 0.1.2
24
+ 0.1.3
25
25
 
26
26
 
27
27
  ## Requirements
@@ -18,5 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'nokogiri'
21
+ gem.add_dependency 'json'
21
22
 
22
23
  end
@@ -3,11 +3,11 @@ module HentryConsumer
3
3
  def initialize(microformat)
4
4
  parse_elements(microformat)
5
5
  end
6
-
6
+
7
7
  def parse_elements(microformat)
8
8
  FormatClass.each do |letter|
9
9
  microformat.css(">*[class*=#{letter}-]").each do |a|
10
- self[symbolize_class(a["class"])] = a.text.gsub('\n', " ").strip
10
+ assign_value(symbolize_class(a["class"]), a.text.gsub('\n', " ").strip)
11
11
  end
12
12
  end
13
13
  end
@@ -16,8 +16,22 @@ module HentryConsumer
16
16
  klass.to_s.downcase.split.first.gsub(/\w{1,2}-/, "").to_sym
17
17
  end
18
18
 
19
+ def [](key)
20
+ self.send(key)
21
+ end
22
+
19
23
  def []=(key, value)
20
- self.send(key.to_s + "=", value) if self.respond_to?(key.to_s + "=")
24
+ self.send(key.to_s + "=", value)
25
+ end
26
+
27
+ def assign_value(symbolized_class, value)
28
+ return unless self.respond_to?(symbolized_class)
29
+ if FormatRules.can_have_many?(symbolized_class)
30
+ self[symbolized_class] ||= []
31
+ self[symbolized_class] << value
32
+ else
33
+ self[symbolized_class] = value
34
+ end
21
35
  end
22
36
  end
23
37
  end
@@ -0,0 +1,14 @@
1
+ module HentryConsumer
2
+ class FormatRules
3
+ REQUIRED = []
4
+ CAN_HAVE_MANY = [:url, :email, :author, :summary, :hcard]
5
+ class << self
6
+ def required?(format)
7
+ REQUIRED.include? format
8
+ end
9
+ def can_have_many?(format)
10
+ CAN_HAVE_MANY.include? format
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,22 @@
1
1
  module HentryConsumer
2
2
  class HCard < Element
3
3
  attr_accessor :name, :email, :url
4
+ alias_method :emails, :email
5
+ alias_method :urls, :url
6
+
7
+
8
+ def to_json(*a)
9
+ {:items =>
10
+ [{
11
+ :type => ["h-card"],
12
+ :properties => {
13
+ :name => self.name,
14
+ :email => self.email,
15
+ :url => self.url
16
+ }
17
+ }]
18
+ }.to_json(*a)
19
+ end
20
+
4
21
  end
5
22
  end
@@ -2,13 +2,8 @@ module HentryConsumer
2
2
  class HEntry < Element
3
3
 
4
4
  attr_accessor :name, :categories, :author, :content, :bookmark, :published_at, :summary
5
-
6
- # Certainly there is a better way to do this.
7
- def initialize(microformat)
8
- @categories = {}
9
- super
10
- end
11
-
5
+ alias_method :authors, :author
6
+
12
7
  def parse_elements(microformat)
13
8
  FormatClass.each do |letter|
14
9
  microformat.css(">*[class*=#{letter}-]").each do |attrs|
@@ -18,14 +13,32 @@ module HentryConsumer
18
13
  end
19
14
  end
20
15
  end
16
+
17
+ def to_json(*a)
18
+ {:items =>
19
+ [{
20
+ :type => ["h-entry"],
21
+ :properties => {
22
+ :name => self.name,
23
+ :categories => self.categories,
24
+ :author => self.author,
25
+ :content => self.content,
26
+ :bookmark => self.bookmark,
27
+ :published_at => self.published_at,
28
+ :summary => self.summary
29
+ }
30
+ }]
31
+ }.to_json(*a)
32
+ end
21
33
 
22
34
  private
23
35
 
24
36
  def parse_element(microformat, klass)
25
37
  case klass
26
- when 'h-card'
27
- self.author = HCard.new(microformat)
38
+ when 'p-author'
39
+ self.assign_value(symbolize_class(klass), HCard.new(microformat))
28
40
  when 'p-category'
41
+ self.categories ||= {}
29
42
  self.categories[microformat.text.gsub('\n', " ").strip] = microformat["href"]
30
43
  when 'e-content'
31
44
  self.content = parse_content(microformat)
@@ -34,7 +47,7 @@ module HentryConsumer
34
47
  when "u-uid"
35
48
  self.bookmark = microformat["href"]
36
49
  else
37
- self[symbolize_class(klass)] = microformat.text.gsub('\n', " ").strip
50
+ self.assign_value(symbolize_class(klass), microformat.text.gsub('\n', " ").strip)
38
51
  end
39
52
  end
40
53
 
@@ -14,5 +14,14 @@ module HentryConsumer
14
14
  end
15
15
  end
16
16
 
17
+ def to_json(*a)
18
+ {:items =>
19
+ [{
20
+ :type => ["h-feed"],
21
+ :properties => {:entries => self.entries}
22
+ }]
23
+ }.to_json(*a)
24
+ end
25
+
17
26
  end
18
27
  end
@@ -1,3 +1,3 @@
1
1
  module HentryConsumer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'nokogiri'
2
2
  require 'open-uri'
3
+ require 'json'
3
4
  require 'hentry_consumer/element'
4
5
  Gem.find_files("hentry_consumer/**/*.rb").each { |path| require path }
5
6
 
@@ -0,0 +1,21 @@
1
+ require 'hentry_consumer'
2
+
3
+ describe HentryConsumer::FormatRules do
4
+ before do
5
+ stub_const("HentryConsumer::FormatRules::REQUIRED", [:url])
6
+ stub_const("HentryConsumer::FormatRules::CAN_HAVE_MANY", [:url])
7
+ end
8
+
9
+ it "stubs correctly" do
10
+ HentryConsumer::FormatRules::REQUIRED.should include :url
11
+ end
12
+
13
+ it "is required" do
14
+ HentryConsumer::FormatRules.required?(:url).should be_true
15
+ end
16
+
17
+ it "can have many" do
18
+ HentryConsumer::FormatRules.can_have_many?(:url).should be_true
19
+ end
20
+
21
+ end
@@ -1,12 +1,17 @@
1
1
  require 'hentry_consumer'
2
2
 
3
3
  describe HentryConsumer::HEntry do
4
+ before do
5
+ stub_const("HentryConsumer::FormatRules::REQUIRED", [:url, :email])
6
+ stub_const("HentryConsumer::FormatRules::CAN_HAVE_MANY", [:url, :email, :author])
7
+ end
8
+
4
9
  let(:result) { HentryConsumer.parse(File.open("spec/support/example.html")) }
5
10
  let(:entry) { result.entries.first }
6
- subject { entry.author }
7
-
11
+ subject { entry.authors.first }
12
+
8
13
  its(:name) { should eq "Jessica Suttles"}
9
- its(:email) { should eq "jessica.suttles@getg5.com"}
10
- its(:url) { should eq "http//g5.com/authors/jessica-suttles"}
11
-
14
+
15
+ its(:emails) { should have(1).things }
16
+ its(:urls) { should have(2).things }
12
17
  end
@@ -13,7 +13,7 @@ describe HentryConsumer::HEntry do
13
13
  end
14
14
 
15
15
  it "has a summary" do
16
- entry.summary.should eq "Signed up with 3 locations"
16
+ entry.summary.first.should eq "Signed up with 3 locations"
17
17
  end
18
18
 
19
19
  it "has a time" do
@@ -24,8 +24,11 @@ describe HentryConsumer::HEntry do
24
24
  entry.bookmark.should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700"
25
25
  end
26
26
 
27
+ it "should have 2 authors" do
28
+ entry.authors.should have(2).things HentryConsumer::HCard
29
+ end
27
30
  it "has an author as an hcard" do
28
- entry.author.should be_an_instance_of HentryConsumer::HCard
31
+ entry.authors.first.should be_an_instance_of HentryConsumer::HCard
29
32
  end
30
33
 
31
34
  describe "categories" do
@@ -54,5 +57,19 @@ describe HentryConsumer::HEntry do
54
57
  end
55
58
 
56
59
  end
60
+
61
+ describe "json" do
62
+ let(:json) { JSON.parse(entry.to_json) }
63
+
64
+ it { json["items"].should be_an_instance_of Array }
65
+ it { json["items"].first["type"].should include 'h-entry'}
66
+ it { json["items"].first["properties"]["name"].should eq 'Senior Cat Living'}
67
+ it { json["items"].first["properties"]["content"].should match /Locations/ }
68
+ it { json["items"].first["properties"]["author"].should be_an_instance_of Array }
69
+ it { json["items"].first["properties"]["author"].first["items"].first["type"].should include "h-card" }
70
+ it { json["items"].first["properties"]["bookmark"].should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700" }
71
+ it { json["items"].first["properties"]["published_at"].should eq "2012-08-26 20:09-0700" }
72
+ it { json["items"].first["properties"]["summary"].should be_an_instance_of Array }
73
+ end
57
74
 
58
75
  end
@@ -2,4 +2,14 @@ require 'hentry_consumer'
2
2
 
3
3
  describe HentryConsumer::HFeed do
4
4
 
5
+ describe "to json" do
6
+ let(:feed) { HentryConsumer.parse(File.open("spec/support/example.html")) }
7
+ let(:json) { JSON.parse(feed.to_json) }
8
+
9
+ it { json["items"].first["type"].should include 'h-feed'}
10
+ it { json["items"].first["properties"]["entries"].should be_an_instance_of Array }
11
+ it { json["items"].first["properties"]["entries"].first["items"].should be_an_instance_of Array }
12
+ it { json["items"].first["properties"]["entries"].first["items"].first["type"].should include "h-entry" }
13
+
14
+ end
5
15
  end
@@ -20,7 +20,21 @@
20
20
  <a class="u-url u-uid" href="http//g5.com/authors/jessica-suttles">
21
21
  http//g5.com/authors/jessica-suttles
22
22
  </a>
23
+ <a class="u-url u-uid" href="http//g5.com/authors/jessica-suttlez">
24
+ http//g5.com/authors/jessica-suttlez
25
+ </a>
26
+
27
+ </div>
28
+ <div class="p-author h-card">
29
+ <p class="p-name">Bookis Smuin</p>
30
+ <a class="u-email" href="mailto:bookis.smuin@getg5.com">
31
+ bookis.smuin@getg5.com
32
+ </a>
33
+ <a class="u-url u-uid" href="http//g5.com/authors/bookis-smuin">
34
+ http//g5.com/authors/bookis-smuin
35
+ </a>
23
36
  </div>
37
+
24
38
  <dl class="e-content">
25
39
  <dt>Customer</dt>
26
40
  <dd class="h-card">
metadata CHANGED
@@ -1,39 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: hentry_consumer
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 3
10
+ version: 0.1.3
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Bookis Smuin
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-10-05 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-10-15 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: nokogiri
16
- requirement: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
22
32
  type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
23
36
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
37
+ requirement: &id002 !ruby/object:Gem::Requirement
25
38
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
30
48
  description: A hATOM feed parser
31
- email:
49
+ email:
32
50
  - vegan.bookis@gmail.com
33
51
  executables: []
52
+
34
53
  extensions: []
54
+
35
55
  extra_rdoc_files: []
36
- files:
56
+
57
+ files:
37
58
  - .gitignore
38
59
  - Gemfile
39
60
  - Guardfile
@@ -43,10 +64,12 @@ files:
43
64
  - hentry_consumer.gemspec
44
65
  - lib/hentry_consumer.rb
45
66
  - lib/hentry_consumer/element.rb
67
+ - lib/hentry_consumer/format_rules.rb
46
68
  - lib/hentry_consumer/h_card.rb
47
69
  - lib/hentry_consumer/h_entry.rb
48
70
  - lib/hentry_consumer/h_feed.rb
49
71
  - lib/hentry_consumer/version.rb
72
+ - spec/lib/hentry_consumer/format_rules_spec.rb
50
73
  - spec/lib/hentry_consumer/h_card_spec.rb
51
74
  - spec/lib/hentry_consumer/h_entry_spec.rb
52
75
  - spec/lib/hentry_consumer/h_feed_spec.rb
@@ -55,30 +78,39 @@ files:
55
78
  - spec/support/example.html
56
79
  homepage: https://github.com/G5/hentry_consumer
57
80
  licenses: []
81
+
58
82
  post_install_message:
59
83
  rdoc_options: []
60
- require_paths:
84
+
85
+ require_paths:
61
86
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
87
+ required_ruby_version: !ruby/object:Gem::Requirement
63
88
  none: false
64
- requirements:
65
- - - ! '>='
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
97
  none: false
70
- requirements:
71
- - - ! '>='
72
- - !ruby/object:Gem::Version
73
- version: '0'
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
74
105
  requirements: []
106
+
75
107
  rubyforge_project:
76
108
  rubygems_version: 1.8.24
77
109
  signing_key:
78
110
  specification_version: 3
79
- summary: Takes in HTML containing an h-feed classed element and returns serialized
80
- data based on the Microformat 2 hEntry specs
81
- test_files:
111
+ summary: Takes in HTML containing an h-feed classed element and returns serialized data based on the Microformat 2 hEntry specs
112
+ test_files:
113
+ - spec/lib/hentry_consumer/format_rules_spec.rb
82
114
  - spec/lib/hentry_consumer/h_card_spec.rb
83
115
  - spec/lib/hentry_consumer/h_entry_spec.rb
84
116
  - spec/lib/hentry_consumer/h_feed_spec.rb