sawyer 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sawyer.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Sawyer
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
 
4
4
  class Error < StandardError; end
5
5
  end
@@ -10,5 +10,6 @@ require 'set'
10
10
  resource
11
11
  relation
12
12
  response
13
+ serializer
13
14
  agent
14
15
  ).each { |f| require File.expand_path("../sawyer/#{f}", __FILE__) }
data/lib/sawyer/agent.rb CHANGED
@@ -3,15 +3,38 @@ require 'uri_template'
3
3
 
4
4
  module Sawyer
5
5
  class Agent
6
- NO_BODY = Set.new [:get, :head]
6
+ NO_BODY = Set.new([:get, :head])
7
+
8
+ class << self
9
+ attr_writer :serializer
10
+ end
11
+
12
+ def self.serializer
13
+ @serializer ||= Serializer.any_json
14
+ end
15
+
16
+ def self.encode(data)
17
+ serializer.encode(data)
18
+ end
19
+
20
+ def self.decode(data)
21
+ serializer.decode(data)
22
+ end
7
23
 
8
24
  # Agents handle making the requests, and passing responses to
9
25
  # Sawyer::Response.
10
26
  #
11
27
  # endpoint - String URI of the API entry point.
12
- def initialize(endpoint, conn = nil)
28
+ # options - Hash of options.
29
+ # :faraday - Optional Faraday::Connection to use.
30
+ # :serializer - Optional serializer Class. Defaults to
31
+ # self.serializer_class.
32
+ #
33
+ # Yields the Faraday::Connection if a block is given.
34
+ def initialize(endpoint, options = nil)
13
35
  @endpoint = endpoint
14
- @conn = conn || Faraday.new(endpoint)
36
+ @conn = (options && options[:faraday]) || Faraday.new(endpoint)
37
+ @serializer = (options && options[:serializer]) || self.class.serializer
15
38
  yield @conn if block_given?
16
39
  end
17
40
 
@@ -80,7 +103,7 @@ module Sawyer
80
103
  #
81
104
  # Returns a String.
82
105
  def encode_body(data)
83
- Yajl.dump data
106
+ @serializer.encode(data)
84
107
  end
85
108
 
86
109
  # Decodes a String response body to a resource.
@@ -89,7 +112,7 @@ module Sawyer
89
112
  #
90
113
  # Returns an Object resource (Hash by default).
91
114
  def decode_body(str)
92
- Yajl.load str, :symbolize_keys => true
115
+ @serializer.decode(str)
93
116
  end
94
117
 
95
118
  def expand_url(url, options = nil)
@@ -1,5 +1,3 @@
1
- require 'yajl'
2
-
3
1
  module Sawyer
4
2
  class Response
5
3
  attr_reader :agent,
@@ -0,0 +1,106 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ module Sawyer
5
+ class Serializer
6
+ def self.any_json
7
+ yajl || multi_json || json
8
+ end
9
+
10
+ def self.yajl
11
+ require 'yajl'
12
+ new(Yajl)
13
+ rescue LoadError
14
+ end
15
+
16
+ def self.json
17
+ require 'json'
18
+ new(JSON)
19
+ rescue LoadError
20
+ end
21
+
22
+ def self.multi_json
23
+ require 'multi_json'
24
+ new(MultiJson)
25
+ rescue LoadError
26
+ end
27
+
28
+ def self.message_pack
29
+ require 'msgpack'
30
+ new(MessagePack, :pack, :unpack)
31
+ rescue LoadError
32
+ end
33
+
34
+ # Public: Wraps a serialization format for Sawyer. Nested objects are
35
+ # prepared for serialization (such as changing Times to ISO 8601 Strings).
36
+ # Any serialization format that responds to #dump and #load will work.
37
+ def initialize(format, dump_method_name = nil, load_method_name = nil)
38
+ @format = format
39
+ @dump = @format.method(dump_method_name || :dump)
40
+ @load = @format.method(load_method_name || :load)
41
+ end
42
+
43
+ # Public: Encodes an Object (usually a Hash or Array of Hashes).
44
+ #
45
+ # data - Object to be encoded.
46
+ #
47
+ # Returns an encoded String.
48
+ def encode(data)
49
+ @dump.call(encode_object(data))
50
+ end
51
+
52
+ # Public: Decodes a String into an Object (usually a Hash or Array of
53
+ # Hashes).
54
+ #
55
+ # data - An encoded String.
56
+ #
57
+ # Returns a decoded Object.
58
+ def decode(data)
59
+ return nil if data.nil? || data.empty?
60
+ decode_object(@load.call(data))
61
+ end
62
+
63
+ def encode_object(data)
64
+ case data
65
+ when Hash then encode_hash(data)
66
+ when Array then data.map { |o| encode_object(data) }
67
+ else data
68
+ end
69
+ end
70
+
71
+ def encode_hash(hash)
72
+ hash.keys.each do |key|
73
+ case value = hash[key]
74
+ when Date then hash[key] = value.to_time.utc.xmlschema
75
+ when Time then hash[key] = value.utc.xmlschema
76
+ end
77
+ end
78
+ hash
79
+ end
80
+
81
+ def decode_object(data)
82
+ case data
83
+ when Hash then decode_hash(data)
84
+ when Array then data.map { |o| decode_object(data) }
85
+ else data
86
+ end
87
+ end
88
+
89
+ def decode_hash(hash)
90
+ hash.keys.each do |key|
91
+ hash[key.to_sym] = decode_hash_value(key, hash.delete(key))
92
+ end
93
+ hash
94
+ end
95
+
96
+ def decode_hash_value(key, value)
97
+ if key =~ /^_(at|on)$/
98
+ Time.parse(value)
99
+ elsif value.is_a?(Hash)
100
+ decode_hash(value)
101
+ else
102
+ value
103
+ end
104
+ end
105
+ end
106
+ end
data/sawyer.gemspec CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'sawyer'
16
- s.version = '0.0.3'
17
- s.date = '2012-09-26'
16
+ s.version = '0.0.4'
17
+ s.date = '2012-09-27'
18
18
  s.rubyforge_project = 'sawyer'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -37,7 +37,6 @@ Gem::Specification.new do |s|
