Module: OEmbed::Formatter::JSON

Defined in:
lib/oembed/formatter/json.rb,
lib/oembed/formatter/json/backends/yaml.rb,
lib/oembed/formatter/json/backends/jsongem.rb,
lib/oembed/formatter/json/backends/activesupportjson.rb

Overview

Handles parsing JSON values using the best available backend.

Defined Under Namespace

Modules: Backends

Constant Summary collapse

DECODERS =

A Array of all available backends, listed in order of preference.

%w(ActiveSupportJSON JSONGem Yaml)

Class Method Summary collapse

Class Method Details

.backendObject

Returns the current JSON backend.



21
22
23
24
25
# File 'lib/oembed/formatter/json.rb', line 21

def backend
  set_default_backend unless defined?(@backend)
  raise OEmbed::FormatNotSupported, :json unless defined?(@backend)
  @backend
end

.backend=(name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oembed/formatter/json.rb', line 27

def backend=(name)
  if name.is_a?(Module)
    @backend = name
  else
    already_required = false
    begin 
      already_required = OEmbed::Formatter::JSON::Backends.const_defined?(name, false)
    rescue ArgumentError # we're dealing with ruby < 1.9 where const_defined? only takes 1 argument, but behaves the way we want it to.
      already_required = OEmbed::Formatter::JSON::Backends.const_defined?(name)
    rescue NameError # no backends have been loaded yet
      already_required = false
    end
    
    require "oembed/formatter/json/backends/#{name.to_s.downcase}" unless already_required
    @backend = OEmbed::Formatter::JSON::Backends::const_get(name)
  end
  @parse_error = @backend::ParseError
end

.decode(json) ⇒ Object

Parses a JSON string or IO and convert it into an object



16
17
18
# File 'lib/oembed/formatter/json.rb', line 16

def decode(json)
  backend.decode(json)
end

.set_default_backendObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/oembed/formatter/json.rb', line 53

def set_default_backend
  DECODERS.find do |name|
    begin
      self.backend = name
      true
    rescue LoadError
      # Try next decoder.
      false
    end
  end
end

.supported?Boolean

Returns true if there is a valid JSON backend. Otherwise, raises OEmbed::FormatNotSupported

Returns:

  • (Boolean)


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

def supported?
  !!backend
end

.with_backend(name) ⇒ Object



46
47
48
49
50
51
# File 'lib/oembed/formatter/json.rb', line 46

def with_backend(name)
  old_backend, self.backend = backend, name
  yield
ensure
  self.backend = old_backend
end