faraday_yaml 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ driver.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in faraday_yaml.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Faraday YAML Middleware
2
+
3
+ Yeah, JSON is at least 2.9x cooler than YAML, but sometimes you're stuck using it, right?
4
+
5
+ ## Installation
6
+
7
+ gem install faraday_yaml
8
+
9
+ ### Examples
10
+
11
+ #### Response
12
+ Here's an example: (Github's YAML API is currently deprecated, but it works for the sake of this example)
13
+
14
+ conn = Faraday::Connection.new(:url => "http://github.com") do |builder|
15
+ builder.adapter Faraday.default_adapter
16
+ builder.use Faraday::Response::YAML
17
+ end
18
+
19
+ resp = conn.get do |req|
20
+ req.url "/api/v2/yaml/user/show/dmarkow"
21
+ end
22
+
23
+ u = resp.body
24
+ u['user']['name']
25
+ # => "Dylan Markow"
26
+
27
+ #### Request
28
+
29
+ conn = Faraday::Connection.new(:url => "http://USERNAME:PASSWORD@github.com") do |builder|
30
+ builder.adapter Faraday.default_adapter
31
+ builder.use Faraday::Request::YAML
32
+ builder.use Faraday::Response::YAML
33
+ end
34
+
35
+ resp = conn.post do |req|
36
+ req.url "/api/v2/yaml/user/show/dmarkow"
37
+ req.body = {
38
+ "values" => {
39
+ "location" => "Portland, OR"
40
+ }
41
+ }
42
+ end
43
+
44
+ u = resp.body
45
+ u['user']['location']
46
+ # => "Portland, OR"
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "faraday_yaml/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "faraday_yaml"
7
+ s.version = FaradayYaml::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Dylan Markow"]
10
+ s.email = ["dylan@dylanmarkow.com"]
11
+ s.homepage = "http://github.com/dmarkow/faraday_yaml"
12
+ s.summary = %q{YAML Response/Request Middleware for Faraday}
13
+ s.description = s.summary
14
+
15
+ s.rubyforge_project = "faraday_yaml"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.add_development_dependency "rspec", "~> 2.4.0"
22
+ s.add_runtime_dependency "faraday", "~> 0.5.4"
23
+ end
@@ -0,0 +1,20 @@
1
+ require 'faraday'
2
+
3
+ module Faraday
4
+ class Request::YAML < Faraday::Middleware
5
+ begin
6
+ require 'yaml'
7
+ rescue LoadError, NameError => e
8
+ self.load_error = e
9
+ end
10
+
11
+ def call(env)
12
+ env[:request_headers]['Content-Type'] = 'application/x-yaml'
13
+ if env[:body] && !env[:body].respond_to?(:to_str)
14
+ env[:body] = env[:body].to_yaml
15
+ end
16
+ @app.call env
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,3 @@
1
+ module FaradayYaml
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'faraday_yaml/request/yaml.rb'
2
+ require 'faraday_yaml/response/yaml.rb'
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Using the YAML Response Middleware" do
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
12
+
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"]}
16
+ end
17
+
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"
23
+ end
24
+ end
25
+
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")
30
+ end
31
+
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
37
+ end
38
+ end
39
+
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")
44
+ end
45
+
46
+ it "still uses the status code" do
47
+ @response.status.should == 200
48
+ end
49
+
50
+ it "should skip empty content" do
51
+ @response.body.should be_nil
52
+ end
53
+ end
54
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path('../../lib/faraday_yaml.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_yaml
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Dylan Markow
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-04 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 4
31
+ - 0
32
+ version: 2.4.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: faraday
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ - 5
46
+ - 4
47
+ version: 0.5.4
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: YAML Response/Request Middleware for Faraday
51
+ email:
52
+ - dylan@dylanmarkow.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - README.md
63
+ - Rakefile
64
+ - faraday_yaml.gemspec
65
+ - lib/faraday_yaml.rb
66
+ - lib/faraday_yaml/request/yaml.rb
67
+ - lib/faraday_yaml/response/yaml.rb
68
+ - lib/faraday_yaml/version.rb
69
+ - spec/response_yaml_spec.rb
70
+ - spec/spec_helper.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/dmarkow/faraday_yaml
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project: faraday_yaml
99
+ rubygems_version: 1.3.7
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: YAML Response/Request Middleware for Faraday
103
+ test_files:
104
+ - spec/response_yaml_spec.rb
105
+ - spec/spec_helper.rb