hypermicrodata 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module Hypermicrodata
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,104 @@
1
+ module Uberous
2
+ class Uber
3
+ attr_reader :data_collection, :error_data_collection, :version
4
+ def initialize(data_collection = [], error_data_collection = [], version = '1.0')
5
+ @data_collection = data_collection
6
+ @error_data_collection = error_data_collection
7
+ @version = version
8
+ end
9
+
10
+ def add_data(data)
11
+ @data_collection << data
12
+ end
13
+
14
+ def add_error_data(data)
15
+ @error_data_collection << data
16
+ end
17
+
18
+ def add_link(rel, url, options = {})
19
+ link = Data.new(options.merge(rel: rel, url: url))
20
+ add_data(link)
21
+ end
22
+
23
+ def to_hash
24
+ data = @data_collection.map(&:to_hash)
25
+ error = @error_data_collection.map(&:to_hash)
26
+ uber = { version: @version }
27
+ uber[:data] = data unless data.empty?
28
+ uber[:error] = error unless error.empty?
29
+ { uber: uber }
30
+ end
31
+ end
32
+
33
+ class Data
34
+ ATTR_NAMES = [:id, :name, :rel, :url, :action, :transclude, :model, :sending, :accepting, :value].freeze
35
+ attr_reader :data_collection
36
+ def initialize(attrs = {})
37
+ @attrs = ATTR_NAMES.each_with_object({}) {|name, h| h[name] = attrs[name] if attrs.has_key?(name) } # slice
38
+ @data_collection = Array(attrs[:data_collection])
39
+ end
40
+
41
+ ATTR_NAMES.each do |attr_name|
42
+ define_method(attr_name) do
43
+ @attrs[attr_name]
44
+ end
45
+ define_method("#{attr_name}=") do |value|
46
+ @attrs[attr_name] = value
47
+ end
48
+ end
49
+
50
+ def add_data(data)
51
+ @data_collection << data
52
+ end
53
+
54
+ def add_link(rel, url, options = {})
55
+ link = Data.new(options.merge(rel: rel, url: url))
56
+ add_data(link)
57
+ end
58
+
59
+ def to_hash
60
+ hash = @attrs.dup
61
+ data = @data_collection.map(&:to_hash)
62
+ hash[:data] = data unless data.empty?
63
+ hash
64
+ end
65
+ end
66
+
67
+ class Loader
68
+ attr_reader :uber
69
+ def initialize(hash)
70
+ uber_hash = hash['uber']
71
+ @uber_data_hashes = uber_hash.fetch('data', [])
72
+ @uber_error_data_hashes = uber_hash.fetch('error', [])
73
+ @uber = Uber.new([], [], uber_hash['version'])
74
+
75
+ load_data
76
+ load_error_data
77
+ end
78
+
79
+ def load_data
80
+ @uber_data_hashes.each do |uber_data_hash|
81
+ @uber.add_data(hash_to_data(uber_data_hash))
82
+ end
83
+ end
84
+
85
+ def load_error_data
86
+ @uber_error_data_hashes.each do |uber_error_data_hash|
87
+ @uber.add_data(hash_to_data(uber_error_data_hash))
88
+ end
89
+ end
90
+
91
+ def hash_to_data(hash)
92
+ data_hash = {}
93
+ hash.each do |k, v|
94
+ data_hash[(k.to_sym rescue k)] = v # symbolize_keys
95
+ end
96
+ child_data_hashes = data_hash.delete(:data) || []
97
+ data = Data.new(data_hash)
98
+ child_data_hashes.each do |child_data_hash|
99
+ data.add_data(hash_to_data(child_data_hash))
100
+ end
101
+ data
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,22 @@
1
+ <!doctype html>
2
+ <html>
3
+ <!-- shameless -->
4
+ <head>
5
+ <title>Jason Ronallo</title>
6
+ </head>
7
+
8
+ <body>
9
+ <span itemscope itemtype="http://schema.org/Person"
10
+ itemid="http://ronallo.com#me">
11
+ <a itemprop="url" href="http://twitter.com/ronallo">
12
+ <span itemprop="name">Jason Ronallo</span>
13
+ </a> is the
14
+ <span itemprop="jobTitle">Associate Head of Digital Library Initiatives</span> at
15
+ <span itemprop="affiliation" itemscope itemtype="http://schema.org/Library" itemid="http://lib.ncsu.edu">
16
+ <span itemprop="name">
17
+ <a itemprop="url" href="http://www.lib.ncsu.edu">NCSU Libraries</a>
18
+ </span>
19
+ </span>.
20
+ </span>
21
+ </body>
22
+ </html>
@@ -0,0 +1,16 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>itemref example from Microdata spec</title>
5
+ </head>
6
+
7
+ <body>
8
+ <div itemscope id="amanda" itemref="a b"></div>
9
+ <p id="a">Name: <span itemprop="name">Amanda</span></p>
10
+ <div id="b" itemprop="band" itemscope itemref="c"></div>
11
+ <div id="c">
12
+ <p>Band: <span itemprop="name">Jazz Band</span></p>
13
+ <p>Size: <span itemprop="size">12</span> players</p>
14
+ </div>
15
+ </body>
16
+ </html>
@@ -0,0 +1,22 @@
1
+ <!doctype html>
2
+ <html>
3
+ <!-- shameless -->
4
+ <head>
5
+ <title>Jason Ronallo</title>
6
+ </head>
7
+
8
+ <body>
9
+ <span itemtype="http://schema.org/Person"
10
+ itemid="http://ronallo.com#me">
11
+ <a itemprop="url" href="http://twitter.com/ronallo">
12
+ <span itemprop="name">Jason Ronallo</span>
13
+ </a> is the
14
+ <span itemprop="jobTitle">Associate Head of Digital Library Initiatives</span> at
15
+ <span itemprop="affiliation" itemtype="http://schema.org/Library" itemid="http://lib.ncsu.edu">
16
+ <span itemprop="name">
17
+ <a itemprop="url" href="http://www.lib.ncsu.edu">NCSU Libraries</a>
18
+ </span>
19
+ </span>.
20
+ </span>
21
+ </body>
22
+ </html>
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'hypermicrodata'
3
+ require 'pry'
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class TestItemref < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @items = Hypermicrodata.get_items('test/data/example_itemref.html')
7
+ end
8
+
9
+ def test_top_item_name
10
+ assert_equal ['Amanda'], @items.first.properties['name']
11
+ end
12
+
13
+ def test_band_name_and_size
14
+ band = @items.first.properties['band'].first
15
+ assert_equal ['Jazz Band'], band.properties['name']
16
+ assert_equal ['12'], band.properties['size']
17
+ end
18
+
19
+ end
data/test/test_json.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class TestJson < Test::Unit::TestCase
4
+
5
+ def setup
6
+ json = Hypermicrodata.to_json('test/data/example_itemref.html')
7
+ @top_item = JSON.parse(json)['items'].first
8
+ end
9
+
10
+ def test_json_without_itemtypes
11
+ assert !@top_item.has_key?('type')
12
+ assert !@top_item['properties']['band'].first.has_key?('type')
13
+ end
14
+
15
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class TestParse < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @items = Hypermicrodata.get_items('test/data/example.html')
7
+ end
8
+
9
+ def test_top_item_type
10
+ assert_equal ['http://schema.org/Person'], @items.first.type
11
+ end
12
+
13
+ def test_top_item_id
14
+ assert_equal "http://ronallo.com#me", @items.first.id
15
+ end
16
+
17
+ def test_top_item_properties
18
+ properties = @items.first.properties
19
+ assert_equal ["Jason Ronallo"], properties['name']
20
+ assert_equal ["http://twitter.com/ronallo"], properties['url']
21
+ assert_equal ["Associate Head of Digital Library Initiatives"], properties['jobTitle']
22
+ end
23
+
24
+ def test_nested_item
25
+ item = @items.first.properties['affiliation'][0]
26
+ assert_equal ['http://schema.org/Library'], item.type
27
+ assert_equal "http://lib.ncsu.edu", item.id
28
+ end
29
+
30
+ def test_nested_item_properties
31
+ properties = @items.first.properties['affiliation'][0].properties
32
+ assert_equal ['NCSU Libraries'], properties['name']
33
+ assert_equal ['http://www.lib.ncsu.edu'], properties['url']
34
+ end
35
+
36
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hypermicrodata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Ronallo
8
+ - Toru KAWAMURA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2014-10-13 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - &id002
21
+ - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: mechanize
28
+ prerelease: false
29
+ requirement: &id003 !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - *id002
32
+ type: :runtime
33
+ version_requirements: *id003
34
+ - !ruby/object:Gem::Dependency
35
+ name: halibut
36
+ prerelease: false
37
+ requirement: &id004 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - *id002
40
+ type: :runtime
41
+ version_requirements: *id004
42
+ - !ruby/object:Gem::Dependency
43
+ name: multi_json
44
+ prerelease: false
45
+ requirement: &id005 !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - *id002
48
+ type: :runtime
49
+ version_requirements: *id005
50
+ - !ruby/object:Gem::Dependency
51
+ name: bundler
52
+ prerelease: false
53
+ requirement: &id006 !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: "1.3"
58
+ type: :development
59
+ version_requirements: *id006
60
+ - !ruby/object:Gem::Dependency
61
+ name: rake
62
+ prerelease: false
63
+ requirement: &id007 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - *id002
66
+ type: :development
67
+ version_requirements: *id007
68
+ description: HTML5 Microdata extractor with Hypermedia
69
+ email:
70
+ - jronallo@gmail.com
71
+ - tkawa@4bit.net
72
+ executables:
73
+ - hypermicrodata.rb
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gitignore
80
+ - .travis.yml
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/hypermicrodata.rb
86
+ - hypermicrodata.gemspec
87
+ - lib/hypermicrodata.rb
88
+ - lib/hypermicrodata/document.rb
89
+ - lib/hypermicrodata/extract.rb
90
+ - lib/hypermicrodata/item.rb
91
+ - lib/hypermicrodata/itemprop_parser.rb
92
+ - lib/hypermicrodata/link.rb
93
+ - lib/hypermicrodata/property.rb
94
+ - lib/hypermicrodata/rails/html_based_json_renderer.rb
95
+ - lib/hypermicrodata/serializer/base.rb
96
+ - lib/hypermicrodata/serializer/hal.rb
97
+ - lib/hypermicrodata/serializer/jsonld.rb
98
+ - lib/hypermicrodata/serializer/uber.rb
99
+ - lib/hypermicrodata/submit_button.rb
100
+ - lib/hypermicrodata/version.rb
101
+ - lib/uberous/uber.rb
102
+ - test/data/example.html
103
+ - test/data/example_itemref.html
104
+ - test/data/example_with_no_itemscope.html
105
+ - test/test_helper.rb
106
+ - test/test_itemref.rb
107
+ - test/test_json.rb
108
+ - test/test_parse.rb
109
+ homepage: https://github.com/tkawa/hypermicrodata
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+
114
+ post_install_message:
115
+ rdoc_options: []
116
+
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - *id002
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - *id002
125
+ requirements: []
126
+
127
+ rubyforge_project:
128
+ rubygems_version: 2.0.14
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Ruby library for extracting HTML5 Microdata with Hypermedia
132
+ test_files:
133
+ - test/data/example.html
134
+ - test/data/example_itemref.html
135
+ - test/data/example_with_no_itemscope.html
136
+ - test/test_helper.rb
137
+ - test/test_itemref.rb
138
+ - test/test_json.rb
139
+ - test/test_parse.rb