medie 1.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies to develop your gem here.
4
+ # Include everything needed to run rake, tests, features, etc.
5
+ group :development do
6
+ gem "rspec", "~> 2.3.0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.5.2"
9
+ gem "rcov", ">= 0"
10
+ end
11
+
12
+ gem "methodize"
13
+ gem "json_pure"
14
+ gem "activesupport"
15
+ gem "actionpack", ">= 3.0.0"
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Guilherme Silveira
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ = medie
2
+
3
+ Medie is a simple repository for media type handlers.
4
+
5
+ Register your own handler and use it as in:
6
+
7
+ <pre>
8
+ registry = Medie::Registry.new.use(MyHandler.new)
9
+
10
+ registry.for("anything").marshal some_object
11
+ registry.for("anything").unmarshal "some_content"
12
+ </pre>
13
+
14
+ Although created to be used with Restfulie, it can be used anywhere else.
15
+
16
+ == Contributing to medie
17
+
18
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
19
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
20
+ * Fork the project
21
+ * Start a feature/bugfix branch
22
+ * Commit and push until you are happy with your contribution
23
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
24
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
25
+
26
+ == Copyright
27
+
28
+ Copyright (c) 2010 Guilherme Silveira. See LICENSE.txt for
29
+ further details.
30
+
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "medie"
15
+ gem.homepage = "http://github.com/caelum/medie"
16
+ gem.license = "MIT"
17
+ gem.summary = "A gem that allows you register media types and unmarshal/marshal data accordingly"
18
+ gem.description = "A gem that allows you register media types and unmarshal/marshal data accordingly"
19
+ gem.email = "guilherme.silveira@caelum.com.br"
20
+ gem.authors = ["Guilherme Silveira"]
21
+ gem.version = "1.0.0.beta3"
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rspec/core'
26
+ require 'rspec/core/rake_task'
27
+ RSpec::Core::RakeTask.new(:spec) do |spec|
28
+ spec.pattern = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+ version = "1.0.0.beta3"
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "medie #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
@@ -0,0 +1,24 @@
1
+ # Medie supports by default a series of media formats, such as
2
+ # xml, json, atom, opensearch and form-urlencoded.
3
+ #
4
+ # Use register to register your own media format, but
5
+ # do not forget to contribute with your converter if
6
+ # it is a well known media type that Medie still
7
+ # does not support.
8
+ module Medie
9
+
10
+ require 'medie/registry'
11
+ def self.registry
12
+ @registry ||= Registry.new
13
+ end
14
+
15
+ require 'medie/link'
16
+ require 'medie/linked'
17
+
18
+ require 'medie/generic'
19
+ require 'medie/form_url_encoded'
20
+ require 'medie/open_search'
21
+ require 'medie/json'
22
+ require 'medie/xml'
23
+
24
+ end
@@ -0,0 +1 @@
1
+ require 'medie/form_url_encoded/driver'
@@ -0,0 +1,26 @@
1
+ module Medie
2
+ module FormUrlEncoded
3
+ class Driver
4
+ def marshal(content, rel)
5
+ if content.kind_of? Hash
6
+ content.map { |key, value| "#{key}=#{value}" }.join("&")
7
+ else
8
+ content
9
+ end
10
+ end
11
+
12
+ def unmarshal(content)
13
+ def content.links
14
+ []
15
+ end
16
+ content
17
+ end
18
+
19
+ def can_handle?(content_type)
20
+ !(content_type.nil? || content_type.split(";")[0]!="application/x-www-form-urlencoded")
21
+ end
22
+
23
+ Medie.registry << Driver.new
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module Medie
2
+
3
+ # This is a simple implementation of a media type that provides no links
4
+ # and no marshalling, except the content itself. There is no parsing.
5
+ class Generic
6
+
7
+ # Because there is no media type registered
8
+ # return the content itself
9
+ def unmarshal(content)
10
+ def content.links
11
+ []
12
+ end
13
+ content
14
+ end
15
+
16
+ # Because there is no media type registered, return the string itself.
17
+ def marshal(string, rel)
18
+ string
19
+ end
20
+
21
+ def can_handle?(media_type)
22
+ true
23
+ end
24
+
25
+ end
26
+
27
+ Medie.registry << Generic.new
28
+
29
+ end
@@ -0,0 +1,5 @@
1
+ require 'methodize'
2
+ require 'medie/linked'
3
+ require 'medie/json/links'
4
+ require 'medie/json/driver'
5
+ require 'json'
@@ -0,0 +1,30 @@
1
+ module Medie
2
+ module Json
3
+ # Receives Json representations and turn them into hashes. Turn
4
+ # hashes into json objects.
5
+ class Driver
6
+ def marshal(obj, rel)
7
+ if obj.kind_of? String
8
+ obj
9
+ else
10
+ obj.to_json
11
+ end
12
+ end
13
+
14
+ def unmarshal(content)
15
+ if content.nil?
16
+ return {}.extend(Methodize).extend(Linked).use_link_type(Medie::Json::Links)
17
+ end
18
+
19
+ ::JSON.parse(content).extend(Methodize).extend(Linked).use_link_type(Medie::Json::Links)
20
+ end
21
+
22
+ def can_handle?(content_type)
23
+ !(content_type.nil? || content_type.split(";")[0]!="application/json")
24
+ end
25
+
26
+ end
27
+
28
+ Medie.registry << Driver.new
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ module Medie
2
+ module Json
3
+
4
+ # represents a set of links using json
5
+ class Links
6
+ def initialize(parent_node)
7
+ @node = parent_node
8
+ end
9
+
10
+ def refresh
11
+ links.self.follow.get
12
+ end
13
+
14
+ def method_missing(symbol, *args, &block)
15
+ linkset = @node.select {|link| link.rel == symbol.to_s }
16
+ linkset.map! { |link| Medie::Link.new(link) }
17
+ unless linkset.empty?
18
+ linkset.size == 1 ? linkset.first : linkset
19
+ else
20
+ nil
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ module Medie
2
+
3
+ # a link object
4
+ class Link
5
+ def initialize(obj)
6
+ @obj = obj
7
+ end
8
+
9
+ def type
10
+ content_type
11
+ end
12
+
13
+ def href
14
+ @obj["href"]
15
+ end
16
+
17
+ def rel
18
+ @obj["rel"]
19
+ end
20
+ def content_type
21
+ @obj["type"]
22
+ end
23
+
24
+ def method_missing(symbol, *args, &block)
25
+ @obj.send(symbol, *args, &block)
26
+ end
27
+
28
+ def to_s
29
+ "<link to #{@options}>"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ module Medie
2
+ module Linked
3
+ def links
4
+ links = fetch("link", [])
5
+ links = [links] unless links.kind_of? Array
6
+ @type_to_use.new(links)
7
+ end
8
+
9
+ def __normalize__(value)
10
+ case value
11
+ when Hash
12
+ value.extend(Methodize).extend(Linked).use_link_type(@type_to_use)
13
+ when Array
14
+ value.map { |v| __normalize__(v) }
15
+ else
16
+ value
17
+ end
18
+ value
19
+ end
20
+
21
+ def use_link_type(type)
22
+ @type_to_use = type
23
+ self
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'medie/open_search/descriptor'
2
+ require 'medie/open_search/driver'
@@ -0,0 +1,16 @@
1
+ module Medie
2
+ module OpenSearch
3
+ class Descriptor
4
+
5
+ def initialize(hash)
6
+ @hash = hash["OpenSearchDescription"]
7
+ end
8
+
9
+ def urls
10
+ uris = @hash["Url"]
11
+ uris.kind_of?(Array) ? uris : [uris]
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module Medie
2
+ module OpenSearch
3
+ class Driver
4
+
5
+ def unmarshal(xml)
6
+ hash = Medie::Xml::Driver.new.unmarshal(xml)
7
+ descriptor = Descriptor.new(hash)
8
+ end
9
+
10
+ def can_handle?(content_type)
11
+ !(content_type.nil? || content_type.split(";")[0]!="application/opensearchdescription+xml")
12
+ end
13
+
14
+ Medie.registry << Driver.new
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,29 @@
1
+ module Medie
2
+ class Registry
3
+
4
+ # adds a new handler to this registry
5
+ def <<(what)
6
+ use(what)
7
+ end
8
+
9
+ # adds a new handler to this registry
10
+ def use(what)
11
+ registry << what
12
+ self
13
+ end
14
+
15
+ # Returns a handler that can handle this kind of media type.
16
+ # It will always use the last registered handler.
17
+ def for(media_type)
18
+ registry.reverse.find do |handler|
19
+ handler.can_handle?(media_type)
20
+ end
21
+ end
22
+
23
+ private
24
+ def registry
25
+ @registry ||= []
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_support/core_ext/hash/conversions'
2
+ require 'medie/linked'
3
+ require 'medie/xml/links'
4
+ require 'medie/xml/driver'
@@ -0,0 +1,37 @@
1
+ module Medie
2
+ module Xml
3
+ class Driver
4
+ def marshal(obj, options = {})
5
+ if(obj.kind_of?(String))
6
+ obj
7
+ elsif obj.kind_of?(Hash) && obj.size==1
8
+ root = obj.values.first
9
+ if !root.respond_to?(:to_xml)
10
+ raise "Trying to marshal a string into xml does not make sense: '#{obj}'"
11
+ end
12
+ root.to_xml(:root => obj.keys.first)
13
+ else
14
+ obj.to_xml
15
+ end
16
+ end
17
+
18
+ def unmarshal(content)
19
+ if content
20
+ hash = Hash.from_xml(content)
21
+ else
22
+ hash = {}
23
+ end
24
+ hash.extend(Methodize).extend(Medie::Linked).use_link_type(Medie::Xml::Links)
25
+ end
26
+
27
+ def can_handle?(content_type)
28
+ !(content_type.nil? ||
29
+ (content_type.split(";")[0]!="application/xml" &&
30
+ content_type.split(";")[0]!="application/atom+xml" &&
31
+ content_type.split(";")[0]!="text/xml"))
32
+ end
33
+
34
+ Medie.registry << Driver.new
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ module Medie
2
+ module Xml
3
+ class Links
4
+
5
+ def initialize(links)
6
+ @hash = {}
7
+ links = [links] unless links.kind_of? Array
8
+ links = [] unless links
9
+ links.each { |l|
10
+ link = Medie::Link.new(l)
11
+ @hash[link.rel.to_s] = link
12
+ }
13
+ end
14
+
15
+ def [](name)
16
+ @hash[name]
17
+ end
18
+
19
+ def size
20
+ @hash.size
21
+ end
22
+
23
+ def keys
24
+ @hash.keys
25
+ end
26
+
27
+ def method_missing(sym, *args)
28
+ raise "Links can not receive arguments" unless args.empty?
29
+ self[sym.to_s]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,101 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{medie}
8
+ s.version = "1.0.0.beta3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Guilherme Silveira"]
12
+ s.date = %q{2011-01-04}
13
+ s.description = %q{A gem that allows you register media types and unmarshal/marshal data accordingly}
14
+ s.email = %q{guilherme.silveira@caelum.com.br}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "lib/medie.rb",
27
+ "lib/medie/form_url_encoded.rb",
28
+ "lib/medie/form_url_encoded/driver.rb",
29
+ "lib/medie/generic.rb",
30
+ "lib/medie/json.rb",
31
+ "lib/medie/json/driver.rb",
32
+ "lib/medie/json/links.rb",
33
+ "lib/medie/link.rb",
34
+ "lib/medie/linked.rb",
35
+ "lib/medie/open_search.rb",
36
+ "lib/medie/open_search/descriptor.rb",
37
+ "lib/medie/open_search/driver.rb",
38
+ "lib/medie/registry.rb",
39
+ "lib/medie/xml.rb",
40
+ "lib/medie/xml/driver.rb",
41
+ "lib/medie/xml/links.rb",
42
+ "medie.gemspec",
43
+ "spec/integration/full_json.js",
44
+ "spec/integration/json_spec.rb",
45
+ "spec/medie/json/driver_spec.rb",
46
+ "spec/medie/linked/form_url_encoded/driver_spec.rb",
47
+ "spec/medie/linked/open_search/descriptor_spec.rb",
48
+ "spec/medie/registry_spec.rb",
49
+ "spec/medie/xml/driver_spec.rb",
50
+ "spec/spec_helper.rb"
51
+ ]
52
+ s.homepage = %q{http://github.com/caelum/medie}
53
+ s.licenses = ["MIT"]
54
+ s.require_paths = ["lib"]
55
+ s.rubygems_version = %q{1.3.7}
56
+ s.summary = %q{A gem that allows you register media types and unmarshal/marshal data accordingly}
57
+ s.test_files = [
58
+ "spec/integration/json_spec.rb",
59
+ "spec/medie/json/driver_spec.rb",
60
+ "spec/medie/linked/form_url_encoded/driver_spec.rb",
61
+ "spec/medie/linked/open_search/descriptor_spec.rb",
62
+ "spec/medie/registry_spec.rb",
63
+ "spec/medie/xml/driver_spec.rb",
64
+ "spec/spec_helper.rb"
65
+ ]
66
+
67
+ if s.respond_to? :specification_version then
68
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
69
+ s.specification_version = 3
70
+
71
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
72
+ s.add_runtime_dependency(%q<methodize>, [">= 0"])
73
+ s.add_runtime_dependency(%q<json_pure>, [">= 0"])
74
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
75
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
76
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
77
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
78
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
79
+ s.add_development_dependency(%q<rcov>, [">= 0"])
80
+ else
81
+ s.add_dependency(%q<methodize>, [">= 0"])
82
+ s.add_dependency(%q<json_pure>, [">= 0"])
83
+ s.add_dependency(%q<activesupport>, [">= 0"])
84
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
85
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
86
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
87
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
88
+ s.add_dependency(%q<rcov>, [">= 0"])
89
+ end
90
+ else
91
+ s.add_dependency(%q<methodize>, [">= 0"])
92
+ s.add_dependency(%q<json_pure>, [">= 0"])
93
+ s.add_dependency(%q<activesupport>, [">= 0"])
94
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
95
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
96
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
97
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
98
+ s.add_dependency(%q<rcov>, [">= 0"])
99
+ end
100
+ end
101
+
@@ -0,0 +1,46 @@
1
+ {
2
+ "articles": {
3
+ "link": [
4
+ {
5
+ "rel": "self",
6
+ "href": "http://another.place.com",
7
+ "type": "text/json",
8
+ "charset": "utf-8"
9
+ },
10
+ {
11
+ "rel": "search",
12
+ "href": "http://search.place.com",
13
+ "type": "application/opensearchdescription+xml",
14
+ "charset": "utf-8"
15
+ }
16
+ ],
17
+ "size": 2,
18
+ "freezed": null,
19
+ "article": [
20
+ {
21
+ "link": {
22
+ "rel": "self",
23
+ "href": "http://a.link.com",
24
+ "type": "text/json",
25
+ "profile": "article",
26
+ "charset": "utf-8"
27
+ },
28
+ "title": "a awesome title",
29
+ "subtitle": "Article subtitle",
30
+ "author": "John Maclane"
31
+ },
32
+ {
33
+ "link": {
34
+ "rel": "self",
35
+ "href": "http://a.link.com",
36
+ "type": "text/json",
37
+ "profile": "article",
38
+ "charset": "utf-8"
39
+ },
40
+ "title": "a awesome title",
41
+ "subtitle": "Article subtitle",
42
+ "author": "John Maclane"
43
+ }
44
+ ]
45
+ }
46
+ }
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Medie::Json do
4
+
5
+ before :all do
6
+ full_json = IO.read(File.dirname(__FILE__) + '/full_json.js')
7
+ @json = Medie::Json::Driver.new.unmarshal(full_json)
8
+ end
9
+
10
+ describe "JSON read" do
11
+
12
+ it "should be able to read a JSON object in many ways" do
13
+
14
+ @json["articles"]["link"].first["type"].should == "text/json"
15
+ @json.articles.link.first.type.should == "text/json"
16
+
17
+ @json.articles.links.search.href.should == "http://search.place.com"
18
+ @json.articles.links.unknow_rel.should == nil
19
+ end
20
+
21
+ end
22
+
23
+ describe "JSON write" do
24
+
25
+ it "should be able to write a JSON object in many ways" do
26
+ @json["articles"]["size"] = 10
27
+ @json["articles"]["size"].should == 10
28
+
29
+ @json.articles.link << {"href" => "http://dont.panic.com", "rel" => "towel"}
30
+ @json.articles.link.last.href.should == "http://dont.panic.com"
31
+ @json.articles.link.last.rel.should == "towel"
32
+ @json.articles.link.size.should == 3
33
+
34
+ @json.articles.link.last.type = "application/json"
35
+ @json.articles.link.last.type.should == "application/json"
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Medie::Json::Driver do
4
+
5
+ context "when looking up the handler" do
6
+
7
+ it "should accept pure application/json" do
8
+ Medie::Json::Driver.new.can_handle?("application/json").should be_true
9
+ end
10
+
11
+ it "should not accept anything else" do
12
+ Medie::Json::Driver.new.can_handle?("application/json2").should be_false
13
+ end
14
+
15
+ it "should accept profiles and options" do
16
+ Medie::Json::Driver.new.can_handle?("application/json;profile=client").should be_true
17
+ end
18
+
19
+ end
20
+
21
+ context "when unmarshalling" do
22
+
23
+ it "should return an empty hash if its empty" do
24
+ result = Medie::Json::Driver.new.unmarshal(nil)
25
+ result.should be_empty
26
+ result.should be_kind_of(Hash)
27
+ result.should be_kind_of(Methodize)
28
+ result.should be_kind_of(Medie::Linked)
29
+ result.links.should be_kind_of(Medie::Json::Links)
30
+ end
31
+
32
+ it "should return the unmarshalled json hash enhanced" do
33
+ result = Medie::Json::Driver.new.unmarshal({"name" => "guilherme"}.to_json)
34
+ result.should == {"name" => "guilherme"}
35
+ result.should be_kind_of(Hash)
36
+ result.should be_kind_of(Methodize)
37
+ result.should be_kind_of(Medie::Linked)
38
+ result.links.should be_kind_of(Medie::Json::Links)
39
+ end
40
+
41
+ end
42
+
43
+ context "when marshalling" do
44
+
45
+ it "should return itself if its a string" do
46
+ result = Medie::Json::Driver.new.marshal("guilherme", nil)
47
+ result.should == "guilherme"
48
+ end
49
+
50
+ it "should return the unmarshalled json hash enhanced" do
51
+ result = Medie::Json::Driver.new.marshal({"name" => "guilherme"}, nil)
52
+ result.should == {"name" => "guilherme"}.to_json
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
+
3
+ describe Medie::FormUrlEncoded::Driver do
4
+ context "when unmarshalling" do
5
+
6
+ it "when passing not a hash, returns itself" do
7
+ Medie::FormUrlEncoded::Driver.new.marshal("some content", "").should == "some content"
8
+ end
9
+
10
+ it "when passing a hash should concatenate" do
11
+ params = {:user => "guilherme", :password => "my_pass"}
12
+ marshalled_params = Medie::FormUrlEncoded::Driver.new.marshal(params, "")
13
+ marshalled_params.should =~ /password=my_pass/
14
+ marshalled_params.should =~ /user=guilherme/
15
+ marshalled_params.should =~ /&/
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
+
3
+ describe Medie::OpenSearch::Descriptor do
4
+
5
+ context "unmarshalling one url documents" do
6
+
7
+ before do
8
+ xml = '<?xml version="1.0" encoding="UTF-8"?>
9
+ <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
10
+ <ShortName>Restbuy</ShortName>
11
+ <Description>Restbuy search engine.</Description>
12
+ <Tags>restbuy</Tags>
13
+ <Contact>admin@restbuy.com</Contact>
14
+ <Url type="application/atom+xml" template="http://localhost:3000/products?q={searchTerms}&amp;pw={startPage?}&amp;format=atom" />
15
+ <Url type="application/json" template="http://localhost:3000/products?q={searchTerms}&amp;pw={startPage?}&amp;format=json" />
16
+ </OpenSearchDescription>'
17
+ @descriptor = Medie::OpenSearch::Driver.new.unmarshal(xml)
18
+ end
19
+
20
+ it "should unmarshall opensearch xml descriptions" do
21
+ @descriptor.urls.size.should == 2
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class Always
4
+ def can_handle?(response)
5
+ true
6
+ end
7
+ end
8
+ class Never
9
+ def can_handle?(response)
10
+ false
11
+ end
12
+ end
13
+
14
+ describe Medie do
15
+
16
+ it "should return acceptable registries" do
17
+
18
+ handler = Always.new
19
+ registry = Medie::Registry.new.use(handler)
20
+ registry.for("anything").should == handler
21
+
22
+ end
23
+
24
+ it "should return nil if there is no handler available" do
25
+
26
+ registry = Medie::Registry.new.use(Never.new)
27
+ registry.for("anything").should be_nil
28
+
29
+ end
30
+
31
+
32
+ it "should always use the most recent registered handler" do
33
+
34
+ handler = Always.new
35
+ registry = Medie::Registry.new
36
+ registry.use(Always.new)
37
+ registry.use(handler)
38
+ registry.for("anything").should == handler
39
+
40
+ end
41
+
42
+
43
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Medie::Xml::Driver do
4
+
5
+ context "when looking up the handler" do
6
+
7
+ it "should accept pure application/xml" do
8
+ Medie::Xml::Driver.new.can_handle?("application/xml").should be_true
9
+ Medie::Xml::Driver.new.can_handle?("application/atom+xml").should be_true
10
+ Medie::Xml::Driver.new.can_handle?("text/xml").should be_true
11
+ end
12
+
13
+ it "should not accept anything else" do
14
+ Medie::Xml::Driver.new.can_handle?("application/xml2").should be_false
15
+ end
16
+
17
+ it "should accept profiles and options" do
18
+ Medie::Xml::Driver.new.can_handle?("application/xml;profile=client").should be_true
19
+ Medie::Xml::Driver.new.can_handle?("application/atom+xml;profile=client").should be_true
20
+ Medie::Xml::Driver.new.can_handle?("text/xml;profile=client").should be_true
21
+ end
22
+
23
+ end
24
+
25
+ context "when unmarshalling" do
26
+
27
+ it "should return an empty hash if its empty" do
28
+ result = Medie::Xml::Driver.new.unmarshal(nil)
29
+ result.should be_empty
30
+ result.should be_kind_of(Hash)
31
+ result.should be_kind_of(Methodize)
32
+ result.should be_kind_of(Medie::Linked)
33
+ result.links.should be_kind_of(Medie::Xml::Links)
34
+ end
35
+
36
+ it "should return the unmarshalled Xml hash enhanced" do
37
+ result = Medie::Xml::Driver.new.unmarshal("<name>guilherme</name>")
38
+ result.should == {"name" => "guilherme"}
39
+ result.should be_kind_of(Hash)
40
+ result.should be_kind_of(Methodize)
41
+ result.should be_kind_of(Medie::Linked)
42
+ result.links.should be_kind_of(Medie::Xml::Links)
43
+ end
44
+
45
+ end
46
+
47
+ context "when marshalling" do
48
+
49
+ it "should return itself if its a string" do
50
+ result = Medie::Xml::Driver.new.marshal("guilherme", nil)
51
+ result.should == "guilherme"
52
+ end
53
+
54
+ it "should return the unmarshalled Xml hash enhanced" do
55
+ lambda { Medie::Xml::Driver.new.marshal({"name" => "guilherme"}, nil) }.
56
+ should raise_error("Trying to marshal a string into xml does not make sense: 'nameguilherme'")
57
+ end
58
+
59
+ it "should return the unmarshalled Xml hash enhanced" do
60
+ hash = {"name" => {"first" => "guilherme"}}
61
+ result = Medie::Xml::Driver.new.marshal(hash, nil)
62
+ result.should == hash["name"].to_xml(:root => "name")
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'medie'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,226 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: medie
3
+ version: !ruby/object:Gem::Version
4
+ hash: -1848230053
5
+ prerelease: true
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ - beta3
11
+ version: 1.0.0.beta3
12
+ platform: ruby
13
+ authors:
14
+ - Guilherme Silveira
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-01-04 00:00:00 -02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ type: :runtime
25
+ name: methodize
26
+ version_requirements: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
34
+ version: "0"
35
+ requirement: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ prerelease: false
38
+ type: :runtime
39
+ name: json_pure
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ requirement: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ prerelease: false
52
+ type: :runtime
53
+ name: activesupport
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirement: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ prerelease: false
66
+ type: :runtime
67
+ name: actionpack
68
+ version_requirements: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 7
74
+ segments:
75
+ - 3
76
+ - 0
77
+ - 0
78
+ version: 3.0.0
79
+ requirement: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ prerelease: false
82
+ type: :development
83
+ name: rspec
84
+ version_requirements: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 2
92
+ - 3
93
+ - 0
94
+ version: 2.3.0
95
+ requirement: *id005
96
+ - !ruby/object:Gem::Dependency
97
+ prerelease: false
98
+ type: :development
99
+ name: bundler
100
+ version_requirements: &id006 !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ~>
104
+ - !ruby/object:Gem::Version
105
+ hash: 23
106
+ segments:
107
+ - 1
108
+ - 0
109
+ - 0
110
+ version: 1.0.0
111
+ requirement: *id006
112
+ - !ruby/object:Gem::Dependency
113
+ prerelease: false
114
+ type: :development
115
+ name: jeweler
116
+ version_requirements: &id007 !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ~>
120
+ - !ruby/object:Gem::Version
121
+ hash: 7
122
+ segments:
123
+ - 1
124
+ - 5
125
+ - 2
126
+ version: 1.5.2
127
+ requirement: *id007
128
+ - !ruby/object:Gem::Dependency
129
+ prerelease: false
130
+ type: :development
131
+ name: rcov
132
+ version_requirements: &id008 !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ hash: 3
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ requirement: *id008
142
+ description: A gem that allows you register media types and unmarshal/marshal data accordingly
143
+ email: guilherme.silveira@caelum.com.br
144
+ executables: []
145
+
146
+ extensions: []
147
+
148
+ extra_rdoc_files:
149
+ - LICENSE.txt
150
+ - README.rdoc
151
+ files:
152
+ - .document
153
+ - .rspec
154
+ - Gemfile
155
+ - LICENSE.txt
156
+ - README.rdoc
157
+ - Rakefile
158
+ - lib/medie.rb
159
+ - lib/medie/form_url_encoded.rb
160
+ - lib/medie/form_url_encoded/driver.rb
161
+ - lib/medie/generic.rb
162
+ - lib/medie/json.rb
163
+ - lib/medie/json/driver.rb
164
+ - lib/medie/json/links.rb
165
+ - lib/medie/link.rb
166
+ - lib/medie/linked.rb
167
+ - lib/medie/open_search.rb
168
+ - lib/medie/open_search/descriptor.rb
169
+ - lib/medie/open_search/driver.rb
170
+ - lib/medie/registry.rb
171
+ - lib/medie/xml.rb
172
+ - lib/medie/xml/driver.rb
173
+ - lib/medie/xml/links.rb
174
+ - medie.gemspec
175
+ - spec/integration/full_json.js
176
+ - spec/integration/json_spec.rb
177
+ - spec/medie/json/driver_spec.rb
178
+ - spec/medie/linked/form_url_encoded/driver_spec.rb
179
+ - spec/medie/linked/open_search/descriptor_spec.rb
180
+ - spec/medie/registry_spec.rb
181
+ - spec/medie/xml/driver_spec.rb
182
+ - spec/spec_helper.rb
183
+ has_rdoc: true
184
+ homepage: http://github.com/caelum/medie
185
+ licenses:
186
+ - MIT
187
+ post_install_message:
188
+ rdoc_options: []
189
+
190
+ require_paths:
191
+ - lib
192
+ required_ruby_version: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ hash: 3
198
+ segments:
199
+ - 0
200
+ version: "0"
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ">"
205
+ - !ruby/object:Gem::Version
206
+ hash: 25
207
+ segments:
208
+ - 1
209
+ - 3
210
+ - 1
211
+ version: 1.3.1
212
+ requirements: []
213
+
214
+ rubyforge_project:
215
+ rubygems_version: 1.3.7
216
+ signing_key:
217
+ specification_version: 3
218
+ summary: A gem that allows you register media types and unmarshal/marshal data accordingly
219
+ test_files:
220
+ - spec/integration/json_spec.rb
221
+ - spec/medie/json/driver_spec.rb
222
+ - spec/medie/linked/form_url_encoded/driver_spec.rb
223
+ - spec/medie/linked/open_search/descriptor_spec.rb
224
+ - spec/medie/registry_spec.rb
225
+ - spec/medie/xml/driver_spec.rb
226
+ - spec/spec_helper.rb