public
Description: oEmbed for Ruby
Homepage: http://oembed.com/
Clone URL: git://github.com/judofyr/ruby-oembed.git
ruby-oembed / spec / response_spec.rb
100644 90 lines (71 sloc) 3.166 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
require File.dirname(__FILE__) + '/spec_helper'
 
describe OEmbed::Response do
  include OEmbedSpecHelper
  
  before(:all) do
    @flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
    @qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", :xml)
    @viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", :json)
    
    @flickr << "http://*.flickr.com/*"
    @qik << "http://qik.com/video/*"
    @qik << "http://qik.com/*"
    @viddler << "http://*.viddler.com/*"
    
    @new_res = OEmbed::Response.new(valid_response(:object), OEmbed::Providers::OohEmbed)
    
    @default_res = OEmbed::Response.create_for(valid_response(:json), @flickr)
    @xml_res = OEmbed::Response.create_for(valid_response(:xml), @qik, :xml)
    @json_res = OEmbed::Response.create_for(valid_response(:json), @viddler, :json)
  end
  
  it "should set the provider" do
    @new_res.provider.should == OEmbed::Providers::OohEmbed
    
    @default_res.provider.should == @flickr
    @xml_res.provider.should == @qik
    @json_res.provider.should == @viddler
  end
  
  it "should parse the data into #fields" do
    @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 "should only allow JSON or XML" do
    lambda do
      OEmbed::Response.create_for(valid_response(:json), @flickr, :json)
    end.should_not raise_error(OEmbed::FormatNotSupported)
    
    lambda do
      OEmbed::Response.create_for(valid_response(:xml), @flickr, :xml)
    end.should_not raise_error(OEmbed::FormatNotSupported)
    
    lambda do
      OEmbed::Response.create_for(valid_response(:yml), @flickr, :yml)
    end.should raise_error(OEmbed::FormatNotSupported)
  end
  
  it "should not parse the incorrect format" do
    lambda do
      OEmbed::Response.create_for(valid_response(:xml), @flickr)
    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 "should access the XML data through #field" do
    @xml_res.field(:type).should == "photo"
    @xml_res.field(:version).should == "1.0"
    @xml_res.field(:fields).should == "hello"
    @xml_res.field(:__id__).should == "1234"
  end
  
  it "should access the JSON data through #field" do
    @json_res.field(:type).should == "photo"
    @json_res.field(:version).should == "1.0"
    @json_res.field(:fields).should == "hello"
    @json_res.field(:__id__).should == 1234
  end
  
  it "should automagically define helpers" do
    @default_res.type.should == "photo"
    @default_res.version.should == "1.0"
  end
  
  it "should protect important methods" do
    @default_res.fields.should_not == @default_res.field(:fields)
    @default_res.__id__.should_not == @default_res.field(:__id__)
  end
end