arr-force 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1 @@
1
+ require 'autotest/bundler'
data/.gemtest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,41 @@
1
+ !.gitignore
2
+ *.gem
3
+ *.rbc
4
+ *.sw[a-p]
5
+ *.tmproj
6
+ *.tmproject
7
+ *.un~
8
+ *~
9
+ .DS_Store
10
+ .Spotlight-V100
11
+ .Trashes
12
+ ._*
13
+ .bundle
14
+ .config
15
+ .directory
16
+ .elc
17
+ .redcar
18
+ .yardoc
19
+ /.emacs.desktop
20
+ /.emacs.desktop.lock
21
+ Desktop.ini
22
+ Gemfile.lock
23
+ Icon?
24
+ InstalledFiles
25
+ Session.vim
26
+ Thumbs.db
27
+ \#*\#
28
+ _yardoc
29
+ auto-save-list
30
+ coverage
31
+ doc/
32
+ lib/bundler/man
33
+ pkg
34
+ pkg/*
35
+ rdoc
36
+ spec/reports
37
+ test/tmp
38
+ test/version_tmp
39
+ tmp
40
+ tmtags
41
+ tramp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=nested
3
+ --backtrace
data/.yardopts ADDED
@@ -0,0 +1,3 @@
1
+ --markup markdown
2
+ -
3
+ LICENSE.md
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) 2011, Steve Agalloco
2
+ All rights reserved.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ ArrForce
2
+ ========
3
+
4
+ Parsing XML is teh suck, but sometimes you don't have a choice, who hasn't experienced this joy?
5
+
6
+ ```xml
7
+ <access_tokens>
8
+ <access_token>
9
+ <token>abc</token>
10
+ <secret>123</secret>
11
+ </access_token>
12
+ </access_tokens>
13
+ ```
14
+
15
+ Parsing that returns a hash:
16
+
17
+ ```
18
+ { 'access_tokens' => { 'access_token' => { 'token' = 'abc', 'secret = '123' } }
19
+ ```
20
+
21
+ What you probably wanted was this:
22
+
23
+ ```
24
+ { 'access_tokens' => [ 'access_token' => { 'token' = 'abc', 'secret = '123' } ] }
25
+ ```
26
+
27
+ ArrForce is a Faraday middleware that will do that for you. Just tell ArrForce what keys should be transformed into arrays and ArrForce will make sure they are.
28
+
29
+ Installation
30
+ ------------
31
+ gem install arr-force
32
+
33
+ Usage
34
+ -----
35
+
36
+ Simply add it to your middleware stack:
37
+
38
+ ```ruby
39
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
40
+ builder.use Faraday::Response::ArrForce, :access_tokens
41
+ builder.use Faraday::Response::ParseXml
42
+ end
43
+ ```
44
+
45
+ Note on Patches/Pull Requests
46
+ -----------------------------
47
+
48
+ * Fork the project.
49
+ * Make your feature addition or bug fix.
50
+ * Add tests for it. This is important so I don't break it in a
51
+ future version unintentionally.
52
+ * Commit, do not mess with rakefile, version, or history.
53
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
54
+ * Send me a pull request. Bonus points for topic branches.
55
+
56
+ Copyright
57
+ ---------
58
+
59
+ Copyright (c) 2011 Steve Agalloco. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
10
+ task :test => :spec
11
+
12
+ require 'yard'
13
+ namespace :doc do
14
+ YARD::Rake::YardocTask.new do |task|
15
+ task.files = ['LICENSE.md', 'lib/**/*.rb']
16
+ task.options = ['--markup', 'markdown']
17
+ end
18
+ end
data/arr_force.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/arr_force/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'arr-force'
6
+ gem.version = ArrForce::VERSION
7
+ gem.author = "Steve Agalloco"
8
+ gem.email = 'steve.agalloco@gmail.com'
9
+ gem.homepage = 'https://github.com/spagalloco/arr-force'
10
+ gem.summary = %q{Faraday Middleware to ensure certain keys are converted to arrays}
11
+ gem.description = %q{Faraday Middleware to ensure certain keys are converted to arrays}
12
+
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
16
+ gem.require_paths = ['lib']
17
+
18
+ gem.add_development_dependency 'ZenTest', '~> 4.5'
19
+ gem.add_development_dependency 'maruku', '~> 0.6'
20
+ gem.add_development_dependency 'rake', '~> 0.9'
21
+ gem.add_development_dependency 'rspec', '~> 2.6'
22
+ gem.add_development_dependency 'simplecov', '~> 0.4'
23
+ gem.add_development_dependency 'yard', '~> 0.7'
24
+
25
+ gem.add_dependency 'faraday', '0.7'
26
+ end
@@ -0,0 +1,3 @@
1
+ module ArrForce
2
+ VERSION = "1.0.0"
3
+ end
data/lib/arr_force.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'faraday'
2
+
3
+ class Faraday::Response
4
+ autoload :ArrForce, 'faraday/response/arr_force'
5
+ end
@@ -0,0 +1,41 @@
1
+ require 'faraday'
2
+
3
+ module Faraday
4
+ class Response::ArrForce < Response::Middleware
5
+
6
+ def parse(body)
7
+ return body unless @to_array.any?
8
+
9
+ case body
10
+ when Hash
11
+ normalize(body)
12
+ when Array
13
+ body.map { |item| item.is_a?(Hash) ? normalize(item) : item }
14
+ else
15
+ body
16
+ end
17
+ end
18
+
19
+ def initialize(env = nil, *args)
20
+ @to_array = [args.shift].flatten.map { |k| k.to_s }
21
+ super(env)
22
+ end
23
+
24
+ private
25
+
26
+ def normalize(response_object)
27
+ if response_object.kind_of?(Hash)
28
+ response_object.each_pair do |key, value|
29
+ if @to_array.include?(key.to_s)
30
+ response_object[key] = [normalize(value)].flatten
31
+ else
32
+ response_object[key] = normalize(value)
33
+ end
34
+ end
35
+ else
36
+ response_object
37
+ end
38
+ response_object
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,78 @@
1
+ require 'helper'
2
+
3
+ describe Faraday::Response::ArrForce do
4
+
5
+ context 'when used' do
6
+ let(:arr_force) { Faraday::Response::ArrForce.new(nil, :tokens) }
7
+
8
+ it 'should create an Array for arguments' do
9
+ env = { :body => { "tokens" => { "token" => '123', 'secret' => 'abc' } } }
10
+ res = arr_force.on_complete(env)
11
+ res.class.should == Hash
12
+ res['tokens'].class.should == Array
13
+ end
14
+
15
+ it 'should pass through strings' do
16
+ env = { :body => 'this is a string' }
17
+ res = arr_force.on_complete(env)
18
+ res.class.should == String
19
+ res.should == 'this is a string'
20
+ end
21
+
22
+ it 'should properly handle arrays' do
23
+ env = { :body => ['abc', { 'somekey' => 'somevalue'}, 123] }
24
+ res = arr_force.on_complete(env)
25
+ res.class.should == Array
26
+ end
27
+
28
+ it 'should properly handle keys nested in arrays' do
29
+ env = { :body => ['abc', { 'tokens' => 'boo'}, 123] }
30
+ res = arr_force.on_complete(env)
31
+ res.class.should == Array
32
+ res[1]['tokens'].class.should == Array
33
+ end
34
+ end
35
+
36
+ context 'when not configured' do
37
+ let(:arr_force) { Faraday::Response::ArrForce.new }
38
+
39
+ it 'should have no impact' do
40
+ env = { :body => { "tokens" => { "token" => '123', 'secret' => 'abc' } } }
41
+ res = arr_force.on_complete(env)
42
+ res.class.should == Hash
43
+ res['tokens'].class.should == Hash
44
+ end
45
+ end
46
+
47
+ context 'when passed multiple arguments' do
48
+ let(:arr_force) { Faraday::Response::ArrForce.new(nil, [:tokens, :access_tokens]) }
49
+
50
+ it 'should create an Array for arguments' do
51
+ env = { :body => { "tokens" => { "token" => '123', 'secret' => 'abc' }, 'access_tokens' => 'abcdefg' } }
52
+ res = arr_force.on_complete(env)
53
+ res.class.should == Hash
54
+ res['tokens'].class.should == Array
55
+ res['access_tokens'].class.should == Array
56
+ end
57
+ end
58
+
59
+ context 'integration test' do
60
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
61
+ let(:connection) do
62
+ Faraday::Connection.new do |builder|
63
+ builder.adapter :test, stubs
64
+ builder.use Faraday::Response::ArrForce, :tokens, :voodoo
65
+ end
66
+ end
67
+
68
+ # although it is not good practice to pass a hash as the body, if we add ParseJson
69
+ # to the middleware stack we end up testing two middlewares instead of one
70
+ it 'should create a Hash from the body' do
71
+ stubs.get('/hash') {[200, {'content-type' => 'application/json; charset=utf-8'}, { "tokens" => { "token" => '123', 'secret' => 'abc' }, "otherkey" => 'xyz' }]}
72
+ res = connection.get('/hash').body
73
+ res['tokens'].class.should == Array
74
+ res['tokens'].first['token'].should == '123'
75
+ res['otherkey'].should == 'xyz'
76
+ end
77
+ end
78
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'although not required, its recommended you use bundler when running the tests'
5
+ end
6
+
7
+ require 'simplecov'
8
+ SimpleCov.start
9
+
10
+ require 'rspec'
11
+ require 'arr_force'
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arr-force
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Steve Agalloco
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-25 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: ZenTest
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: "4.5"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: maruku
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "0.6"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: "0.9"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: "2.6"
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: simplecov
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: "0.4"
69
+ type: :development
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: yard
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ version: "0.7"
80
+ type: :development
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: faraday
84
+ prerelease: false
85
+ requirement: &id007 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - "="
89
+ - !ruby/object:Gem::Version
90
+ version: "0.7"
91
+ type: :runtime
92
+ version_requirements: *id007
93
+ description: Faraday Middleware to ensure certain keys are converted to arrays
94
+ email: steve.agalloco@gmail.com
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files: []
100
+
101
+ files:
102
+ - .autotest
103
+ - .gemtest
104
+ - .gitignore
105
+ - .rspec
106
+ - .yardopts
107
+ - Gemfile
108
+ - LICENSE.md
109
+ - README.md
110
+ - Rakefile
111
+ - arr_force.gemspec
112
+ - lib/arr_force.rb
113
+ - lib/arr_force/version.rb
114
+ - lib/faraday/response/arr_force.rb
115
+ - spec/arr_force_spec.rb
116
+ - spec/helper.rb
117
+ has_rdoc: true
118
+ homepage: https://github.com/spagalloco/arr-force
119
+ licenses: []
120
+
121
+ post_install_message:
122
+ rdoc_options: []
123
+
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: "0"
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project:
141
+ rubygems_version: 1.6.1
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Faraday Middleware to ensure certain keys are converted to arrays
145
+ test_files:
146
+ - spec/arr_force_spec.rb
147
+ - spec/helper.rb