lib/tophat/opengraph.rb in tophat-1.5.0 vs lib/tophat/opengraph.rb in tophat-1.6.0

- old
+ new

@@ -3,69 +3,97 @@ class OpenGraphGenerator include ActionView::Helpers def initialize(options={}) - TopHat.current['app_id'] = options.delete(:app_id) if options && options.has_key?(:app_id) - TopHat.current['admins'] = options.delete(:admins) if options && options.has_key?(:admins) - TopHat.current['graph_data'] = {} + @app_id = options.delete(:app_id) if options && options.has_key?(:app_id) + @admins = options.delete(:admins) if options && options.has_key?(:admins) + @graph_data = {} end def merge(options={}) - TopHat.current['app_id'] = options.delete(:app_id) if options && options.has_key?(:app_id) - TopHat.current['admins'] = options.delete(:admins) if options && options.has_key?(:admins) + @app_id = options.delete(:app_id) if options && options.has_key?(:app_id) + @admins = options.delete(:admins) if options && options.has_key?(:admins) end def app_id - output = TopHat.current['app_id'] ? tag(:meta, :property => 'fb:app_id', :content => TopHat.current['app_id']) : "" + output = @app_id ? tag(:meta, :property => 'fb:app_id', :content => @app_id) : "" output << '\n' unless output.blank? output end def admins - output = TopHat.current['admins'] ? tag(:meta, :property => 'fb:admins', :content => [*TopHat.current['admins']].join(',')) : "" + output = @admins ? tag(:meta, :property => 'fb:admins', :content => [*@admins].join(',')) : "" output << '\n' unless output.blank? output end def render_graph_data output = "" - TopHat.current['graph_data'].each_pair do |key, value| + @graph_data.each do |key, value| output << tag(:meta, :property => "og:#{key}", :content => value) - output << '\n' if TopHat.current['graph_data'].size > 1 + output << '\n' if @graph_data.size > 1 end output end def type(t) - TopHat.current['graph_data'] ||= {} - TopHat.current['graph_data'][:type] = t + @graph_data ||= {} + @graph_data[:type] = t end def has_graph_data? - TopHat.current['graph_data'] + !!@graph_data end def method_missing(method, *args, &block) #:nodoc - TopHat.current['graph_data'] ||= {} - TopHat.current['graph_data'][method] = args.shift + @graph_data ||= {} + @graph_data[method] = args.shift end end - def html_with_opengraph - '<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:og="http://ogp.me/ns#" - xmlns:fb="https://www.facebook.com/2008/fbml">' + HTML4_XMLNS = [ + 'http://www.w3.org/1999/xhtml', + { :prefix => 'og', :url => 'http://ogp.me/ns#' }, + { :prefix => 'fb', :url => 'https://www.facebook.com/2008/fbml' } + ] + + HTML5_XMLNS = [ + { :prefix => 'og', :url => 'http://opengraphprotocol.org/schema/' }, + { :prefix => 'fb', :url => 'http://developers.facebook.com/schema/' } + ] + + def html_with_opengraph(style='html4') + Kernel.warn("html_with_opengraph has been deprecated, use opengraph_html instead.") + opengraph_html(style) end + def opengraph_html(style='html4') + if style == 'html4' + html_tag(:xmlns => HTML4_XMLNS) + elsif style == 'html5' + html_tag(:version => 'HTML+RDFa 1.0', :xmlns => HTML5_XMLNS) + else + html_tag + end + end + def opengraph(options=nil, &block) if options.kind_of? Hash TopHat.current['open_graph_defaults'] = options end if block_given? - TopHat.current['open_graph_generator'] = OpenGraphGenerator.new(TopHat.current['open_graph_defaults']) - yield(TopHat.current['open_graph_generator']) + if block.arity == 1 + Kernel.warn("passing the graph object into the opengraph method has been deprecated, see README for details.") + + TopHat.current['open_graph_generator'] = OpenGraphGenerator.new(TopHat.current['open_graph_defaults']) + yield(TopHat.current['open_graph_generator']) + else + opengraph_generator = OpenGraphGenerator.new(TopHat.current['open_graph_defaults']) + opengraph_generator.instance_eval(&block) + TopHat.current['open_graph_generator'] = opengraph_generator + end else TopHat.current['open_graph_generator'] ||= OpenGraphGenerator.new TopHat.current['open_graph_generator'].merge(TopHat.current['open_graph_defaults']) output = "" output << TopHat.current['open_graph_generator'].app_id \ No newline at end of file