oembed 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +69 -5
- data/lib/oembed/client.rb +4 -4
- data/lib/oembed/parser.rb +1 -1
- data/lib/oembed/uri.rb +2 -2
- data/lib/oembed/version.rb +1 -1
- data/lib/oembed/xml_parser.rb +23 -0
- data/lib/oembed.rb +2 -0
- data/oembed.gemspec +1 -1
- data/spec/oembed/client_spec.rb +10 -2
- data/spec/oembed/parser_spec.rb +11 -7
- data/spec/oembed/uri_spec.rb +10 -0
- data/spec/oembed/xml_parser_spec.rb +32 -0
- metadata +7 -3
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -40,7 +40,6 @@ Lets start with a simple example. It will be a client for the awesome
|
|
40
40
|
```ruby
|
41
41
|
require 'oembed'
|
42
42
|
|
43
|
-
# Speaker Deck client
|
44
43
|
class SpeakerDeck
|
45
44
|
include Oembed::Client
|
46
45
|
|
@@ -55,12 +54,12 @@ That's it. Now you can use a method `#fetch` to get data from oEmbed enpoint of
|
|
55
54
|
|
56
55
|
```ruby
|
57
56
|
client = SpeakerDeck.new
|
58
|
-
|
57
|
+
client.fetch('https://speakerdeck.com/u/soulim/p/rails')
|
59
58
|
```
|
60
59
|
|
61
60
|
The method `#fetch` will return a hash with oEmded data.
|
62
61
|
|
63
|
-
```
|
62
|
+
```ruby
|
64
63
|
{
|
65
64
|
"type" => "rich",
|
66
65
|
"version" => 1.0,
|
@@ -70,12 +69,70 @@ The method `#fetch` will return a hash with oEmded data.
|
|
70
69
|
"author_name" => "Alex Soulim",
|
71
70
|
"author_url" => "https://speakerdeck.com/u/soulim",
|
72
71
|
"html" => "<iframe style=\"border:0; padding:0; margin:0; background:transparent;\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\" frameBorder=\"0\" allowTransparency=\"true\" id=\"presentation_frame_4fd3874cebb4b2001f0277e5\" src=\"//speakerdeck.com/embed/4fd3874cebb4b2001f0277e5\" width=\"710\" height=\"596\"></iframe>\n",
|
73
|
-
"width" => 710,
|
72
|
+
"width" => 710,q
|
74
73
|
"height" => 596
|
75
74
|
}
|
76
75
|
```
|
77
76
|
|
78
|
-
|
77
|
+
`oembed` gem supports JSON and XML response formats. Here is an example of
|
78
|
+
client for XML endpoint.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
require 'oembed'
|
82
|
+
|
83
|
+
class Flickr
|
84
|
+
include Oembed::Client
|
85
|
+
|
86
|
+
def endpoint_uri
|
87
|
+
'http://www.flickr.com/services/oembed.xml'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
client = Flickr.new
|
92
|
+
client.fetch('http://www.flickr.com/photos/alex_soulim/3593916989')
|
93
|
+
```
|
94
|
+
|
95
|
+
It will return:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
{
|
99
|
+
"type"=>"photo",
|
100
|
+
"title"=>"IMG_2072",
|
101
|
+
"author_name"=>"Alex Soulim", "author_url"=>"http://www.flickr.com/photos/alex_soulim/",
|
102
|
+
"width"=>"683",
|
103
|
+
"height"=>"1024",
|
104
|
+
"url"=>"http://farm4.staticflickr.com/3618/3593916989_3d8aa991ea_b.jpg",
|
105
|
+
"web_page"=>"http://www.flickr.com/photos/alex_soulim/3593916989/",
|
106
|
+
"thumbnail_url"=>"http://farm4.staticflickr.com/3618/3593916989_3d8aa991ea_s.jpg",
|
107
|
+
"thumbnail_width"=>"75",
|
108
|
+
"thumbnail_height"=>"75",
|
109
|
+
"web_page_short_url"=>"http://flic.kr/p/6tzLj2",
|
110
|
+
"license"=>"All Rights Reserved",
|
111
|
+
"license_id"=>"0",
|
112
|
+
"version"=>"1.0",
|
113
|
+
"cache_age"=>"3600",
|
114
|
+
"provider_name"=>"Flickr",
|
115
|
+
"provider_url"=>"http://www.flickr.com/"
|
116
|
+
}
|
117
|
+
```
|
118
|
+
|
119
|
+
You can make requests with additional parameters. Let's build a client for
|
120
|
+
Instagram and use `:maxwidth` parameter.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
require 'oembed'
|
124
|
+
|
125
|
+
class Instagram
|
126
|
+
include Oembed::Client
|
127
|
+
|
128
|
+
def endpoint_uri
|
129
|
+
'http://api.instagram.com/oembed'
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
client = Instagram.new
|
134
|
+
client.fetch('http://instagr.am/p/BUG/', maxwidth: 300)
|
135
|
+
```
|
79
136
|
|
80
137
|
## Contributing
|
81
138
|
|
@@ -84,3 +141,10 @@ At the moment `oembed` gem supports the JSON format only. I will fix it soon. St
|
|
84
141
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
85
142
|
4. Push to the branch (`git push origin my-new-feature`)
|
86
143
|
5. Create new Pull Request
|
144
|
+
|
145
|
+
## Supported Ruby versions [![Build Status](https://secure.travis-ci.org/soulim/oembed.png)](http://travis-ci.org/soulim/oembed) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/soulim/oembed)
|
146
|
+
|
147
|
+
This library is tested against the following Ruby implementations:
|
148
|
+
|
149
|
+
- Ruby 1.9.2
|
150
|
+
- Ruby 1.9.3
|
data/lib/oembed/client.rb
CHANGED
@@ -12,16 +12,16 @@ module Oembed
|
|
12
12
|
@http ||= Oembed::Http.new
|
13
13
|
end
|
14
14
|
|
15
|
-
def fetch(resource_uri)
|
15
|
+
def fetch(resource_uri, params = {})
|
16
16
|
begin
|
17
|
-
fetch!(resource_uri)
|
17
|
+
fetch!(resource_uri, params)
|
18
18
|
rescue Oembed::Error
|
19
19
|
nil
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def fetch!(resource_uri)
|
24
|
-
uri = Oembed::Uri.new(endpoint_uri, resource_uri)
|
23
|
+
def fetch!(resource_uri, params = {})
|
24
|
+
uri = Oembed::Uri.new(endpoint_uri, resource_uri, params)
|
25
25
|
http.get(uri.to_s)
|
26
26
|
end
|
27
27
|
end
|
data/lib/oembed/parser.rb
CHANGED
data/lib/oembed/uri.rb
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
module Oembed
|
4
4
|
class Uri
|
5
|
-
def initialize(endpoint_uri, resource_uri)
|
5
|
+
def initialize(endpoint_uri, resource_uri, params = {})
|
6
6
|
@uri = URI.parse(endpoint_uri)
|
7
|
-
@uri.query = URI.encode_www_form(url: resource_uri)
|
7
|
+
@uri.query = URI.encode_www_form(params.merge(url: resource_uri))
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_s
|
data/lib/oembed/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Oembed
|
4
|
+
class XmlParser
|
5
|
+
def self.parse(source)
|
6
|
+
begin
|
7
|
+
document = REXML::Document.new(source)
|
8
|
+
traverse(document.root)
|
9
|
+
rescue REXML::ParseException => e
|
10
|
+
raise Oembed::ParserError.new(e), 'XML parser error'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def self.traverse(node)
|
17
|
+
node.elements.to_a.inject({}) do |accum, elem|
|
18
|
+
accum[elem.name] = elem.has_elements? ? traverse(elem) : elem.text
|
19
|
+
accum
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/oembed.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
require 'net/http'
|
3
3
|
require 'json'
|
4
4
|
require 'uri'
|
5
|
+
require 'rexml/document'
|
5
6
|
|
6
7
|
module Oembed
|
7
8
|
class Error < StandardError; end
|
@@ -28,6 +29,7 @@ module Oembed
|
|
28
29
|
|
29
30
|
autoload :Client, 'oembed/client'
|
30
31
|
autoload :Parser, 'oembed/parser'
|
32
|
+
autoload :XmlParser, 'oembed/xml_parser'
|
31
33
|
autoload :Http, 'oembed/http'
|
32
34
|
autoload :Uri, 'oembed/uri'
|
33
35
|
end
|
data/oembed.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = ["soulim@gmail.com"]
|
7
7
|
gem.description = %q{A slim library to work with oEmbed format.}
|
8
8
|
gem.summary = %q{A slim library to work with oEmbed format.}
|
9
|
-
gem.homepage = "http://github.com/
|
9
|
+
gem.homepage = "http://soulim.github.com/oembed"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/spec/oembed/client_spec.rb
CHANGED
@@ -13,6 +13,10 @@ describe Oembed::Client do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
describe '#fetch' do
|
16
|
+
it 'should accept optional params' do
|
17
|
+
expect {subject.fetch(resource_uri, width: 100) }.not_to raise_error
|
18
|
+
end
|
19
|
+
|
16
20
|
it 'should fetch the date using self.fetch!' do
|
17
21
|
subject.should_receive(:fetch!)
|
18
22
|
subject.fetch(resource_uri)
|
@@ -28,9 +32,13 @@ describe Oembed::Client do
|
|
28
32
|
end
|
29
33
|
|
30
34
|
describe '#fetch!' do
|
35
|
+
it 'should accept optional params' do
|
36
|
+
expect {subject.fetch(resource_uri, width: 100) }.not_to raise_error
|
37
|
+
end
|
38
|
+
|
31
39
|
it 'should prepare request URI' do
|
32
|
-
Oembed::Uri.should_receive(:new).with(endpoint_uri, resource_uri)
|
33
|
-
subject.fetch!(resource_uri)
|
40
|
+
Oembed::Uri.should_receive(:new).with(endpoint_uri, resource_uri, width: 100)
|
41
|
+
subject.fetch!(resource_uri, width: 100)
|
34
42
|
end
|
35
43
|
|
36
44
|
it 'should get oEmbed data via HTTP client' do
|
data/spec/oembed/parser_spec.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Oembed::Parser do
|
4
|
-
let(:body) { { 'foo' => 'bar', 'baz' => 1 } }
|
5
|
-
let(:json_body) { '{"foo":"bar","baz":1}' }
|
6
|
-
let(:xml_body)
|
4
|
+
let(:body) { { 'foo' => 'bar', 'baz' => "1" } }
|
5
|
+
let(:json_body) { '{"foo":"bar","baz":"1"}' }
|
6
|
+
let(:xml_body) do
|
7
|
+
%q(<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
8
|
+
<oembed>
|
9
|
+
<foo>bar</foo>
|
10
|
+
<baz>1</baz>
|
11
|
+
</oembed>)
|
12
|
+
end
|
7
13
|
|
8
14
|
describe '#parse' do
|
9
15
|
context 'when content type is JSON' do
|
@@ -13,10 +19,8 @@ describe Oembed::Parser do
|
|
13
19
|
end
|
14
20
|
|
15
21
|
context 'when content type is XML' do
|
16
|
-
it 'should
|
17
|
-
expect
|
18
|
-
subject.parse(xml_body, 'text/xml')
|
19
|
-
}.to raise_error(NotImplementedError)
|
22
|
+
it 'should return parsed body' do
|
23
|
+
expect(subject.parse(xml_body, 'text/xml')).to eq(body)
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
data/spec/oembed/uri_spec.rb
CHANGED
@@ -6,6 +6,16 @@ describe Oembed::Uri do
|
|
6
6
|
|
7
7
|
subject { Oembed::Uri.new(endpoint_uri, resource_uri) }
|
8
8
|
|
9
|
+
it 'should accept optional params' do
|
10
|
+
uri = Oembed::Uri.new(endpoint_uri, resource_uri, width: 100)
|
11
|
+
|
12
|
+
query_string = URI.parse(uri.to_s).query
|
13
|
+
query_data = URI.decode_www_form(query_string)
|
14
|
+
query = Hash[query_data]
|
15
|
+
|
16
|
+
expect(query['width']).to eq('100')
|
17
|
+
end
|
18
|
+
|
9
19
|
describe '#to_s' do
|
10
20
|
it 'should return oEmbed URI string' do
|
11
21
|
expect(subject.to_s).to eq('http://example.com/oembed?url=http%3A%2F%2Fexample.com%2Ffoo')
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Oembed::XmlParser do
|
4
|
+
let(:source) do
|
5
|
+
%q(<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
6
|
+
<oembed>
|
7
|
+
<foo>text</foo>
|
8
|
+
<bar>
|
9
|
+
<baz>1</baz>
|
10
|
+
</bar>
|
11
|
+
</oembed>)
|
12
|
+
end
|
13
|
+
let(:result) { { 'foo' => 'text', 'bar' => { 'baz' => '1' } } }
|
14
|
+
|
15
|
+
describe '.parse' do
|
16
|
+
it 'should parse given source' do
|
17
|
+
expect(Oembed::XmlParser.parse(source)).to eq(result)
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when there is an exception during parsing' do
|
21
|
+
before do
|
22
|
+
REXML::Document.stub(:new).and_raise(REXML::ParseException.new('error'))
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should raise Oembed::ParserError' do
|
26
|
+
expect {
|
27
|
+
Oembed::XmlParser.parse(source)
|
28
|
+
}.to raise_error(Oembed::ParserError)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oembed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -68,6 +68,7 @@ extra_rdoc_files: []
|
|
68
68
|
files:
|
69
69
|
- .gitignore
|
70
70
|
- .rspec
|
71
|
+
- .travis.yml
|
71
72
|
- Gemfile
|
72
73
|
- LICENSE
|
73
74
|
- README.md
|
@@ -78,14 +79,16 @@ files:
|
|
78
79
|
- lib/oembed/parser.rb
|
79
80
|
- lib/oembed/uri.rb
|
80
81
|
- lib/oembed/version.rb
|
82
|
+
- lib/oembed/xml_parser.rb
|
81
83
|
- oembed.gemspec
|
82
84
|
- spec/oembed/client_spec.rb
|
83
85
|
- spec/oembed/http_spec.rb
|
84
86
|
- spec/oembed/parser_spec.rb
|
85
87
|
- spec/oembed/uri_spec.rb
|
88
|
+
- spec/oembed/xml_parser_spec.rb
|
86
89
|
- spec/oembed_spec.rb
|
87
90
|
- spec/spec_helper.rb
|
88
|
-
homepage: http://github.com/
|
91
|
+
homepage: http://soulim.github.com/oembed
|
89
92
|
licenses: []
|
90
93
|
post_install_message:
|
91
94
|
rdoc_options: []
|
@@ -114,5 +117,6 @@ test_files:
|
|
114
117
|
- spec/oembed/http_spec.rb
|
115
118
|
- spec/oembed/parser_spec.rb
|
116
119
|
- spec/oembed/uri_spec.rb
|
120
|
+
- spec/oembed/xml_parser_spec.rb
|
117
121
|
- spec/oembed_spec.rb
|
118
122
|
- spec/spec_helper.rb
|