hentry_consumer 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,74 @@
1
1
  module HentryConsumer
2
2
  class Element
3
- attr_accessor :element
3
+ attr_accessor :element, :items
4
4
 
5
+ # If a single element is give, return that
6
+ # If many elements are given, return as array
5
7
  def initialize(element)
6
8
  @element = element
7
- parse_elements(@element)
9
+ @items = parse_elements(@element)
8
10
  end
9
11
 
10
12
  def parse_elements(elements)
13
+ # if there are many elements, parse each of them
11
14
  if elements.is_a?(Nokogiri::XML::NodeSet)
12
- elements.each do |element|
15
+ # return an array of microformats contained in elements
16
+ elements.map do |element|
13
17
  parse_elements(element)
14
- end
15
- else
18
+ # just give me a simple array
19
+ end.flatten.compact
20
+ elsif elements.is_a?(Nokogiri::XML::Element)
16
21
  parse_element(elements)
22
+ else
23
+ nil # nothing to see here, move along
17
24
  end
18
25
  end
19
26
 
20
27
  def parse_element(element)
21
28
  classes = element["class"]
22
- # element may be a microformat element
29
+ # if element has class with microformat prefix it must be a microformat
23
30
  if classes =~ /(p|n|e|i|u|dt)-/
24
- classes.split.each do |c|
31
+ classes = cleanse_classes(classes)
32
+ # parse the element for each microformat class
33
+ classes.split.map do |c|
25
34
  parse_microformat(element, c)
26
35
  end
27
- # element may contain a microformat element
28
- else
36
+ # if element has children it may contain a microformat element
37
+ elsif element.children
29
38
  parse_elements(element.children)
39
+ else
40
+ nil # nothing to see here, move along
30
41
  end
31
42
  end
32
43
 
44
+ # :(
45
+ def cleanse_classes(classes)
46
+ if classes =~ /(h-card)/
47
+ if classes =~ /(p-author)/ || classes =~ /(e-g5-client)/
48
+ classes.gsub!(/h-card/, "")
49
+ end
50
+ end
51
+ classes.strip
52
+ end
53
+
33
54
  def parse_microformat(element, c)
34
- assign_value(symbolize_class(c), element.text)
55
+ method = :"parse_#{c.underscore}"
56
+ if self.respond_to?(method)
57
+ self.send(method, element)
58
+ else
59
+ nil
60
+ end
61
+ end
62
+
63
+ def parse_h_card(element)
64
+ HCard.new(element.children)
65
+ end
66
+
67
+ def parse_h_entry(element)
68
+ HEntry.new(element.children)
35
69
  end
36
70
 
71
+
37
72
  def symbolize_class(c)
38
73
  c.to_s.downcase.gsub(/\w{1,2}-/, "").to_sym
39
74
  end
@@ -57,6 +92,18 @@ module HentryConsumer
57
92
  end
58
93
  end
59
94
 
95
+ def to_hash
96
+ { :type => ["element"],
97
+ :properties => {
98
+ :items => self.items,
99
+ }
100
+ }
101
+ end
102
+
103
+ def to_json(*a)
104
+ to_hash.to_json(a)
105
+ end
106
+
60
107
  def to_html
61
108
  @element.to_html
62
109
  end
@@ -3,18 +3,32 @@ module HentryConsumer
3
3
  attr_accessor :name, :email, :url
4
4
  alias_method :emails, :email
5
5
  alias_method :urls, :url
6
+
7
+ def parse_p_name(element)
8
+ assign_value :name, element.text
9
+ end
10
+
11
+ def parse_u_email(element)
12
+ assign_value :email, element["href"]
13
+ end
14
+
15
+ def parse_u_url(element)
16
+ assign_value :url, element["href"]
17
+ end
6
18
 
19
+ def to_hash
20
+ {
21
+ :type => ["h-card"],
22
+ :properties => {
23
+ :name => self.name,
24
+ :email => self.email,
25
+ :url => self.url
26
+ }
27
+ }
28
+ end
29
+
7
30
  def to_json(*a)
8
- {:items =>
9
- [{
10
- :type => ["h-card"],
11
- :properties => {
12
- :name => self.name,
13
- :email => self.email,
14
- :url => self.url
15
- }
16
- }]
17
- }.to_json(a)
31
+ to_hash.to_json(a)
18
32
  end
19
33
  end
20
34
  end
