microby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test, :development do
4
+ gem "rspec"
5
+ gem 'guard-rspec'
6
+ gem 'rb-fsevent', '~> 0.9.1'
7
+ end
8
+
9
+ gemspec
@@ -0,0 +1,6 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 G5
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,112 @@
1
+ # Microby
2
+
3
+ A Ruby gem to parse HTML containing a Microformat 2 h-feed element, returning an a collection of
4
+ serialized h-entry objects. The returned Object structure looks something like this:
5
+
6
+ - HFeed
7
+ - HEntry
8
+ - name
9
+ - summary
10
+ - email
11
+ - content
12
+ - author
13
+ - HCard
14
+ - categories
15
+ - {}
16
+ - HEntry
17
+ - ...
18
+
19
+ [Microformats H-Entry](http://microformats.org/wiki/microformats-2#h-entry)
20
+
21
+
22
+ ## Current Version
23
+
24
+ 0.0.1
25
+
26
+
27
+ ## Requirements
28
+
29
+ * [Nokogiri](http://nokogiri.org)
30
+
31
+
32
+
33
+ ## Installation
34
+
35
+ ### Gemfile
36
+
37
+ Add this line to your application's Gemfile.
38
+
39
+ ```ruby
40
+ gem 'microby'
41
+ ```
42
+
43
+ ### Manual
44
+
45
+ Or install it yourself.
46
+
47
+ ```bash
48
+ gem install microby
49
+ ```
50
+
51
+
52
+ ## Usage
53
+
54
+ ```ruby
55
+ Microby.parse(File|URL)
56
+ ```
57
+ [Example Gist](https://raw.github.com/gist/84b2c0b0ea991cc12dfb/84c735583bb90d345a8ede203a3df3d6cb486e10/g5-hub-feed.html)
58
+
59
+ ## Authors
60
+
61
+ * Bookis Smuin / [@bookis](https://github.com/bookis)
62
+
63
+ ## Contributions
64
+
65
+ 1. Fork it
66
+ 2. Get it running (see Installation above)
67
+ 3. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 4. Write your code and **specs**
69
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 6. Push to the branch (`git push origin my-new-feature`)
71
+ 7. Create new Pull Request
72
+
73
+ If you find bugs, have feature requests or questions, please
74
+ [file an issue](https://github.com/G5/picture_tag-rails/issues).
75
+
76
+ ### Specs
77
+
78
+ ```bash
79
+ rake spec
80
+ ```
81
+
82
+ ### Releases
83
+
84
+ ```bash
85
+ TODO how do you do a release?
86
+ ```
87
+
88
+
89
+ ## License
90
+
91
+ Copyright (c) 2012 G5
92
+
93
+ MIT License
94
+
95
+ Permission is hereby granted, free of charge, to any person obtaining
96
+ a copy of this software and associated documentation files (the
97
+ "Software"), to deal in the Software without restriction, including
98
+ without limitation the rights to use, copy, modify, merge, publish,
99
+ distribute, sublicense, and/or sell copies of the Software, and to
100
+ permit persons to whom the Software is furnished to do so, subject to
101
+ the following conditions:
102
+
103
+ The above copyright notice and this permission notice shall be
104
+ included in all copies or substantial portions of the Software.
105
+
106
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
107
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
108
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
109
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
110
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
111
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
112
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ Gem.find_files("microby/**/*.rb").each { |path| require path }
5
+
6
+ module Microby
7
+ FormatClass = %w(p n e i u dt)
8
+ def self.parse(html)
9
+ HFeed.new(html)
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module Microby
2
+ class Element
3
+ def initialize(microformat)
4
+ parse_elements(microformat)
5
+ end
6
+
7
+ def parse_elements(microformat)
8
+ FormatClass.each do |letter|
9
+ microformat.css(">*[class*=#{letter}-]").each do |a|
10
+ self[symbolize_class(a["class"])] = a.text.gsub('\n', " ").strip
11
+ end
12
+ end
13
+ end
14
+
15
+ def symbolize_class(klass)
16
+ klass.to_s.downcase.split.first.gsub(/\w{1,2}-/, "").to_sym
17
+ end
18
+
19
+ def []=(key, value)
20
+ self.send(key.to_s + "=", value) if self.respond_to?(key.to_s + "=")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Microby
2
+ class HCard < Element
3
+ attr_accessor :name, :email, :url
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ module Microby
2
+ class HEntry < Element
3
+
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
+
12
+ def parse_elements(microformat)
13
+ FormatClass.each do |letter|
14
+ microformat.css(">*[class*=#{letter}-]").each do |attrs|
15
+ attrs["class"].split.each do |klass|
16
+ parse_element(attrs, klass)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def parse_element(microformat, klass)
25
+ case klass
26
+ when 'h-card'
27
+ self.author = HCard.new(microformat)
28
+ when 'p-category'
29
+ self.categories[microformat.text.gsub('\n', " ").strip] = microformat["href"]
30
+ when 'e-content'
31
+ self.content = microformat.inner_html
32
+ when'dt-published'
33
+ self.published_at = microformat["datetime"]
34
+ when "u-uid"
35
+ self.bookmark = microformat["href"]
36
+ else
37
+ self[symbolize_class(klass)] = microformat.text.gsub('\n', " ").strip
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ module Microby
2
+ class HFeed
3
+ attr_accessor :entries
4
+ def initialize(html)
5
+ @entries = []
6
+ parse_html(html)
7
+ end
8
+
9
+ def parse_html(html)
10
+ doc = Nokogiri::HTML(open html)
11
+ doc.css("*[class*=h-feed]").each do |feed|
12
+ feed.css(".h-entry").each do |mf_entry|
13
+ entry = HEntry.new(mf_entry)
14
+ self.entries << entry
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Microby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'microby/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "microby"
8
+ gem.version = Microby::VERSION
9
+ gem.authors = ["Bookis Smuin"]
10
+ gem.email = ["vegan.bookis@gmail.com"]
11
+ gem.description = %q{A hATOM feed parser}
12
+ gem.summary = %q{Takes in HTML containing an h-feed classed element and returns serialized data based on the Microformat 2 hEntry specs}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'nokogiri'
21
+
22
+ end
@@ -0,0 +1,12 @@
1
+ require 'microby'
2
+
3
+ describe Microby::HEntry do
4
+ let(:result) { Microby.parse(File.open("spec/support/example.html")) }
5
+ let(:entry) { result.entries.first }
6
+ subject { entry.author }
7
+
8
+ 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
+
12
+ end
@@ -0,0 +1,58 @@
1
+ require 'microby'
2
+
3
+ describe Microby::HEntry do
4
+ let(:result) { Microby.parse(File.open("spec/support/example.html")) }
5
+ let(:entry) { result.entries.first }
6
+
7
+ it "should have an array of entries" do
8
+ entry.should be_an_instance_of Microby::HEntry
9
+ end
10
+
11
+ it "has a name" do
12
+ entry.name.should eq "Senior Cat Living"
13
+ end
14
+
15
+ it "has a summary" do
16
+ entry.summary.should eq "Signed up with 3 locations"
17
+ end
18
+
19
+ it "has a time" do
20
+ entry.published_at.should eq "2012-08-26 20:09-0700"
21
+ end
22
+
23
+ it "has a bookmark" do
24
+ entry.bookmark.should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700"
25
+ end
26
+
27
+ it "has an author as an hcard" do
28
+ entry.author.should be_an_instance_of Microby::HCard
29
+ end
30
+
31
+ describe "categories" do
32
+ it "has an array of categories" do
33
+ entry.categories.should be_an_instance_of Hash
34
+ end
35
+
36
+ it "has a key of the content" do
37
+ entry.categories["New Customer"].should eq "http://g5.com/tag/new-customer"
38
+ end
39
+ end
40
+
41
+ describe "content" do
42
+ let(:content) { entry.content }
43
+
44
+ it "should be a blob of html" do
45
+ content.should match /Locations/
46
+ end
47
+
48
+ it "should be a blob of html" do
49
+ content.should match /\<dt\>/
50
+ end
51
+
52
+ it "should be a blob of html" do
53
+ content.should_not match /time/
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,5 @@
1
+ require 'microby'
2
+
3
+ describe Microby::HFeed do
4
+
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'microby'
2
+
3
+ describe Microby do
4
+
5
+ it { should respond_to :parse }
6
+
7
+ describe "parsing a file" do
8
+ let(:result) { Microby.parse(File.open("spec/support/example.html")) }
9
+
10
+ it "should be a Microby object" do
11
+ result.should be_an_instance_of Microby::HFeed
12
+ end
13
+
14
+ it "should have 2 enties" do
15
+ result.entries.size.should eq 2
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require 'rubygems'
2
+ require 'microby'
@@ -0,0 +1,157 @@
1
+ <section class="h-feed">
2
+ <article class="h-entry">
3
+ <h1 class="p-name">Senior Cat Living</h1>
4
+ <p class="woot p-summary woot">Signed up with 3 locations</p>
5
+ <p class="p-five">Signed up with 3 locations</p>
6
+ <time datetime="2012-08-26 20:09-0700" class="dt-published">
7
+ 8:09pm on August 26th, 2012
8
+ </time>
9
+ <a class="p-category" href="http://g5.com/tag/new-customer" rel="tag">
10
+ New Customer
11
+ </a>
12
+ <a class="u-url u-uid" href="http://g5.com/feed/entries/2012-08-26-20-09-0700">
13
+ http://g5.com/feed/entries/2012-08-26-20-09-0700
14
+ </a>
15
+ <div class="p-author h-card">
16
+ <p class="p-name">Jessica Suttles</p>
17
+ <a class="u-email" href="mailto:jessica.suttles@getg5.com">
18
+ jessica.suttles@getg5.com
19
+ </a>
20
+ <a class="u-url u-uid" href="http//g5.com/authors/jessica-suttles">
21
+ http//g5.com/authors/jessica-suttles
22
+ </a>
23
+ </div>
24
+ <dl class="e-content">
25
+ <dt>Customer</dt>
26
+ <dd class="h-card">
27
+ <p class="p-name">Senior Cat Living</p>
28
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living">
29
+ http//g5.com/customers/senior-cat-living
30
+ </a>
31
+ </dd>
32
+ <dt>Corporate</dt>
33
+ <dd class="h-card">
34
+ <p class="p-name">Senior Cat Living Corporate</p>
35
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/corporate">
36
+ http//g5.com/customers/senior-cat-living/corporate
37
+ </a>
38
+ </dd>
39
+ <dt>Locations</dt>
40
+ <dd class="h-card">
41
+ <p class="p-name">Senior Cat Living Location 1</p>
42
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/locations/1">
43
+ http//g5.com/customers/senior-cat-living/locations/1
44
+ </a>
45
+ </dd>
46
+ <dd class="h-card">
47
+ <p class="p-name">Senior Cat Living Location 2</p>
48
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/locations/2">
49
+ http//g5.com/customers/senior-cat-living/locations/2
50
+ </a>
51
+ </dd>
52
+ <dd class="h-card">
53
+ <p class="p-name">Senior Cat Living Location 3</p>
54
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/locations/3">
55
+ http//g5.com/customers/senior-cat-living/locations/3
56
+ </a>
57
+ </dd>
58
+ <dt>Features</dt>
59
+ <dd class="h-product">
60
+ <p class="p-name">Content</p>
61
+ <a class="u-url u-uid" href="http//g5.com/features/content">
62
+ http//g5.com/feature/content
63
+ </a>
64
+ </dd>
65
+ <dd class="h-product">
66
+ <p class="p-name">Insight</p>
67
+ <a class="u-url u-uid" href="http//g5.com/features/insight">
68
+ http//g5.com/feature/insight
69
+ </a>
70
+ </dd>
71
+ <dd class="h-product">
72
+ <p class="p-name">Support</p>
73
+ <a class="u-url u-uid" href="http//g5.com/features/support">
74
+ http//g5.com/feature/support
75
+ </a>
76
+ </dd>
77
+ </dl>
78
+ </article>
79
+ <article class="h-entry">
80
+ <h1 class="p-name">Senior Cat Living</h1>
81
+ <p class="woot p-summary woot">Signed up with 3 locations</p>
82
+ <p class="p-five">Signed up with 3 locations</p>
83
+ <time datetime="2012-08-26 20:09-0700" class="dt-published">
84
+ 8:09pm on August 26th, 2012
85
+ </time>
86
+ <a class="p-category" href="http://g5.com/tag/new-customer" rel="tag">
87
+ New Customer
88
+ </a>
89
+ <a class="u-url u-uid" href="http://g5.com/feed/entries/2012-08-26-20-09-0700">
90
+ http://g5.com/feed/entries/2012-08-26-20-09-0700
91
+ </a>
92
+ <div class="p-author h-card">
93
+ <p class="p-name">Jessica Suttles</p>
94
+ <a class="u-email" href="mailto:jessica.suttles@getg5.com">
95
+ jessica.suttles@getg5.com
96
+ </a>
97
+ <a class="u-url u-uid" href="http//g5.com/authors/jessica-suttles">
98
+ http//g5.com/authors/jessica-suttles
99
+ </a>
100
+ </div>
101
+ <dl class="e-content">
102
+ <dt>Customer</dt>
103
+ <dd class="h-card">
104
+ <p class="p-name">Senior Cat Living</p>
105
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living">
106
+ http//g5.com/customers/senior-cat-living
107
+ </a>
108
+ </dd>
109
+ <dt>Corporate</dt>
110
+ <dd class="h-card">
111
+ <p class="p-name">Senior Cat Living Corporate</p>
112
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/corporate">
113
+ http//g5.com/customers/senior-cat-living/corporate
114
+ </a>
115
+ </dd>
116
+ <dt>Locations</dt>
117
+ <dd class="h-card">
118
+ <p class="p-name">Senior Cat Living Location 1</p>
119
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/locations/1">
120
+ http//g5.com/customers/senior-cat-living/locations/1
121
+ </a>
122
+ </dd>
123
+ <dd class="h-card">
124
+ <p class="p-name">Senior Cat Living Location 2</p>
125
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/locations/2">
126
+ http//g5.com/customers/senior-cat-living/locations/2
127
+ </a>
128
+ </dd>
129
+ <dd class="h-card">
130
+ <p class="h-five p-name">Senior Cat Living Location 3</p>
131
+ <a class="u-url u-uid" href="http//g5.com/customers/senior-cat-living/locations/3">
132
+ http//g5.com/customers/senior-cat-living/locations/3
133
+ </a>
134
+ </dd>
135
+ <dt>Features</dt>
136
+ <dd class="h-product">
137
+ <p class="p-name">Content</p>
138
+ <a class="u-url u-uid" href="http//g5.com/features/content">
139
+ http//g5.com/feature/content
140
+ </a>
141
+ </dd>
142
+ <dd class="h-product">
143
+ <p class="p-name">Insight</p>
144
+ <a class="u-url u-uid" href="http//g5.com/features/insight">
145
+ http//g5.com/feature/insight
146
+ </a>
147
+ </dd>
148
+ <dd class="h-product">
149
+ <p class="p-name">Support</p>
150
+ <a class="u-url u-uid" href="http//g5.com/features/support">
151
+ http//g5.com/feature/support
152
+ </a>
153
+ </dd>
154
+ </dl>
155
+ </article>
156
+
157
+ </section>
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: microby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bookis Smuin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A hATOM feed parser
31
+ email:
32
+ - vegan.bookis@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Guardfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/microby.rb
44
+ - lib/microby/element.rb
45
+ - lib/microby/h_card.rb
46
+ - lib/microby/h_entry.rb
47
+ - lib/microby/h_feed.rb
48
+ - lib/microby/version.rb
49
+ - microby.gemspec
50
+ - spec/lib/microby/h_card_spec.rb
51
+ - spec/lib/microby/h_entry_spec.rb
52
+ - spec/lib/microby/h_feed_spec.rb
53
+ - spec/lib/microby_spec.rb
54
+ - spec/spec_helper.rb
55
+ - spec/support/example.html
56
+ homepage: ''
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.24
77
+ signing_key:
78
+ 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:
82
+ - spec/lib/microby/h_card_spec.rb
83
+ - spec/lib/microby/h_entry_spec.rb
84
+ - spec/lib/microby/h_feed_spec.rb
85
+ - spec/lib/microby_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/example.html