faraday_yaml 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format d
2
+ --colour
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.1.0 May 2, 2011
4
+
5
+ * Support Faraday 0.6.x
6
+ * Full request/response specs
7
+
3
8
  ### 0.0.2 February 9, 2011
4
9
 
5
10
  * Only set the request's Content-Type when actually needed. (Some APIs break when you send a Content-Type header and an empty body on GET requests.)
data/Gemfile.lock CHANGED
@@ -1,32 +1,32 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- faraday_yaml (0.0.2)
5
- faraday (~> 0.5.5)
4
+ faraday_yaml (0.1.0)
5
+ faraday (~> 0.6.1)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- addressable (2.2.4)
10
+ addressable (2.2.5)
11
11
  diff-lcs (1.1.2)
12
- faraday (0.5.5)
12
+ faraday (0.6.1)
13
13
  addressable (~> 2.2.4)
14
14
  multipart-post (~> 1.1.0)
15
- rack (>= 1.1.0, < 2)
15
+ rack (< 2, >= 1.1.0)
16
16
  multipart-post (1.1.0)
17
- rack (1.2.1)
18
- rspec (2.4.0)
19
- rspec-core (~> 2.4.0)
20
- rspec-expectations (~> 2.4.0)
21
- rspec-mocks (~> 2.4.0)
22
- rspec-core (2.4.0)
23
- rspec-expectations (2.4.0)
17
+ rack (1.2.2)
18
+ rspec (2.5.0)
19
+ rspec-core (~> 2.5.0)
20
+ rspec-expectations (~> 2.5.0)
21
+ rspec-mocks (~> 2.5.0)
22
+ rspec-core (2.5.1)
23
+ rspec-expectations (2.5.0)
24
24
  diff-lcs (~> 1.1.2)
25
- rspec-mocks (2.4.0)
25
+ rspec-mocks (2.5.0)
26
26
 
27
27
  PLATFORMS
28
28
  ruby
29
29
 
30
30
  DEPENDENCIES
31
31
  faraday_yaml!
32
- rspec (~> 2.4.0)
32
+ rspec (~> 2.5.0)
data/faraday_yaml.gemspec CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
- s.add_development_dependency "rspec", "~> 2.4.0"
22
- s.add_runtime_dependency "faraday", "~> 0.5.5"
21
+ s.add_development_dependency "rspec", "~> 2.5.0"
22
+ s.add_runtime_dependency "faraday", "~> 0.6.1"
23
23
  end
@@ -2,10 +2,10 @@ require 'faraday'
2
2
 
3
3
  module Faraday
4
4
  class Request::YAML < Faraday::Middleware
5
- begin
6
- require 'yaml'
7
- rescue LoadError, NameError => e
8
- self.load_error = e
5
+ dependency 'yaml'
6
+
7
+ def initialize(app)
8
+ @app = app
9
9
  end
10
10
 
11
11
  def call(env)