@@ -3,58 +3,52 @@ module HentryConsumer
3
3
  attr_accessor :name, :categories, :author, :content, :bookmark, :published_at, :summary
4
4
  alias_method :authors, :author
5
5
 
6
- # overrides Element#parse_microformat
7
- def parse_microformat(element, c)
8
- case c
9
- when "p-author" then parse_author(element)
10
- when "p-category" then parse_category(element)
11
- when "e-content" then parse_content(element)
12
- when "dt-published" then parse_published(element)
13
- when "u-uid" then parse_uid(element)
14
- else parse_general(element, c)
15
- end
16
- end
17
-
18
- def parse_author(element)
6
+ def parse_p_name(element)
7
+ assign_value :name, element.text
8
+ end
9
+
10
+ def parse_p_author(element)
19
11
  assign_value :author, HCard.new(element.children)
20
12
  end
21
13
 
22
- def parse_category(element)
14
+ def parse_p_category(element)
23
15
  self.categories ||= {}
24
16
  self.categories[element.text.gsub("\n", " ").strip] = element["href"]
25
17
  end
26
18
 
27
- def parse_content(element)
19
+ def parse_e_content(element)
28
20
  assign_value :content, element.inner_html
29
21
  end
30
22
 
31
- def parse_published(element)
23
+ def parse_dt_published(element)
32
24
  assign_value :published_at, element["datetime"]
33
25
  end
34
26
 
35
- def parse_uid(element)
27
+ def parse_u_uid(element)
36
28
  assign_value :bookmark, element["href"]
37
29
  end
38
30
 
39
- def parse_general(element, c)
40
- assign_value symbolize_class(c), element.text
31
+ def parse_p_summary(element)
32
+ assign_value :summary, element.text
33
+ end
34
+
35
+ def to_hash
36
+ {
37
+ :type => ["h-entry"],
38
+ :properties => {
39
+ :name => self.name,
40
+ :categories => self.categories,
41
+ :author => self.author,
42
+ :content => self.content,
43
+ :bookmark => self.bookmark,
44
+ :published_at => self.published_at,
45
+ :summary => self.summary
46
+ }
47
+ }
41
48
  end
42
49
 
43
50
  def to_json(*a)
44
- {:items =>
45
- [{
46
- :type => ["h-entry"],
47
- :properties => {
48
- :name => self.name,
49
- :categories => self.categories,
50
- :author => self.author,
51
- :content => self.content,
52
- :bookmark => self.bookmark,
53
- :published_at => self.published_at,
54
- :summary => self.summary
55
- }
56
- }]
57
- }.to_json(a)
51
+ to_hash.to_json(a)
58
52
  end
59
53
  end
60
54
  end
@@ -20,13 +20,19 @@ module HentryConsumer
20
20
  end
21
21
  alias_method :to_s, :to_html
22
22
 
23
- def to_json(*a)
24
- json = {:items =>
23
+ def to_hash
24
+ {:items =>
25
25
  [{
26
26
  :type => ["h-feed"],
27
- :properties => {:entries => self.entries}
27
+ :properties => {
28
+ :entries => self.entries
29
+ }
28
30
  }]
29
- }.to_json(a)
31
+ }
32
+ end
33
+
34
+ def to_json(*a)
35
+ to_hash.to_json(a)
30
36
  end
31
37
 
32
38
  end
@@ -1,3 +1,3 @@
1
1
  module HentryConsumer
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -16,3 +16,13 @@ module HentryConsumer
16
16
  other.extend(ClassMethods)
17
17
  end
18
18
  end
19
+
20
+ class String
21
+ def underscore
22
+ self.gsub(/::/, '/').
23
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
24
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
25
+ tr("-", "_").
26
+ downcase
27
+ end
28
+ end
@@ -25,5 +25,4 @@ describe HentryConsumer::FormatRules do
25
25
  it "is unique" do
26
26
  HentryConsumer::FormatRules.unique?(:uid).should be_true
27
27
  end
28
-
29
- end
28
+ end
@@ -12,7 +12,6 @@ describe HentryConsumer::HCard do
12
12
  subject { entry.authors.first }
13
13
 
14
14
  its(:name) { should eq ["Jessica Suttles"]}
15
-
16
15
  its(:emails) { should have(1).things }
17
16
  its(:urls) { should have(2).things }
18
17
  end
@@ -23,7 +22,6 @@ describe HentryConsumer::HCard do
23
22
  subject { entry.authors.first }
