open_graph_protocol 0.0.2 → 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.
@@ -1,28 +1,76 @@
1
1
  module OpenGraphProtocol
2
2
  module ViewHelpers
3
3
 
4
- def og_title(text)
5
- og_tag('og:title', text)
4
+ # produce meta tags for the passed map
5
+ def og(map)
6
+ # delegate to recursive algorithm
7
+ og_tags_from_map(map)
6
8
  end
7
9
 
8
- def og_type(type)
9
- og_tag('og:type', type)
10
- end
10
+ private
11
11
 
12
- def og_image(image)
13
- og_tag('og:image', image)
12
+ # recursively iterate a multi-demensional map and procude tags
13
+ def og_tags_from_map(map, parent = nil, response = nil)
14
+ #create the response if it doesn't exist
15
+ response = ActiveSupport::SafeBuffer.new if response.nil?
16
+ # iterate over the map producing tags
17
+ map.each do |key,value|
18
+ # skip keys with empty values
19
+ next if value.nil? or value.empty?
20
+ # delegate where the value is an array
21
+ if value.is_a? Array
22
+ og_tag_from_array value, og_create_parent(key, parent), response
23
+ next
24
+ # delegate when the value is a map
25
+ elsif value.is_a? Hash
26
+ og_tags_from_map value, og_create_parent(key, parent), response
27
+ next
28
+ end
29
+ # otherwise, append the tag
30
+ og_append_tag key, value, response, parent
31
+ end
32
+ response
14
33
  end
15
34
 
16
- def og_url(url)
17
- og_tag('og:url', url)
35
+ # deal with an element in the map being an array
36
+ def og_tag_from_array(array, parent = nil, response = nil)
37
+ array.each do |value|
38
+ # delegate if the value is a hash
39
+ if value.is_a? Hash
40
+ og_tags_from_map value, parent, response
41
+ next
42
+ end
43
+ # otherwise, append the tag
44
+ og_append_tag parent, value, response
45
+ end
18
46
  end
19
47
 
20
- private
48
+ # if there's a parent, pre-pend it to the key
49
+ def og_create_parent(key, parent = nil)
50
+ return "#{parent}:#{key.to_s}" unless parent.nil?
51
+ key.to_s
52
+ end
21
53
 
22
- def og_tag(property, content)
23
- return if content.nil? or content.empty?
24
- tag :meta, {property: property, content: content}
54
+ # append a tag to the response
55
+ def og_append_tag(key, value, response, parent = nil)
56
+ #create the response if it doesn't exist
57
+ response = ActiveSupport::SafeBuffer.new if response.nil?
58
+ # put new line between elements
59
+ response << "\n" unless response.empty?
60
+ # write the tag
61
+ response << og_tag(key, value, parent)
25
62
  end
26
63
 
64
+ # generate a tag for the key, value pair
65
+ def og_tag(key, value, parent = nil)
66
+ # start with "og:"
67
+ property = "og:"
68
+ # next comes the parent, if it exists
69
+ property << "#{parent}:" unless parent.nil?
70
+ # and finally the key
71
+ property << key.to_s
72
+ # spit out the meta tag
73
+ tag :meta, {property: property, content: value}
74
+ end
27
75
  end
28
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_graph_protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
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: 2013-02-26 00:00:00.000000000 Z
13
+ date: 2013-02-28 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: A gem to facilitate populating the open graph protocol metatags on a
16
16
  web page as part of a rails helper.