@@ -0,0 +1,18 @@
1
+ require 'faraday'
2
+
3
+ module Faraday
4
+ class Response::YAML < Response::Middleware
5
+ dependency 'yaml'
6
+
7
+ def parse(body)
8
+ case body
9
+ when ''
10
+ nil
11
+ else
12
+ YAML.load(body)
13
+ end
14
+ rescue Object => err
15
+ raise Faraday::Error::ParsingError.new(err)
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module FaradayYaml
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/faraday_yaml.rb CHANGED
@@ -1,2 +1,9 @@
1
- require 'faraday_yaml/request/yaml.rb'
2
- require 'faraday_yaml/response/yaml.rb'
1
+ require 'faraday'
2
+
3
+ class Faraday::Request
4
+ autoload :YAML, 'faraday/request/yaml.rb'
5
+ end
6
+
7
+ class Faraday::Response
8
+ autoload :YAML, 'faraday/response/yaml.rb'
9
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Faraday::Request::YAML do
4
+ let(:test_hash) do
5
+ {"user" => {"name" => "Dylan Markow", "username" => "dmarkow"}}
6
+ end
7
+
8
+ let(:test_yaml_string) { "--- \nuser: \n name: Dylan Markow\n username: dmarkow\n" }
9
+
10
+ context "when used" do
11
+ let(:yaml) { Faraday::Request::YAML.new(DummyApp.new) }
12
+ let(:env) do
13
+ {
14
+ :request_headers => {},
15
+ :method => :post,
16
+ :body => test_hash,
17
+ :url => Addressable::URI.parse("http://www.github.com")
18
+ }
19
+ end
20
+
21
+ it "converts the body hash to yaml" do
22
+ request = yaml.call(env)
23
+ request[:body].should == test_yaml_string
24
+ end
25
+
26
+ it "sets the Content-Type header to x-yaml on put and post requests" do
27
+ request = yaml.call(env)
28
+ request[:request_headers]['Content-Type'].should == 'application/x-yaml'
29
+ end
30
+
31
+ it "doesn't set the Content-Type header on get requests" do
32
+ env[:method] = :get
33
+ request = yaml.call(env)
34
+ request[:request_headers]['Content-Type'].should be_nil
35
+ end
36
+ end
37
+
38
+ context "integration tests" do
39
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
40
+ let(:conn) do
41
+ Faraday.new do |conn|
42
+ conn.adapter :test, stubs
43
+ conn.use Faraday::Request::YAML
44
+ end
45
+ end
46
+
47
+ it "converts the body hash to yaml" do
48
+ stubs.post('/echo') do |env|
49
+ posted_as = env[:request_headers]['Content-Type']
50
+ [200, {'Content-Type' => posted_as}, env[:body]]
51
+ end
52
+ response = conn.post('/echo', test_hash, 'Content-Type' => 'application/x-yaml')
53
+ response.headers['Content-Type'].should == 'application/x-yaml'
54
+ response.body.should == test_yaml_string
55
+ end
56
+ end
57
+ end
@@ -1,54 +1,61 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Using the YAML Response Middleware" do
3
+ describe Faraday::Response::YAML do
4
4
 
5
- before do
6
- @stubs = Faraday::Adapter::Test::Stubs.new
7
- @conn = Faraday::Connection.new do |conn|
8
- conn.adapter :test, @stubs
9
- conn.use Faraday::Response::YAML
10
- end
11
- end
5
+ context "when used" do
6
+ let(:yaml) { Faraday::Response::YAML.new }
12
7
 
13
- context "with a valid YAML response" do
14
- before do
15
- @stubs.get("/me") {[200, {'Content-Type' => 'application/x-yaml'}, "---\nuser:\n name: Dylan Markow\n username: dmarkow"]}
8
+ it "handles an empty response" do
9
+ empty = yaml.on_complete(:body => '')
10
+ empty.should be_nil
16
11
  end
17
12
 
18
- it "parses the response into a Hash" do
19
- me = @conn.get("/me").body['user']
20
- me.should be_a(Hash)
21
- me['name'].should == "Dylan Markow"
22
- me['username'].should == "dmarkow"
13
+ it "handles hashes" do
14
+ me = yaml.on_complete(:body => "--- \nuser:\n name: Dylan Markow\n username: dmarkow\n")
15
+ me.class.should == Hash
16
+ me['user']['name'].should == 'Dylan Markow'
17
+ me['user']['username'].should == 'dmarkow'
23
18
  end
24
- end
25
19
 
26
- context "with a nil response" do
27
- before do
28
- @stubs.get("/me") {[200, {'Content-Type' => 'application/x-yaml'}, nil]}
29
- @response = @conn.get("/me")
20
+ it "handles arrays" do
21
+ values = yaml.on_complete(:body => "--- \n- 123\n- 456\n")
22
+ values.class.should == Array
23
+ values.first.should == 123
24
+ values.last.should == 456
30
25
  end
31
26
 
32
- it "still uses the status code" do
33
- @response.status.should == 200
34
- end
35
- it "should skip empty content" do
36
- @response.body.should be_nil
27
+ it "handles arrays of hashes" do
28
+ values = yaml.on_complete(:body => "--- \n- user:\n name: Dylan Markow\n username: dmarkow\n- user:\n name: Rick Olson\n username: technoweenie\n")
29
+ values.class.should == Array
30
+ values.first['user']['username'].should == 'dmarkow'
31
+ values.last['user']['username'].should == 'technoweenie'
37
32
  end
38
- end
39
33
 
40
- context "with an empty response" do
41
- before do
42
- @stubs.get("/me") {[200, {'Content-Type' => 'application/x-yaml'}, ""]}
43
- @response = @conn.get("/me")
34
+ it "handles mixed arrays" do
35
+ values = yaml.on_complete(:body => "--- \n- 123\n- user: \n name: Dylan Markow\n username: dmarkow\n- 456\n")
36
+ values.class.should == Array
37
+ values.first.should == 123
38
+ values.last.should == 456
39
+ values[1]['user']['username'].should == 'dmarkow'
44
40
  end