24
23
 
25
24
  its(:name) { should eq ["Jessica"]}
26
-
27
25
  its(:emails) { should have(1).things }
28
26
  its(:urls) { should have(1).things }
29
27
  end
@@ -8,37 +8,26 @@ describe HentryConsumer::HEntry do
8
8
  it "should have an array of entries" do
9
9
  entry.should be_an_instance_of HentryConsumer::HEntry
10
10
  end
11
-
12
11
  it "has a name" do
13
12
  entry.name.should eq ["Senior Cat Living"]
14
13
  end
15
-
16
14
  it "has a summary" do
17
15
  entry.summary.should eq ["Signed up with 3 locations"]
18
16
  end
19
-
20
17
  it "has a time" do
21
18
  entry.published_at.should eq ["2012-08-26 20:09-0700"]
22
19
  end
23
-
24
20
  it "has a bookmark" do
25
21
  entry.bookmark.should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700"
26
22
  end
27
-
28
- it "should have 2 authors" do
23
+ it "should have 2 authors as HCards" do
29
24
  entry.authors.should have(2).things HentryConsumer::HCard
30
25
  end
31
-
32
- it "has an author as an hcard" do
33
- entry.authors.first.should be_an_instance_of HentryConsumer::HCard
34
- end
35
-
36
26
  describe "categories" do
37
- it "has an array of categories" do
27
+ it "is a hash" do
38
28
  entry.categories.should be_an_instance_of Hash
39
29
  end
40
-
41
- it "has a key of the content" do
30
+ it "has a key of the category and value of it's UID" do
42
31
  entry.categories["New Customer"].should eq "http://g5.com/tag/new-customer"
43
32
  end
44
33
  end
@@ -49,29 +38,26 @@ describe HentryConsumer::HEntry do
49
38
  it "should be a blob of html" do
50
39
  content.first.should match /Locations/
51
40
  end
52
-
53
41
  it "should be a blob of html" do
54
42
  content.first.should match /\<dt\>/
55
43
  end
56
-
57
44
  it "should be a blob of html" do
58
45
  content.first.should_not match /time/
59
46
  end
60
-
61
47
  end
62
48
 
63
49
  describe "json" do
64
50
  let(:json) { JSON.parse(entry.to_json) }
65
51
 