37
37
  ## that are needed for an end user to actually USE your code.
38
38
  s.add_dependency('faraday', ['~> 0.8.4'])
39
39
  s.add_dependency('uri_template', ['~> 0.5.0'])
40
- s.add_dependency('yajl-ruby', ['~> 1.1.0'])
41
40
 
42
41
  ## List your development dependencies here. Development dependencies are
43
42
  ## those that are only needed during development
@@ -62,6 +61,7 @@ Gem::Specification.new do |s|
62
61
  lib/sawyer/relation.rb
63
62
  lib/sawyer/resource.rb
64
63
  lib/sawyer/response.rb
64
+ lib/sawyer/serializer.rb
65
65
  sawyer.gemspec
66
66
  test/agent_test.rb
67
67
  test/helper.rb
data/test/agent_test.rb CHANGED
@@ -14,7 +14,7 @@ module Sawyer
14
14
  @stubs.get '/a/' do |env|
15
15
  assert_equal 'foo.com', env[:url].host
16
16
 
17
- [200, {}, Yajl.dump(
17
+ [200, {}, Sawyer::Agent.encode(
18
18
  :_links => {
19
19
  :users => {:href => '/users'}})]
20
20
  end
@@ -38,7 +38,7 @@ module Sawyer
38
38
  @stubs.get '/a/' do |env|
39
39
  assert_equal 'foo.com', env[:url].host
40
40
 
41
- [200, {}, Yajl.dump(
41
+ [200, {}, Sawyer::Agent.encode(
42
42
  :_links => {
43
43
  :users => {:href => '/users'}})]
44
44
  end
@@ -9,7 +9,7 @@ module Sawyer
9
9
  conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
10
10
  conn.adapter :test, @stubs do |stub|
11
11
  stub.get '/' do
12
- [200, {'Content-Type' => 'application/json'}, Yajl.dump(
12
+ [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
13
13
  :a => 1,
14
14
  :_links => {
15
15
  :self => {:href => '/a', :method => 'POST'}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sawyer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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-26 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: 0.5.0
46
- - !ruby/object:Gem::Dependency
47
- name: yajl-ruby
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 1.1.0
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 1.1.0
62
46
  description: Secret User Agent of HTTP built on Faraday
63
47
  email: technoweenie@gmail.com
64
48
  executables: []
@@ -79,6 +63,7 @@ files:
79
63
  - lib/sawyer/relation.rb
80
64
  - lib/sawyer/resource.rb
81
65
  - lib/sawyer/response.rb
66
+ - lib/sawyer/serializer.rb
82
67
  - sawyer.gemspec
83
68
  - test/agent_test.rb
84
69
  - test/helper.rb
@@ -97,6 +82,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
82
  - - ! '>='
98
83
  - !ruby/object:Gem::Version
99
84
  version: '0'
85
+ segments:
86
+ - 0
87
+ hash: -3466190819871303014
100
88
  required_rubygems_version: !ruby/object:Gem::Requirement
101
89
  none: false
102
90
  requirements: