html-schema 0.2.0.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,14 +5,19 @@ require 'rake/gempackagetask'
5
5
  spec = Gem::Specification.new do |s|
6
6
  s.name = "html-schema"
7
7
  s.authors = ["Lance Pollard"]
8
- s.version = "0.2.0.1"
9
- s.description = "Unified Ruby API for HTML5 Microdata and Microformats"
10
- s.summary = "Unified Ruby API for HTML5 Microdata and Microformats"
8
+ s.version = "0.5.0"
9
+ s.summary = "Schema.org API for Ruby (Microdata and Microformats under one API)"
10
+ s.description = %{
11
+ A unified interface to the semantic web. You tell your ERB/Haml this is a "person" object,
12
+ and it will give you the schema.org microdata, the microformats, or both. Cuts out having to
13
+ remember these "non-human" keys :).
14
+ }
11
15
  s.homepage = "http://github.com/viatropos/html-schema"
16
+ s.license = "MIT"
12
17
  s.email = "lancejpollard@gmail.com"
13
18
  s.rubyforge_project = "html-schema"
14
19
  s.platform = Gem::Platform::RUBY
15
- s.files = %w(Rakefile) + Dir["{lib,rails,spec}/**/*"] - Dir["spec/tmp"]
20
+ s.files = %w(Rakefile) + Dir["{lib}/**/*"]
16
21
  s.require_path = "lib"
17
22
  end
18
23
 
@@ -1,6 +1,6 @@
1
1
  require 'active_support/core_ext'
2
2
 
3
- $:.unshift File.dirname(File.expand_path(__FILE__)) + "/html-schema"
3
+ $:.unshift File.dirname(File.expand_path(__FILE__))
4
4
 
5
5
  class HTMLSchema
6
6
  class << self
@@ -60,24 +60,64 @@ class HTMLSchema
60
60
 
61
61
  end
62
62
 
63
+ def to_hash(options = {})
64
+ recursively_stringify_keys(api.types.keys.inject({}) do |hash, key|
65
+ hash[key] = api.types[key].to_object
66
+ hash
67
+ end, options)
68
+ end
69
+
70
+ def to_yaml
71
+ to_hash.to_yaml
72
+ end
73
+
63
74
  protected
64
75
  def define_api_method(name)
65
76
  self.class.send :define_method, name do
66
77
  self.api[name]
67
78
  end unless self.respond_to?(name)
68
79
  end
80
+
81
+ def compact_keys
82
+ @compact_keys ||= {
83
+ :itemtype => "t",
84
+ :itemscope => "s",
85
+ :itemprop => "p",
86
+ :class => "c",
87
+ :rel => "r",
88
+ :type => "k"
89
+ }
90
+ end
91
+
92
+ private
93
+ def recursively_stringify_keys(hash = {}, options = {})
94
+ hash.keys.inject({}) do |result, key|
95
+ value = hash[key]
96
+ result[options[:compact] ? (compact_keys[key] || key.to_s) : key.to_s] = case value
97
+ when ::Hash
98
+ recursively_stringify_keys(value, options)
99
+ when ::Array
100
+ value.map(&:to_s)
101
+ else
102
+ value.to_s
103
+ end
104
+ result
105
+ end
106
+ end
69
107
  end
70
108
 
71
- require 'object'
72
- require 'attribute'
73
- require 'configuration'
74
- require 'dsl'
75
- require 'api/object'
76
- require 'api/attribute'
77
- require 'microdata/object'
78
- require 'microdata/attribute'
79
- require 'microformat/object'
80
- require 'microformat/attribute'
81
- require 'api'
82
- require 'microdata'
83
- require 'microformat'
109
+ require 'html-schema/railtie'
110
+ require 'html-schema/helper'
111
+ require 'html-schema/object'
112
+ require 'html-schema/attribute'
113
+ require 'html-schema/configuration'
114
+ require 'html-schema/dsl'
115
+ require 'html-schema/api/object'
116
+ require 'html-schema/api/attribute'
117
+ require 'html-schema/microdata/object'
118
+ require 'html-schema/microdata/attribute'
119
+ require 'html-schema/microformat/object'
120
+ require 'html-schema/microformat/attribute'
121
+ require 'html-schema/api'
122
+ require 'html-schema/microdata'
123
+ require 'html-schema/microformat'
@@ -10,16 +10,35 @@ class HTMLSchema
10
10
  @microformat ||= HTMLSchema.instance.microformat[_name]
11
11
  end
12
12
 
13
- def to_microdata
14
- microdata.to_hash
13
+ def to_hash
14
+ result = {}
15
+ deep_merge(result, microdata.to_hash) if microdata
16
+ deep_merge(result, microformat.to_hash) if microformat
17
+ result
15
18
  end
16
19
 
17
- def to_microformat
18
- microformat.to_hash
20
+ def to_object
21
+ result = {}
22
+ deep_merge(result, microdata.to_object) if microdata
23
+ deep_merge(result, microformat.to_object) if microformat
24
+ result
19
25
  end
20
26
 
21
- def to_hash
22
- to_microdata.merge(to_microformat)
27
+ private
28
+ def deep_merge(result, object = {})
29
+ object.each do |key, value|
30
+ if result.has_key?(key)
31
+ case result[key]
32
+ when ::Hash
33
+ result[key] = deep_merge(result[key], value)
34
+ when ::Array
35
+ result[key] |= value
36
+ end
37
+ else
38
+ result[key] = value
39
+ end
40
+ end
41
+ result
23
42
  end
24
43
  end
25
44
  end
@@ -9,6 +9,7 @@ class HTMLSchema
9
9
  @as = options[:as] || name
10
10
  @classes = Array(as).map(&:to_s)
11
11
  @required = options[:required] == true
12
+ @type = options[:type] || :string
12
13
 
13
14
  @options = options.except(:as, :parent, :required, :type)
14
15
  @options.each do |key, value|
@@ -34,6 +35,14 @@ class HTMLSchema
34
35
  @value = format(value)
35
36
  end
36
37
 
38
+ def to_hash
39
+ {}
40
+ end
41
+
42
+ def to_object
43
+ to_hash.merge(:type => type)
44
+ end
45
+
37
46
  class << self
38
47
  def attribute?
39
48
  true
@@ -19,5 +19,11 @@ class HTMLSchema
19
19
  def []=(key, value)
20
20
  types[key] = value
21
21
  end
22
+
23
+ def to_hash
24
+ types.keys.inject({}) do |hash, key|
25
+ hash[key] = types[key].to_hash
26
+ end
27
+ end
22
28
  end
23
29
  end
@@ -24,7 +24,7 @@ class HTMLSchema
24
24
  attribute :country, :as => :addressCountry#, :type => :place, :source => :country
25
25
  end
26
26
  end
27
-
27
+
28
28
  type :composition, :as => :Composition do
29
29
  attribute :abstract, :as => :about, :type => :base
30
30
  attribute :average_rating, :as => :aggregateRating, :type => :rating
@@ -56,7 +56,7 @@ class HTMLSchema
56
56
 
57
57
  # dates
58
58
  attribute :published_at, :as => :datePublished, :type => :date
59
- attribute :updated_at, :as => :datePublished, :type => :date
59
+ #attribute :updated_at, :as => :datePublished, :type => :date
60
60
 
61
61
  type :blog, :as => :Blog
62
62
  type :book, :as => :Book
@@ -36,6 +36,12 @@ class HTMLSchema
36
36
  attributes[key]
37
37
  end
38
38
 
39
+ def to_object
40
+ result = to_hash.merge(:attributes => attributes.keys.inject({}) { |hash, key| hash[key] = attributes[key].to_object ; hash })
41
+ result[:parent] = parent._name if parent
42
+ result
43
+ end
44
+
39
45
  protected
40
46
  def format_class
41
47
  @format_class ||= "#{self.class.name.split("::")[0..-2].join("::")}".constantize
@@ -0,0 +1,9 @@
1
+ class HTMLSchema
2
+ class Railtie < Rails::Railtie
3
+ initializer "htmlschema.insert_into_action_view" do
4
+ ActiveSupport.on_load :action_view do
5
+ ActionView::Base.send :include, HTMLSchema::Helper
6
+ end
7
+ end
8
+ end if defined?(::Rails)
9
+ end
metadata CHANGED
@@ -1,14 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-schema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 93
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 2
9
- - 0
10
- - 1
11
- version: 0.2.0.1
4
+ prerelease:
5
+ version: 0.5.0
12
6
  platform: ruby
13
7
  authors:
14
8
  - Lance Pollard
@@ -16,11 +10,13 @@ autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
12
 
19
- date: 2011-08-06 00:00:00 -05:00
20
- default_executable:
13
+ date: 2011-09-20 00:00:00 Z
21
14
  dependencies: []
22
15
 
23
- description: Unified Ruby API for HTML5 Microdata and Microformats
16
+ description: "\n\
17
+ A unified interface to the semantic web. You tell your ERB/Haml this is a \"person\" object,\n\
18
+ and it will give you the schema.org microdata, the microformats, or both. Cuts out having to\n\
19
+ remember these \"non-human\" keys :).\n"
24
20
  email: lancejpollard@gmail.com
25
21
  executables: []
26
22
 
@@ -44,13 +40,11 @@ files:
44
40
  - lib/html-schema/microformat/object.rb
45
41
  - lib/html-schema/microformat.rb
46
42
  - lib/html-schema/object.rb
43
+ - lib/html-schema/railtie.rb
47
44
  - lib/html-schema.rb
48
- - spec/schema_spec.rb
49
- - spec/spec_helper.rb
50
- has_rdoc: true
51
45
  homepage: http://github.com/viatropos/html-schema
52
- licenses: []
53
-
46
+ licenses:
47
+ - MIT
54
48
  post_install_message:
55
49
  rdoc_options: []
56
50
 
@@ -61,25 +55,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
55
  requirements:
62
56
  - - ">="
63
57
  - !ruby/object:Gem::Version
64
- hash: 3
65
- segments:
66
- - 0
67
58
  version: "0"
68
59
  required_rubygems_version: !ruby/object:Gem::Requirement
69
60
  none: false
70
61
  requirements:
71
62
  - - ">="
72
63
  - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
64
  version: "0"
77
65
  requirements: []
78
66
 
79
67
  rubyforge_project: html-schema
80
- rubygems_version: 1.3.7
68
+ rubygems_version: 1.8.10
81
69
  signing_key:
82
70
  specification_version: 3
83
- summary: Unified Ruby API for HTML5 Microdata and Microformats
71
+ summary: Schema.org API for Ruby (Microdata and Microformats under one API)
84
72
  test_files: []
85
73
 
@@ -1,87 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe HTMLSchema do
4
- context ":output" do
5
- before do
6
- @schema = HTMLSchema.instance
7
- end
8
-
9
- context ":organization" do
10
- before do
11
- @organization = @schema.organization
12
- end
13
-
14
- it "should have attributes" do
15
- @organization.to_hash.should == {
16
- :itemscope => "itemscope",
17
- :itemtype => "http://schema.org/Organization",
18
- :class => "org"
19
- }
20
-
21
- @organization.name.to_hash.should == {:itemprop => "name", :class => "organization-name"}
22
- @organization.description.to_hash.should == {:itemprop => "description", :class => "description"}
23
- @organization.url.to_hash.should == {:itemprop => "url", :class => "url"}
24
- @organization.image.to_hash.should == {:itemprop => "image", :class => "image"}
25
- end
26
- end
27
-
28
- context ":person" do
29
- before do
30
- @person = @schema.person
31
- end
32
-
33
- it "should have attributes" do
34
- @person.to_hash.should == {
35
- #:itemprop => "person",
36
- :itemscope => "itemscope",
37
- :itemtype => "http://schema.org/Person",
38
- :class => "vcard"
39
- }
40
- @person.name.to_hash.should == {:itemprop => "name", :class => "fn"}
41
- @person.url.to_hash.should == {:itemprop => "url", :class => "url"}
42
- @person.image.to_hash.should == {:itemprop => "image", :class => "image"}
43
- end
44
- end
45
-
46
- context ":address" do
47
- before do
48
- @address = @schema.address
49
- end
50
-
51
- it "should have attributes" do
52
- @address.to_hash.should == {
53
- #:itemprop => "address",
54
- :itemscope => "itemscope",
55
- :itemtype => "http://schema.org/PostalAddress",
56
- :class => "adr"
57
- }
58
- @address.name.to_hash.should == {:itemprop => "name", :class => "name"}
59
- @address.url.to_hash.should == {:itemprop => "url", :class => "url"}
60
- @address.image.to_hash.should == {:itemprop => "image", :class => "image"}
61
- end
62
- end
63
-
64
- context ":article" do
65
- before do
66
- @article = @schema.article
67
- end
68
-
69
- it "should have attributes" do
70
- @article.to_hash.should == {
71
- :itemscope => "itemscope",
72
- :itemtype => "http://schema.org/Article",
73
- :class => "hentry"
74
- }
75
-
76
- @article.title.to_hash.should == {:itemprop => "title", :class => "entry-title"}
77
- @article.tag.to_hash.should == {:itemprop => "keywords", :rel => "tag", :class => "tag"}
78
- @article.description.to_hash.should == {:itemprop => "description", :class => "entry-summary"}
79
- @article.body.to_hash.should == {:itemprop => "articleBody", :class => "entry-content"}
80
- @article.published_at.to_hash.should == {:itemprop => "datePublished", :class => "published"}
81
- @article.updated_at.to_hash.should == {:class => "updated"}
82
- @article.author.to_hash.should == {:itemprop => "author", :class => "author"}
83
- @article.url.to_hash.should == {:itemprop => "url", :class => "url"}
84
- end
85
- end
86
- end
87
- end
@@ -1,14 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- require 'active_support/core_ext'
4
- require 'rspec'
5
-
6
- # rspec spec/microdata/base_spec.rb
7
-
8
- RSpec.configure do |config|
9
-
10
- end
11
-
12
- $:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
13
-
14
- require 'html-schema'