41
+ end
45
42
 
46
- it "still uses the status code" do
47
- @response.status.should == 200
43
+ context "integration tests" do
44
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
45
+ let(:conn) do
46
+ Faraday.new do |conn|
47
+ conn.adapter :test, stubs
48
+ conn.use Faraday::Response::YAML
49
+ end
48
50
  end
49
51
 
50
- it "should skip empty content" do
51
- @response.body.should be_nil
52
+ it "creates a hash from the body" do
53
+ stubs.get("/me") {[200, {'Content-Type' => 'application/x-yaml'}, "---\nuser:\n name: Dylan Markow\n username: dmarkow"]}
54
+ me = conn.get("/me").body
55
+ me.class.should == Hash
56
+ me['user']['name'].should == "Dylan Markow"
57
+ me['user']['username'].should == "dmarkow"
52
58
  end
53
59
  end
54
60
  end
61
+
data/spec/spec_helper.rb CHANGED
@@ -1 +1,14 @@
1
1
  require File.expand_path('../../lib/faraday_yaml.rb', __FILE__)
2
+
3
+ class DummyApp
4
+ attr_accessor :env
5
+
6
+ def call(env)
7
+ @env = env
8
+ end
9
+
10
+ def reset
11
+ @env = nil
12
+ end
13
+ end
14
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: faraday_yaml
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dylan Markow
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-09 00:00:00 -08:00
13
+ date: 2011-05-02 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -21,7 +21,7 @@ dependencies:
21
21
  requirements:
22
22
  - - ~>
23
23
  - !ruby/object:Gem::Version
24
- version: 2.4.0
24
+ version: 2.5.0
25
25
  type: :development
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - ~>
34
34
  - !ruby/object:Gem::Version
35
- version: 0.5.5
35
+ version: 0.6.1
36
36
  type: :runtime
37
37
  version_requirements: *id002
38
38
  description: YAML Response/Request Middleware for Faraday
@@ -46,16 +46,18 @@ extra_rdoc_files: []
46
46
 
47
47
  files:
48
48
  - .gitignore
49
+ - .rspec
49
50
  - CHANGELOG.md
50
51
  - Gemfile
51
52
  - Gemfile.lock
52
53
  - README.md
53
54
  - Rakefile
54
55
  - faraday_yaml.gemspec
56
+ - lib/faraday/request/yaml.rb
57
+ - lib/faraday/response/yaml.rb
55
58
  - lib/faraday_yaml.rb
56
- - lib/faraday_yaml/request/yaml.rb
57
- - lib/faraday_yaml/response/yaml.rb
58
59
  - lib/faraday_yaml/version.rb
60
+ - spec/request_yaml_spec.rb
59
61
  - spec/response_yaml_spec.rb
60
62
  - spec/spec_helper.rb
61
63
  has_rdoc: true
@@ -82,10 +84,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
84
  requirements: []
83
85
 
84
86
  rubyforge_project: faraday_yaml
85
- rubygems_version: 1.5.0
87
+ rubygems_version: 1.6.2
86
88
  signing_key:
87
89
  specification_version: 3
88
90
  summary: YAML Response/Request Middleware for Faraday
89
91
  test_files:
92
+ - spec/request_yaml_spec.rb
90
93
  - spec/response_yaml_spec.rb
91
94
  - spec/spec_helper.rb
@@ -1,28 +0,0 @@
1
- require 'faraday'
2
-
3
- module Faraday
4
- class Response::YAML < Response::Middleware
5
- begin
6
- require 'yaml'
7
-
8
- def self.register_on_complete(env)
9
- env[:response].on_complete do |finished_env|
10
- finished_env[:body] = parse(finished_env[:body])
11
- end
12
- end
13
- rescue LoadError, NameError => e
14
- self.load_error = e
15
- end
16
-
17
- def initialize(app)
18
- super
19
- @parser = nil
20
- end
21
-
22
- def self.parse(body)
23
- YAML.load(body) || nil
24
- rescue Object => err
25
- raise Faraday::Error::ParsingError.new(err)
26
- end
27
- end
28
- end