Skip to content

Commit

Permalink
remove yajl as a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Sep 28, 2012
1 parent e03f615 commit b5cc59c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
13 changes: 9 additions & 4 deletions lib/sawyer/agent.rb
Expand Up @@ -10,10 +10,15 @@ class << self
end

def self.serializer
@serializer ||= begin
require 'yajl'
Serializer.new(Yajl)
end
@serializer ||= Serializer.any_json
end

def self.encode(data)
serializer.encode(data)
end

def self.decode(data)
serializer.decode(data)
end

# Agents handle making the requests, and passing responses to
Expand Down
29 changes: 29 additions & 0 deletions lib/sawyer/serializer.rb
Expand Up @@ -3,6 +3,34 @@

module Sawyer
class Serializer
def self.any_json
yajl || multi_json || json
end

def self.yajl
require 'yajl'
new(Yajl)
rescue LoadError
end

def self.json
require 'json'
new(JSON)
rescue LoadError
end

def self.multi_json
require 'multi_json'
new(MultiJson)
rescue LoadError
end

def self.message_pack
require 'msgpack'
new(MessagePack, :pack, :unpack)
rescue LoadError
end

# Public: Wraps a serialization format for Sawyer. Nested objects are
# prepared for serialization (such as changing Times to ISO 8601 Strings).
# Any serialization format that responds to #dump and #load will work.
Expand All @@ -28,6 +56,7 @@ def encode(data)
#
# Returns a decoded Object.
def decode(data)
return nil if data.nil? || data.empty?
decode_object(@load.call(data))
end

Expand Down
1 change: 0 additions & 1 deletion sawyer.gemspec
Expand Up @@ -37,7 +37,6 @@ Gem::Specification.new do |s|
## that are needed for an end user to actually USE your code.
s.add_dependency('faraday', ['~> 0.8.4'])
s.add_dependency('uri_template', ['~> 0.5.0'])
s.add_dependency('yajl-ruby', ['~> 1.1.0'])

## List your development dependencies here. Development dependencies are
## those that are only needed during development
Expand Down
4 changes: 2 additions & 2 deletions test/agent_test.rb
Expand Up @@ -14,7 +14,7 @@ def test_accesses_root_relations
@stubs.get '/a/' do |env|
assert_equal 'foo.com', env[:url].host

[200, {}, Yajl.dump(
[200, {}, Sawyer::Agent.encode(
:_links => {
:users => {:href => '/users'}})]
end
Expand All @@ -38,7 +38,7 @@ def test_starts_a_session
@stubs.get '/a/' do |env|
assert_equal 'foo.com', env[:url].host

[200, {}, Yajl.dump(
[200, {}, Sawyer::Agent.encode(
:_links => {
:users => {:href => '/users'}})]
end
Expand Down
2 changes: 1 addition & 1 deletion test/response_test.rb
Expand Up @@ -9,7 +9,7 @@ def setup
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test, @stubs do |stub|
stub.get '/' do
[200, {'Content-Type' => 'application/json'}, Yajl.dump(
[200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
:a => 1,
:_links => {
:self => {:href => '/a', :method => 'POST'}
Expand Down

0 comments on commit b5cc59c

Please sign in to comment.