transcoder 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +7 -0
- data/README.md +10 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/core_ext/autoload.rb +27 -0
- data/lib/transcoder.rb +12 -1
- data/lib/transcoder/parser/feed.rb +2 -2
- data/lib/transcoder/parser/json.rb +2 -2
- data/lib/transcoder/parser/xml.rb +2 -2
- data/lib/transcoder/parser/yaml.rb +14 -0
- data/spec/parser/feed_spec.rb +36 -0
- data/spec/parser/json_spec.rb +7 -0
- data/spec/parser/xml_spec.rb +7 -0
- data/spec/parser/yaml_spec.rb +7 -0
- data/spec/spec.opts +1 -0
- data/spec/transcoder_spec.rb +12 -3
- data/transcoder.gemspec +84 -0
- metadata +16 -4
data/CHANGES
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
= 0.2.0
|
2
|
+
* Added Transcoder.for method for a shorter version of Transcoder::Parser.for
|
3
|
+
* Added Yaml parser
|
4
|
+
* Switched all parsers to use autoload for their associated library dependencies (http://gist.github.com/324367)
|
5
|
+
|
6
|
+
= 0.1.0 (06/02/2010)
|
7
|
+
* Initial release
|
data/README.md
CHANGED
@@ -8,6 +8,10 @@ Say you'd pulled an API based resource using `rest-client`
|
|
8
8
|
* All that you wanted was a ruby object.
|
9
9
|
* You don't want to know anything about the format that the API uses.
|
10
10
|
* You're pretty sure its something pretty common.
|
11
|
+
|
12
|
+
## Install
|
13
|
+
|
14
|
+
gem install transcoder
|
11
15
|
|
12
16
|
## Example
|
13
17
|
$ irb -rubygems
|
@@ -17,7 +21,7 @@ Say you'd pulled an API based resource using `rest-client`
|
|
17
21
|
|
18
22
|
>> rc = RestClient.get('http://vimeo.com/api/v2/benschwarz/likes.json')
|
19
23
|
|
20
|
-
>> Transcoder
|
24
|
+
>> Transcoder.for(rc.headers[:content_type]).parse(rc.to_s)
|
21
25
|
|
22
26
|
What you'll get back is a Ruby object. Lovley.
|
23
27
|
|
@@ -27,6 +31,11 @@ What you'll get back is a Ruby object. Lovley.
|
|
27
31
|
* XML
|
28
32
|
* RSS / Atom
|
29
33
|
|
34
|
+
### Note about RSS / Atom
|
35
|
+
|
36
|
+
Transcoder will return a SimpleRSS object rather than a straight up Ruby hash, this shouldn't be (much) of a reason for concern.
|
37
|
+
This simply means that you'll want to call the properties of the returned object with method calls rather than hash access.
|
38
|
+
|
30
39
|
## Future thoughts
|
31
40
|
|
32
41
|
* Add something to cast attributes to a native ruby object, eg - if its key is created_at, try to date parse it.
|
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/benschwarz/transcoder"
|
12
12
|
gem.authors = ["Ben Schwarz"]
|
13
13
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
-
gem.add_development_dependency "
|
14
|
+
gem.add_development_dependency "rest-client"
|
15
15
|
gem.add_dependency "registry", ">= 0.1.2"
|
16
16
|
gem.add_dependency "json"
|
17
17
|
gem.add_dependency "crack"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Object
|
2
|
+
class << self
|
3
|
+
alias_method :_const_missing, :const_missing
|
4
|
+
private :_const_missing
|
5
|
+
|
6
|
+
def const_missing(constant)
|
7
|
+
if Kernel._autoloads.key? constant
|
8
|
+
require Kernel._autoloads[constant]
|
9
|
+
const_get(constant)
|
10
|
+
else
|
11
|
+
_const_missing(constant)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Kernel
|
18
|
+
module_function :autoload
|
19
|
+
|
20
|
+
def _autoloads
|
21
|
+
@@_autoloads
|
22
|
+
end
|
23
|
+
|
24
|
+
def autoload(const, path)
|
25
|
+
(@@_autoloads||={})[const] = path
|
26
|
+
end
|
27
|
+
end
|
data/lib/transcoder.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
require 'registry'
|
2
2
|
|
3
3
|
module Transcoder
|
4
|
+
VERSION = File.read(File.join(File.dirname(__FILE__), "..", "VERSION"))
|
5
|
+
|
4
6
|
class Parser
|
5
7
|
extend Registry
|
6
8
|
end
|
9
|
+
|
10
|
+
def self.for(mime)
|
11
|
+
Parser.for(mime)
|
12
|
+
end
|
7
13
|
end
|
8
14
|
|
15
|
+
# Autoload that works with Rubygems,
|
16
|
+
# rather than from the current $LOAD_PATH only
|
17
|
+
require 'core_ext/autoload'
|
18
|
+
|
9
19
|
$LOAD_PATH << File.dirname(__FILE__)
|
10
20
|
|
11
21
|
require 'transcoder/parser/json'
|
12
22
|
require 'transcoder/parser/xml'
|
13
|
-
require 'transcoder/parser/feed'
|
23
|
+
require 'transcoder/parser/feed'
|
24
|
+
require 'transcoder/parser/yaml'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Transcoder::Feed do
|
4
|
+
it "should parse the feed and return a ruby object" do
|
5
|
+
atom = <<-ATOM
|
6
|
+
<?xml version="1.0" encoding="utf-8"?>
|
7
|
+
|
8
|
+
<feed xmlns="http://www.w3.org/2005/Atom">
|
9
|
+
|
10
|
+
<title>Example Feed</title>
|
11
|
+
<subtitle>A subtitle.</subtitle>
|
12
|
+
<link href="http://example.org/feed/" rel="self" />
|
13
|
+
<link href="http://example.org/" />
|
14
|
+
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
|
15
|
+
<updated>2003-12-13T18:30:02Z</updated>
|
16
|
+
<author>
|
17
|
+
<name>John Doe</name>
|
18
|
+
<email>johndoe@example.com</email>
|
19
|
+
</author>
|
20
|
+
|
21
|
+
<entry>
|
22
|
+
<title>Atom-Powered Robots Run Amok</title>
|
23
|
+
<link href="http://example.org/2003/12/13/atom03" />
|
24
|
+
<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
|
25
|
+
<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
|
26
|
+
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
|
27
|
+
<updated>2003-12-13T18:30:02Z</updated>
|
28
|
+
<summary>Some text.</summary>
|
29
|
+
</entry>
|
30
|
+
|
31
|
+
</feed>
|
32
|
+
ATOM
|
33
|
+
|
34
|
+
Transcoder.for("application/atom+xml").parse(atom).should be_an_instance_of(SimpleRSS)
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec.opts
CHANGED
data/spec/transcoder_spec.rb
CHANGED
@@ -1,17 +1,26 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "Transcoder" do
|
4
|
+
it "should alias Transcoder.for to Transcoder::Parser.for" do
|
5
|
+
Transcoder::Parser.should_receive(:for).with("application/json")
|
6
|
+
Transcoder.for("application/json")
|
7
|
+
end
|
8
|
+
|
4
9
|
describe "mime-types" do
|
5
10
|
it "should parse json" do
|
6
|
-
Transcoder
|
11
|
+
Transcoder.for('application/json').should == Transcoder::Json
|
7
12
|
end
|
8
13
|
|
9
14
|
it "should parse xml" do
|
10
|
-
Transcoder
|
15
|
+
Transcoder.for('text/xml').should == Transcoder::XML
|
11
16
|
end
|
12
17
|
|
13
18
|
it "should parse rss or atom" do
|
14
|
-
Transcoder
|
19
|
+
Transcoder.for('application/atom+xml').should == Transcoder::Feed
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should parse yaml" do
|
23
|
+
Transcoder.for('application/x-yaml').should == Transcoder::Yaml
|
15
24
|
end
|
16
25
|
end
|
17
26
|
end
|
data/transcoder.gemspec
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{transcoder}
|
8
|
+
s.version = "0.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ben Schwarz"]
|
12
|
+
s.date = %q{2010-03-08}
|
13
|
+
s.description = %q{Transcode from json, xml or atom/rss feeds to Ruby.}
|
14
|
+
s.email = %q{ben.schwarz@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"CHANGES",
|
23
|
+
"LICENSE",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/core_ext/autoload.rb",
|
28
|
+
"lib/transcoder.rb",
|
29
|
+
"lib/transcoder/parser/feed.rb",
|
30
|
+
"lib/transcoder/parser/json.rb",
|
31
|
+
"lib/transcoder/parser/xml.rb",
|
32
|
+
"lib/transcoder/parser/yaml.rb",
|
33
|
+
"spec/parser/feed_spec.rb",
|
34
|
+
"spec/parser/json_spec.rb",
|
35
|
+
"spec/parser/xml_spec.rb",
|
36
|
+
"spec/parser/yaml_spec.rb",
|
37
|
+
"spec/spec.opts",
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/transcoder_spec.rb",
|
40
|
+
"transcoder.gemspec"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/benschwarz/transcoder}
|
43
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.3.6}
|
46
|
+
s.summary = %q{Transcode from common web based formats into plain old Ruby.}
|
47
|
+
s.test_files = [
|
48
|
+
"spec/parser/feed_spec.rb",
|
49
|
+
"spec/parser/json_spec.rb",
|
50
|
+
"spec/parser/xml_spec.rb",
|
51
|
+
"spec/parser/yaml_spec.rb",
|
52
|
+
"spec/spec_helper.rb",
|
53
|
+
"spec/transcoder_spec.rb"
|
54
|
+
]
|
55
|
+
|
56
|
+
if s.respond_to? :specification_version then
|
57
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
|
+
s.specification_version = 3
|
59
|
+
|
60
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
61
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
62
|
+
s.add_development_dependency(%q<rest-client>, [">= 0"])
|
63
|
+
s.add_runtime_dependency(%q<registry>, [">= 0.1.2"])
|
64
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
65
|
+
s.add_runtime_dependency(%q<crack>, [">= 0"])
|
66
|
+
s.add_runtime_dependency(%q<simple-rss>, [">= 0"])
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
69
|
+
s.add_dependency(%q<rest-client>, [">= 0"])
|
70
|
+
s.add_dependency(%q<registry>, [">= 0.1.2"])
|
71
|
+
s.add_dependency(%q<json>, [">= 0"])
|
72
|
+
s.add_dependency(%q<crack>, [">= 0"])
|
73
|
+
s.add_dependency(%q<simple-rss>, [">= 0"])
|
74
|
+
end
|
75
|
+
else
|
76
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
77
|
+
s.add_dependency(%q<rest-client>, [">= 0"])
|
78
|
+
s.add_dependency(%q<registry>, [">= 0.1.2"])
|
79
|
+
s.add_dependency(%q<json>, [">= 0"])
|
80
|
+
s.add_dependency(%q<crack>, [">= 0"])
|
81
|
+
s.add_dependency(%q<simple-rss>, [">= 0"])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Schwarz
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-08 00:00:00 +11:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
|
-
name:
|
35
|
+
name: rest-client
|
36
36
|
prerelease: false
|
37
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
@@ -105,17 +105,25 @@ extra_rdoc_files:
|
|
105
105
|
files:
|
106
106
|
- .document
|
107
107
|
- .gitignore
|
108
|
+
- CHANGES
|
108
109
|
- LICENSE
|
109
110
|
- README.md
|
110
111
|
- Rakefile
|
111
112
|
- VERSION
|
113
|
+
- lib/core_ext/autoload.rb
|
112
114
|
- lib/transcoder.rb
|
113
115
|
- lib/transcoder/parser/feed.rb
|
114
116
|
- lib/transcoder/parser/json.rb
|
115
117
|
- lib/transcoder/parser/xml.rb
|
118
|
+
- lib/transcoder/parser/yaml.rb
|
119
|
+
- spec/parser/feed_spec.rb
|
120
|
+
- spec/parser/json_spec.rb
|
121
|
+
- spec/parser/xml_spec.rb
|
122
|
+
- spec/parser/yaml_spec.rb
|
116
123
|
- spec/spec.opts
|
117
124
|
- spec/spec_helper.rb
|
118
125
|
- spec/transcoder_spec.rb
|
126
|
+
- transcoder.gemspec
|
119
127
|
has_rdoc: true
|
120
128
|
homepage: http://github.com/benschwarz/transcoder
|
121
129
|
licenses: []
|
@@ -147,5 +155,9 @@ signing_key:
|
|
147
155
|
specification_version: 3
|
148
156
|
summary: Transcode from common web based formats into plain old Ruby.
|
149
157
|
test_files:
|
158
|
+
- spec/parser/feed_spec.rb
|
159
|
+
- spec/parser/json_spec.rb
|
160
|
+
- spec/parser/xml_spec.rb
|
161
|
+
- spec/parser/yaml_spec.rb
|
150
162
|
- spec/spec_helper.rb
|
151
163
|
- spec/transcoder_spec.rb
|