Module: OEmbed::Formatter

Defined in:
lib/oembed/formatter.rb,
lib/oembed/formatter/xml.rb,
lib/oembed/formatter/base.rb,
lib/oembed/formatter/json.rb,
lib/oembed/formatter/xml/backends/rexml.rb,
lib/oembed/formatter/json/backends/yaml.rb,
lib/oembed/formatter/xml/backends/nokogiri.rb,
lib/oembed/formatter/json/backends/jsongem.rb,
lib/oembed/formatter/xml/backends/xmlsimple.rb,
lib/oembed/formatter/json/backends/activesupportjson.rb

Overview

Takes the raw response from an oEmbed server and turns it into a nice Hash of data.

Defined Under Namespace

Modules: Base, JSON, XML

Class Method Summary (collapse)

Class Method Details

+ (Object) decode(format, value)

Convert the given value into a nice Hash of values. The format should be the name of the response format (e.g. 'json'). The value should be a String or IO containing the response from an oEmbed server.

For example:

value = '{"version": "1.0", "type": "link", "title": "Some Cool News Article"}'
OEmbed::Formatter.decode('json', value)
#=> {"version": "1.0", "type": "link", "title": "Some Cool News Article"}


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/oembed/formatter.rb', line 38

def decode(format, value)
  supported?(format)
  
  begin
    case format.to_s
    when 'json'
      begin
        JSON.decode(value)
      rescue JSON.backend.parse_error
        raise OEmbed::ParseError, $!.message
      end
    when 'xml'
      begin
        XML.decode(value)
      rescue XML.backend.parse_error
        raise OEmbed::ParseError, $!.message
      end
    end
  rescue
    raise OEmbed::ParseError, "#{$!.class}: #{$!.message}"
  end
end

+ (Object) default

Returns the default format for OEmbed::Provider requests as a String.



11
12
13
14
# File 'lib/oembed/formatter.rb', line 11

def default
  # Listed in order of preference.
  %w{json xml}.detect { |type| supported?(type) rescue false }
end

+ (Boolean) supported?(format)

Given the name of a format we want to know about (e.g. 'json'), returns true if there is a valid backend. If there is no backend, raises OEmbed::FormatNotSupported.

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
# File 'lib/oembed/formatter.rb', line 19

def supported?(format)
  case format.to_s
  when 'json'
    JSON.supported?
  when 'xml'
    XML.supported?
  else
    raise OEmbed::FormatNotSupported, format
  end
end

+ (Object) test_backend(backend_module)

Test the given backend to make sure it parses known values correctly. The backend_module should be either a JSON or XML backend.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oembed/formatter.rb', line 63

def test_backend(backend_module)
  expected = {
    "version"=>1.0,
    "string"=>"test",
    "int"=>42,
    "html"=>"<i>Cool's</i>\n the \"word\"!",
  }
  
  given_value = case backend_module.to_s
  when /OEmbed::Formatter::JSON::Backends::/
    <<-JSON
{"version":"1.0", "string":"test", "int":42,"html":"<i>Cool's</i>\\n the \\"word\\"\\u0021"}
    JSON
  when /OEmbed::Formatter::XML::Backends::/
    <<-XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <version>1.0</version>
  <string>test</string>
  <int>42</int>
  <html>&lt;i&gt;Cool's&lt;/i&gt;\n the &quot;word&quot;&#x21;</html>
</oembed>
    XML
  else
    nil
  end
  
  actual = backend_module.decode(given_value)
  
  # For the test to be true the actual output Hash should have the
  # exact same list of keys _and_ the values should be the same
  # if we ignoring typecasting.
  actual.keys.sort == expected.keys.sort &&
    !actual.detect { |key, value| value.to_s != expected[key].to_s }
end