66
- it { json["items"].should be_an_instance_of Array }
67
- it { json["items"].first["type"].should include 'h-entry'}
68
- it { json["items"].first["properties"]["name"].should eq ['Senior Cat Living']}
69
- it { json["items"].first["properties"]["content"].first.should match /Locations/ }
70
- it { json["items"].first["properties"]["author"].should be_an_instance_of Array }
71
- it { json["items"].first["properties"]["author"].first["items"].first["type"].should include "h-card" }
72
- it { json["items"].first["properties"]["bookmark"].should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700" }
73
- it { json["items"].first["properties"]["published_at"].should eq ["2012-08-26 20:09-0700"] }
74
- it { json["items"].first["properties"]["summary"].should be_an_instance_of Array }
52
+ it { json.should be_an_instance_of Hash }
53
+ it { json["type"].should include 'h-entry'}
54
+ it { json["properties"]["name"].should eq ['Senior Cat Living']}
55
+ it { json["properties"]["content"].first.should match /Locations/ }
56
+ it { json["properties"]["author"].should be_an_instance_of Array }
57
+ it { json["properties"]["author"].first["type"].should include "h-card" }
58
+ it { json["properties"]["bookmark"].should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700" }
59
+ it { json["properties"]["published_at"].should eq ["2012-08-26 20:09-0700"] }
60
+ it { json["properties"]["summary"].should be_an_instance_of Array }
75
61
  end
76
62
  end
77
63
 
@@ -82,37 +68,27 @@ describe HentryConsumer::HEntry do
82
68
  it "should have an array of entries" do
83
69
  entry.should be_an_instance_of HentryConsumer::HEntry
84
70
  end
85
-
86
71
  it "has a name" do
87
72
  entry.name.should eq ["Wabi Sabi Town"]
88
73
  end
89
-
90
74
  it "has a summary" do
91
75
  entry.summary.should eq ["Signed up with 2 locations"]
92
76
  end
93
-
94
77
  it "has a time" do
95
78
  entry.published_at.should eq ["2012-10-10T19:11:17Z"]
96
79
  end
97
-
98
80
  it "has a bookmark" do
99
81
  entry.bookmark.should eq "http://localhost:3000/customers/3"
100
82
  end
101
-
102
- it "should have 1 author" do
83
+ it "should have 1 author as an HCard" do
103
84
  entry.authors.should have(1).things HentryConsumer::HCard
104
85
  end
105
86
 
106
- it "has an author as an hcard" do
107
- entry.authors.first.should be_an_instance_of HentryConsumer::HCard
108
- end
109
-
110
87
  describe "categories" do
111
- it "has an array of categories" do
88
+ it "is a Hash" do
112
89
  entry.categories.should be_an_instance_of Hash
113
90
  end
114
-
115
- it "has a key of the content" do
91
+ it "has a key of the category and value of it's UID" do
116
92
  entry.categories["Some Category"].should eq "#"
117
93
  end
118
94
  end
@@ -123,29 +99,26 @@ describe HentryConsumer::HEntry do
123
99
  it "should be a blob of html" do
124
100
  content.first.should match /Locations/
125
101
  end
126
-
127
102
  it "should be a blob of html" do
128
103
  content.first.should match /\<dt\>/
129
104
  end
130
-
131
105
  it "should be a blob of html" do
132
106
  content.first.should_not match /time/
133
107
  end
134
-
135
108
  end
136
109
 
137
110
  describe "json" do
138
111
  let(:json) { JSON.parse(entry.to_json) }
139
112
 
140
- it { json["items"].should be_an_instance_of Array }
141
- it { json["items"].first["type"].should include 'h-entry'}
142
- it { json["items"].first["properties"]["name"].should eq ['Wabi Sabi Town']}
143
- it { json["items"].first["properties"]["content"].first.should match /Locations/ }
144
- it { json["items"].first["properties"]["author"].should be_an_instance_of Array }
145
- it { json["items"].first["properties"]["author"].first["items"].first["type"].should include "h-card" }
146
- it { json["items"].first["properties"]["bookmark"].should eq "http://localhost:3000/customers/3" }
147
- it { json["items"].first["properties"]["published_at"].should eq ["2012-10-10T19:11:17Z"] }
148
- it { json["items"].first["properties"]["summary"].should be_an_instance_of Array }
113
+ it { json.should be_an_instance_of Hash }
114
+ it { json["type"].should include 'h-entry'}
115
+ it { json["properties"]["name"].should eq ['Wabi Sabi Town']}
116
+ it { json["properties"]["content"].first.should match /Locations/ }
117
+ it { json["properties"]["author"].should be_an_instance_of Array }
118
+ it { json["properties"]["author"].first["type"].should include "h-card" }
119
+ it { json["properties"]["bookmark"].should eq "http://localhost:3000/customers/3" }
120
+ it { json["properties"]["published_at"].should eq ["2012-10-10T19:11:17Z"] }
121
+ it { json["properties"]["summary"].should be_an_instance_of Array }
149
122
  end
150
123
  end
151
124
  end
@@ -7,8 +7,8 @@ describe HentryConsumer::HFeed do
7
7
  let(:json) { JSON.parse(feed.to_json) }
8
8
  it { json["items"].first["type"].should include 'h-feed'}
9
9
  it { json["items"].first["properties"]["entries"].should be_an_instance_of Array }
10
- it { json["items"].first["properties"]["entries"].first["items"].should be_an_instance_of Array }
11
- it { json["items"].first["properties"]["entries"].first["items"].first["type"].should include "h-entry" }
10
+ it { json["items"].first["properties"]["entries"].should be_an_instance_of Array }
11
+ it { json["items"].first["properties"]["entries"].first["type"].should include "h-entry" }
12
12
  end
13
13
 
14
14
  describe "#to_s" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hentry_consumer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-14 00:00:00.000000000 Z
13
+ date: 2012-11-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri
@@ -152,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  segments:
154
154
  - 0
155
- hash: -775147852953388774
155
+ hash: -4504941096207612030
156
156
  required_rubygems_version: !ruby/object:Gem::Requirement
157
157
  none: false
158
158
  requirements:
@@ -161,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  version: '0'
162
162
  segments:
163
163
  - 0
164
- hash: -775147852953388774
164
+ hash: -4504941096207612030
165
165
  requirements: []
166
166
  rubyforge_project:
167
167
  rubygems_version: 1.8.24