<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -7,27 +7,31 @@ flickr.name = &quot;Flickr&quot;
 flickr.url = &quot;http://flickr.com/&quot;
 
 # Another one:
-# The default format defaults to json
+# The default format is json
 qik = OEmbed::Provider.new(&quot;http://qik.com/api/oembed.{format}&quot;)
 qik &lt;&lt; &quot;http://qik.com/*&quot;
 
-# Get a raw XML-file from Flickr:
+# Get a raw XML-file from Flickr
 flickr.raw(&quot;http://flickr.com/photos/my_user/1231231312&quot;)
-# Get a raw JSON-file
+# Get a raw JSON-file from Flickr
 flickr.raw(&quot;http://flickr.com/photos/my_user/1231231312&quot;, :format =&gt; :json)
 
-# Returns a OEmbed::Response
-flickr.get(&quot;http://flickr.com/photos/my_user/1231231312&quot;)
-
-# Register them to 
+# Register both providers
 OEmbed::Providers.register(flickr, qik)
 
-OEmbed::Providers.raw(&quot;http://qik.com/test&quot;, :format =&gt; :xml)
+# Get a raw XML-file from whichever provider matches
+OEmbed::Providers.raw(&quot;http://qik.com/video/1&quot;, :format =&gt; :xml)
+
+# Returns a OEmbed::Response using XmlSimple library to parse the response
+res = flickr.get(&quot;http://flickr.com/photos/my_user/1231231312&quot;)
+# Returns a OEmbed::Response using the JSON library to parse the response
+res = flickr.get(&quot;http://flickr.com/photos/my_user/1231231312&quot;, :format =&gt; :json)
 
-res.is_a?(OEmbed::Response)
-res.type
-res.version
-res.author_name
-res.author_url
-res.provider.url
-res.provider.name
\ No newline at end of file
+res.is_a?(OEmbed::Response) # =&gt; true
+res.type # =&gt; &quot;photo&quot;
+res.version # =&gt; &quot;1.0&quot;
+res.html # =&gt; &quot;&lt;img src='http://farm1.static.flickr.com/2312/3123123_123123.jpg' /&gt;&quot;
+res.author_name # =&gt; &quot;my_user&quot;
+res.author_url # =&gt; &quot;http://flickr.com/photos/my_user&quot;
+res.provider.url # =&gt; &quot;http://flickr.com/&quot;
+res.provider.name # =&gt; &quot;Flickr&quot;
\ No newline at end of file</diff>
      <filename>idea.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,9 +1,16 @@
 $:.unshift File.dirname(__FILE__)
 
+%w(json/ext json).each do |lib|
+  begin
+    require lib
+    break
+  rescue LoadError
+  end
+end
+
 begin
-  require 'json/ext'
+  require 'xmlsimple'
 rescue LoadError
-  require 'json'
 end
 
 require 'net/http'</diff>
      <filename>lib/oembed.rb</filename>
    </modified>
    <modified>
      <diff>@@ -11,6 +11,12 @@ module OEmbed
     end
   end
   
+  class FormatNotSupported &lt; StandardError
+    def to_s
+      &quot;This server doesn't have the correct libraries installed to support the '#{super}' format&quot;
+    end
+  end
+  
   class UnknownResponse &lt; StandardError
     def to_s
       &quot;Got unkown response (#{super}) from server&quot;</diff>
      <filename>lib/oembed/errors.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,10 +2,28 @@ module OEmbed
   class Provider
     attr_accessor :format, :name, :url, :urls, :endpoint
     
-    def initialize(endpoint, format = :json)
+    def initialize(endpoint, format = nil)
       @endpoint = endpoint
       @urls = []
+      # Try to use the best available format
       @format = format
+      case @format
+      when :json
+        begin
+          JSON.respond_to?(:load)
+        rescue NameError
+          @format = nil
+        end
+      when :xml
+        begin
+          XmlSimple.respond_to?(:xml_in)
+          @format = :xml
+        rescue NameError
+          @format = nil
+        end
+      else
+        @format = nil
+      end
     end
     
     def &lt;&lt;(url)
@@ -57,7 +75,8 @@ module OEmbed
     end
     
     def get(url, options = {})
-      OEmbed::Response.create_for(raw(url, options.merge(:format =&gt; :json)), self)
+      options[:format] ||= @format if @format
+      OEmbed::Response.create_for(raw(url, options), self, options[:format])
     end                   
     
     def format_in_url?</diff>
      <filename>lib/oembed/provider.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,10 +1,19 @@
 module OEmbed
   class Response
     METHODS = [:define_methods!, :provider, :field, :fields]
-    attr_reader :fields, :provider
+    attr_reader :fields, :provider, :format
     
-    def self.create_for(json, provider)
-      fields = JSON.load(json)
+    def self.create_for(raw, provider, format)
+      begin
+        fields = case format
+        when :xml
+          XmlSimple.xml_in(raw, {'ForceArray'=&gt;false})
+        else
+          JSON.load(raw)
+        end
+      rescue NameError
+        raise OEmbed::FormatNotSupported, (format || :json).to_s
+      end
 
       resp_type = case fields['type']
         when 'photo' : OEmbed::Response::Photo</diff>
      <filename>lib/oembed/response.rb</filename>
    </modified>
    <modified>
      <diff>@@ -6,10 +6,29 @@ describe OEmbed::Provider do
   before(:all) do
     @flickr = OEmbed::Provider.new(&quot;http://www.flickr.com/services/oembed/&quot;)
     @qik = OEmbed::Provider.new(&quot;http://qik.com/api/oembed.{format}&quot;, :xml)
+    @viddler = OEmbed::Provider.new(&quot;http://lab.viddler.com/services/oembed/&quot;, :json)
     
     @flickr &lt;&lt; &quot;http://*.flickr.com/*&quot;
     @qik &lt;&lt; &quot;http://qik.com/video/*&quot;
     @qik &lt;&lt; &quot;http://qik.com/*&quot;
+    @viddler &lt;&lt; &quot;http://*.viddler.com/*&quot;
+  end
+  
+  it &quot;should default to nil&quot; do
+    @flickr.format.should be_nil
+  end
+  
+  it &quot;should allow xml&quot; do
+    @qik.format.should == :xml
+  end
+  
+  it &quot;should allow json&quot; do
+    @viddler.format.should == :json
+  end
+  
+  it &quot;should not allow random formats&quot; do
+    @hulu = OEmbed::Provider.new(&quot;http://www.hulu.com/api/oembed.{format}&quot;, :yml)
+    @hulu.format.should be_nil
   end
   
   it &quot;should add URL schemes&quot; do
@@ -38,7 +57,7 @@ describe OEmbed::Provider do
       uri = @flickr.build(url(:flickr))
       uri.host.should == &quot;www.flickr.com&quot;
       uri.path.should == &quot;/services/oembed/&quot;
-      uri.query.include?(&quot;format=json&quot;).should be_true
+      uri.query.include?(&quot;format=#{@flickr.format}&quot;).should be_true
       uri.query.include?(&quot;url=http://flickr.com/photos/bees/2362225867/&quot;).should be_true
 
       uri = @qik.build(url(:qik))
@@ -111,15 +130,42 @@ describe OEmbed::Provider do
   end
   
   describe &quot;#get&quot; do
-    it &quot;should set the format to json&quot; do
+    it &quot;should send the specified format&quot; do
       @flickr.should_receive(:raw).
-        with(url(:flickr), :format =&gt; :json).
-        and_return('{}')
+        with(url(:flickr), {:format=&gt;:json}).
+        and_return(valid_response(:json))
+      @flickr.get(url(:flickr), :format=&gt;:json)
+      
+      @flickr.should_receive(:raw).
+        with(url(:flickr), {:format=&gt;:xml}).
+        and_return(valid_response(:xml))
+      @flickr.get(url(:flickr), :format=&gt;:xml)
+      
+      @flickr.should_receive(:raw).
+        with(url(:flickr), {:format=&gt;:yml}).
+        and_return(valid_response(:json))
+      @flickr.get(url(:flickr), :format=&gt;:yml)
+    end
+    
+    it &quot;should send the provider's format if none is specified&quot; do
+      @flickr.should_receive(:raw).
+        with(url(:flickr), {}).
+        and_return(valid_response(:json))
       @flickr.get(url(:flickr))
+      
+      @qik.should_receive(:raw).
+        with(url(:qik), :format=&gt;:xml).
+        and_return(valid_response(:xml))
+      @qik.get(url(:qik))
+      
+      @viddler.should_receive(:raw).
+        with(url(:viddler), :format=&gt;:json).
+        and_return(valid_response(:json))
+      @viddler.get(url(:viddler))
     end
 
     it &quot;should return OEmbed::Response&quot; do
-      @flickr.stub!(:raw).and_return('{}')
+      @flickr.stub!(:raw).and_return(valid_response(:json))
       @flickr.get(url(:flickr)).is_a?(OEmbed::Response).should be_true
     end
   end</diff>
      <filename>spec/provider_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,45 +1,76 @@
 require File.dirname(__FILE__) + '/spec_helper'
 
 describe OEmbed::Response do
+  include OEmbedSpecHelper
+  
   before(:all) do
-    data = &lt;&lt;-END
-      {
-        &quot;type&quot;: &quot;photo&quot;,
-        &quot;version&quot;: &quot;1.0&quot;,
-        &quot;fields&quot;: &quot;hello&quot;,
-        &quot;__id__&quot;: 1234
-      }
-    END
-    @res = OEmbed::Response.new(data, OEmbed::Providers::Flickr)
+    @flickr = OEmbed::Provider.new(&quot;http://www.flickr.com/services/oembed/&quot;)
+    @qik = OEmbed::Provider.new(&quot;http://qik.com/api/oembed.{format}&quot;, :xml)
+    @viddler = OEmbed::Provider.new(&quot;http://lab.viddler.com/services/oembed/&quot;, :json)
+    
+    @flickr &lt;&lt; &quot;http://*.flickr.com/*&quot;
+    @qik &lt;&lt; &quot;http://qik.com/video/*&quot;
+    @qik &lt;&lt; &quot;http://qik.com/*&quot;
+    @viddler &lt;&lt; &quot;http://*.viddler.com/*&quot;
+    
+    @new_res = OEmbed::Response.new(valid_response(:object), OEmbed::Providers::OohEmbed)
+    
+    @default_res = OEmbed::Response.create_for(valid_response(:json), @flickr, nil)
+    @xml_res = OEmbed::Response.create_for(valid_response(:xml), @qik, :xml)
+    @json_res = OEmbed::Response.create_for(valid_response(:json), @viddler, :json)
   end
   
   it &quot;should set the provider&quot; do
-    @res.provider.should == OEmbed::Providers::Flickr
+    @new_res.provider.should == OEmbed::Providers::OohEmbed
+    
+    @default_res.provider.should == @flickr
+    @xml_res.provider.should == @qik
+    @json_res.provider.should == @viddler
   end
   
   it &quot;should parse the data into #fields&quot; do
-    @res.fields.should == {
-      &quot;type&quot; =&gt; &quot;photo&quot;,
-      &quot;version&quot; =&gt; &quot;1.0&quot;,
-      &quot;fields&quot; =&gt; &quot;hello&quot;,
-      &quot;__id__&quot; =&gt; 1234
-    }
+    @new_res.fields.keys.should == valid_response(:object).keys
+    
+    @default_res.fields.keys.should == valid_response(:object).keys
+    @xml_res.fields.keys.should == valid_response(:object).keys
+    @json_res.fields.keys.should == valid_response(:object).keys
+  end
+  
+  it &quot;should not parse the incorrect format&quot; do
+    lambda do
+      OEmbed::Response.create_for(valid_response(:xml), @flickr, nil)
+    end.should raise_error(JSON::ParserError)
+    
+    lambda do
+      OEmbed::Response.create_for(valid_response(:xml), @viddler, :json)
+    end.should raise_error(JSON::ParserError)
+    
+    lambda do
+      OEmbed::Response.create_for(valid_response(:json), @viddler, :xml)
+    end.should raise_error(ArgumentError)
+  end
+  
+  it &quot;should access the XML data through #field&quot; do
+    @xml_res.field(:type).should == &quot;photo&quot;
+    @xml_res.field(:version).should == &quot;1.0&quot;
+    @xml_res.field(:fields).should == &quot;hello&quot;
+    @xml_res.field(:__id__).should == &quot;1234&quot;
   end
   
-  it &quot;should access the data through #field&quot; do
-    @res.field(:type).should == &quot;photo&quot;
-    @res.field(:version).should == &quot;1.0&quot;
-    @res.field(:fields).should == &quot;hello&quot;
-    @res.field(:__id__).should == 1234
+  it &quot;should access the JSON data through #field&quot; do
+    @json_res.field(:type).should == &quot;photo&quot;
+    @json_res.field(:version).should == &quot;1.0&quot;
+    @json_res.field(:fields).should == &quot;hello&quot;
+    @json_res.field(:__id__).should == 1234
   end
   
   it &quot;should automagically define helpers&quot; do
-    @res.type.should == &quot;photo&quot;
-    @res.version.should == &quot;1.0&quot;
+    @default_res.type.should == &quot;photo&quot;
+    @default_res.version.should == &quot;1.0&quot;
   end
   
   it &quot;should protect important methods&quot; do
-    @res.fields.should_not == @res.field(:fields)
-    @res.__id__.should_not == @res.field(:__id__)
+    @default_res.fields.should_not == @default_res.field(:fields)
+    @default_res.__id__.should_not == @default_res.field(:__id__)
   end
 end
\ No newline at end of file</diff>
      <filename>spec/response_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -14,4 +14,35 @@ module OEmbedSpecHelper
     return &quot;http://fake.com/&quot; if site == :fake
     EXAMPLE[site]
   end
+  
+  def valid_response(format)
+    case format
+    when :object
+      {
+        &quot;type&quot; =&gt; &quot;photo&quot;,
+        &quot;version&quot; =&gt; &quot;1.0&quot;,
+        &quot;fields&quot; =&gt; &quot;hello&quot;,
+        &quot;__id__&quot; =&gt; 1234
+      }
+    when :json
+      &lt;&lt;-JSON
+        {
+          &quot;type&quot;: &quot;photo&quot;,
+          &quot;version&quot;: &quot;1.0&quot;,
+          &quot;fields&quot;: &quot;hello&quot;,
+          &quot;__id__&quot;: 1234
+        }
+      JSON
+    when :xml
+      &lt;&lt;-XML
+        &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;yes&quot;?&gt;
+        &lt;oembed&gt;
+        	&lt;type&gt;photo&lt;/type&gt;
+        	&lt;version&gt;1.0&lt;/version&gt;
+        	&lt;fields&gt;hello&lt;/fields&gt;
+        	&lt;__id__&gt;1234&lt;/__id__&gt;
+        &lt;/oembed&gt;
+      XML
+    end
+  end
 end
\ No newline at end of file</diff>
      <filename>spec/spec_helper.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>167fab2b0959e9ef25dd613c6596fa0b23e32ee7</id>
    </parent>
  </parents>
  <author>
    <name>Marcos Wright Kuhns</name>
    <email>webmaster@kuhnsfam.com</email>
  </author>
  <url>http://github.com/judofyr/ruby-oembed/commit/93cbe4f4d32ec88bc32116a59fc70c4f52ed109d</url>
  <id>93cbe4f4d32ec88bc32116a59fc70c4f52ed109d</id>
  <committed-date>2009-01-27T09:09:54-08:00</committed-date>
  <authored-date>2009-01-26T18:25:52-08:00</authored-date>
  <message>Initial work allowing the use of the XmlSimple library instead of JSON

Signed-off-by: Magnus Holm &lt;judofyr@gmail.com&gt;</message>
  <tree>811395b1d112e61201476daad0a38246001e107f</tree>
  <committer>
    <name>Magnus Holm</name>
    <email>judofyr@gmail.com</email>
  </committer>
</commit>
