maremma 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30550938a1699f8e0e09fdf6292f3e92a23445d0
4
+ data.tar.gz: 7e61b93e27de91251455a44bfc1fa7cc7c2d8abd
5
+ SHA512:
6
+ metadata.gz: 2aac0440dd63a4d48a8fd01ef2345d0e797df2a8ea275eb789b2e6cc96d86fc4466a5023c33f892d3282d8921f3232a63a5243402efbdc5b679c6b79c4c5e30f
7
+ data.tar.gz: cb1c930340e29f9a2a30f080357aa2bedacb434b895a4dd8710c366ab8b5e36c640963212f7bf9ae51455f0134c4bcf846af40dcb0ee7dfb21b681173650d9fd
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ install:
5
+ - travis_retry bundle install
6
+ script: bundle exec rspec
7
+ deploy:
8
+ provider: rubygems
9
+ api_key:
10
+ secure: YqeY08GVxIbvUPawYI6UrMPXtGUuCMC4u6R1Iup2AGJ7AUlYkMwqrwPfzmZPM2fjCBEpKpbY1OekkFUnUvxZvH4gsqMNB7558At6be/i3W1batAu3KZ6GLRk5HDIQcqb1T3noGU2xjwNprblOgrWO8RKxR/Ey3crqLv77FHtHHBTsI3nPPyYgurs1PvN4hjlxStlYMKlLgRBQeEzSKGajKQwj80HiCV+nHRdEEJ/y+a+ZwaxjMrB7Y7VDW+a+NETQw9yoL1KwVxaugGfpAlqP/oCmRVfL18nyMoVwWqYV44ewQh2NoOpzi1RD05DvFhI4qOHkt0/yRIzO/QhGrhpYmktKR1KgNSu8TJAYcb7J7A2OIx8sqyOraENcZK5fjaatIpSxAg0axo0LnLxKi3zBIr1pcdkzqhQyo1hs1PNvtWSrXDoVgAAb8uqRNfRXKPzrukATvL+gyFJnnxyF/uPzZItxZv/cAbHfNvWJDk3vEquX4P1SiAsG1a1JsWRKgrDPlvT45E74vE1MxIElo+25NktLqutG1dwz/TFFrDcTGSTwO/NijGdAPJVfbTDuVblKCh9UZmf38X9+3QT5U8OA4Z4GaG8Aud0PB+ml27uDGqBtS7PGC69/q3lbV9s7zpYe/9pnhRfHGzNCGcKpPIbW2gUiJINKbd5ZV4KMv1uZCM=
11
+ gem: maremma
12
+ on:
13
+ tags: true
14
+ repo: datacite/maremma
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## v.1.0 (November 28, 2015)
2
+
3
+ [maremma 1.0](https://github.com/datacite/maremma/releases/tag/v.1.0) was released on November 28, 2015:
4
+
5
+ * initial version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-github.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ maremma (1.0.0)
5
+ activesupport (~> 4.2, >= 4.2.5)
6
+ builder (~> 3.2, >= 3.2.2)
7
+ excon (~> 0.45.0)
8
+ faraday (~> 0.9.0)
9
+ faraday-encoding (~> 0.0.1)
10
+ faraday_middleware (~> 0.10.0)
11
+ multi_json (~> 1.11.2)
12
+ nokogiri (~> 1.6.6.4)
13
+ oj (~> 2.13.1)
14
+
15
+ GEM
16
+ remote: https://rubygems.org/
17
+ specs:
18
+ activesupport (4.2.5)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ addressable (2.3.8)
25
+ builder (3.2.2)
26
+ crack (0.4.2)
27
+ safe_yaml (~> 1.0.0)
28
+ diff-lcs (1.2.5)
29
+ excon (0.45.4)
30
+ faraday (0.9.2)
31
+ multipart-post (>= 1.2, < 3)
32
+ faraday-encoding (0.0.1)
33
+ faraday
34
+ faraday_middleware (0.10.0)
35
+ faraday (>= 0.7.4, < 0.10)
36
+ hashdiff (0.2.3)
37
+ i18n (0.7.0)
38
+ json (1.8.3)
39
+ mini_portile (0.6.2)
40
+ minitest (5.8.3)
41
+ multi_json (1.11.2)
42
+ multipart-post (2.0.0)
43
+ nokogiri (1.6.6.4)
44
+ mini_portile (~> 0.6.0)
45
+ oj (2.13.1)
46
+ rack (1.6.4)
47
+ rack-test (0.6.3)
48
+ rack (>= 1.0)
49
+ rspec (3.4.0)
50
+ rspec-core (~> 3.4.0)
51
+ rspec-expectations (~> 3.4.0)
52
+ rspec-mocks (~> 3.4.0)
53
+ rspec-core (3.4.1)
54
+ rspec-support (~> 3.4.0)
55
+ rspec-expectations (3.4.0)
56
+ diff-lcs (>= 1.2.0, < 2.0)
57
+ rspec-support (~> 3.4.0)
58
+ rspec-mocks (3.4.0)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.4.0)
61
+ rspec-support (3.4.1)
62
+ safe_yaml (1.0.4)
63
+ thread_safe (0.3.5)
64
+ tzinfo (1.2.2)
65
+ thread_safe (~> 0.1)
66
+ vcr (3.0.0)
67
+ webmock (1.22.3)
68
+ addressable (>= 2.3.6)
69
+ crack (>= 0.3.2)
70
+ hashdiff
71
+
72
+ PLATFORMS
73
+ ruby
74
+
75
+ DEPENDENCIES
76
+ bundler (~> 1.0)
77
+ maremma!
78
+ rack-test
79
+ rspec (~> 3.4)
80
+ vcr
81
+ webmock
82
+
83
+ BUNDLED WITH
84
+ 1.10.6
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Martin Fenner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Maremma
2
+
3
+ [![Build Status](https://travis-ci.org/datacite/maremma.svg?branch=master)](https://travis-ci.org/datacite/maremma)
4
+ [![Code Climate](https://codeclimate.com/github/datacite/maremma/badges/gpa.svg)](https://codeclimate.com/github/datacite/maremma)
5
+
6
+ Utility library for network calls. Based on [Faraday](https://github.com/lostisland/faraday) and [Excon](https://github.com/excon/excon), provides a wrapper for XML/JSON parsing and error handling.
7
+
8
+ ## Installation
9
+
10
+ The usual way with Bundler: add the following to your `Gemfile` to install the current version of the gem:
11
+
12
+ ```ruby
13
+ gem 'maremma'
14
+ ```
15
+
16
+ Then run `bundle install` to install into your environment.
17
+
18
+ You can also install the gem system-wide in the usual way:
19
+
20
+ ```bash
21
+ gem install maremma
22
+ ```
23
+
24
+ ## Usage
25
+ ```ruby
26
+ get_result(url)
27
+ ```
28
+
29
+ ## License
30
+
31
+ The [MIT License](license.txt) (OSI approved, see more at http://www.opensource.org/licenses/mit-license.php)
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env rake
2
+
3
+ # Get useful helper tasks from Bundler
4
+ require "bundler/gem_tasks"
5
+
@@ -0,0 +1,121 @@
1
+ require 'active_support/all'
2
+ require 'json'
3
+ require 'nokogiri'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+ require 'faraday/encoding'
7
+ require 'excon'
8
+ require 'uri'
9
+
10
+ DEFAULT_TIMEOUT = 60
11
+ NETWORKABLE_EXCEPTIONS = [Faraday::ClientError,
12
+ Faraday::TimeoutError,
13
+ Faraday::SSLError,
14
+ Faraday::ConnectionFailed,
15
+ URI::InvalidURIError,
16
+ Encoding::UndefinedConversionError,
17
+ ArgumentError,
18
+ NoMethodError,
19
+ TypeError]
20
+
21
+ module Maremma
22
+ class Base
23
+ def get_result(url, content_type: 'json', headers: {}, **options)
24
+ conn = faraday_conn(content_type, options)
25
+ conn = auth_conn(conn, options)
26
+
27
+ conn.options[:timeout] = options[:timeout] || DEFAULT_TIMEOUT
28
+
29
+ # make sure we use a 'Host' header
30
+ headers['Host'] = URI.parse(url).host
31
+
32
+ if options[:data]
33
+ response = conn.post url, {}, headers do |request|
34
+ request.body = options[:data]
35
+ end
36
+ else
37
+ response = conn.get url, {}, headers
38
+ end
39
+ parse_response(response.body)
40
+ rescue *NETWORKABLE_EXCEPTIONS => error
41
+ rescue_faraday_error(error)
42
+ end
43
+
44
+ def faraday_conn(content_type = 'json', options = {})
45
+ # use short version for html, xml and json
46
+ content_types = { "html" => 'text/html; charset=UTF-8',
47
+ "xml" => 'application/xml',
48
+ "json" => 'application/json' }
49
+ accept_header = content_types.fetch(content_type, content_type)
50
+
51
+ # redirect limit
52
+ limit = options[:limit] || 10
53
+
54
+ Faraday.new do |c|
55
+ c.headers['Accept'] = accept_header
56
+ c.headers['User-Agent'] = "spinone - http://#{ENV['HOSTNAME']}"
57
+ c.use FaradayMiddleware::FollowRedirects, limit: limit, cookie: :all
58
+ c.request :multipart
59
+ c.request :json if accept_header == 'application/json'
60
+ c.use Faraday::Response::RaiseError
61
+ c.response :encoding
62
+ c.adapter :excon
63
+ end
64
+ end
65
+
66
+ def auth_conn(conn, options)
67
+ if options[:bearer]
68
+ conn.authorization :Bearer, options[:bearer]
69
+ elsif options[:token]
70
+ conn.authorization :Token, token: options[:token]
71
+ elsif options[:username]
72
+ conn.basic_auth(options[:username], options[:password])
73
+ end
74
+ conn
75
+ end
76
+
77
+ def rescue_faraday_error(error)
78
+ if error.is_a?(Faraday::ResourceNotFound)
79
+ { error: "resource not found", status: 404 }
80
+ elsif error.is_a?(Faraday::TimeoutError) || error.is_a?(Faraday::ConnectionFailed) || (error.try(:response) && error.response[:status] == 408)
81
+ { error: "execution expired", status: 408 }
82
+ else
83
+ { error: parse_error_response(error.message), status: 400 }
84
+ end
85
+ end
86
+
87
+ def parse_error_response(string)
88
+ string = parse_response(string)
89
+
90
+ if string.is_a?(Hash) && string['error']
91
+ string['error']
92
+ else
93
+ string
94
+ end
95
+ end
96
+
97
+ def parse_response(string)
98
+ from_json(string) || from_xml(string) || from_string(string)
99
+ end
100
+
101
+ protected
102
+
103
+ def from_xml(string)
104
+ if Nokogiri::XML(string).errors.empty?
105
+ Hash.from_xml(string)
106
+ else
107
+ nil
108
+ end
109
+ end
110
+
111
+ def from_json(string)
112
+ JSON.parse(string)
113
+ rescue JSON::ParserError
114
+ nil
115
+ end
116
+
117
+ def from_string(string)
118
+ string.gsub(/\s+\n/, "\n").strip.force_encoding('UTF-8')
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module Maremma
2
+ VERSION = "1.0.0"
3
+ end
data/lib/maremma.rb ADDED
@@ -0,0 +1 @@
1
+ require 'maremma/base'
data/maremma.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ require "date"
2
+ require File.expand_path("../lib/maremma/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.authors = "Martin Fenner"
6
+ s.email = "mfenner@datacite.org"
7
+ s.name = "maremma"
8
+ s.homepage = "https://github.com/datacite/maremma"
9
+ s.summary = "Simplified network calls"
10
+ s.date = Date.today
11
+ s.description = "Simplifies network calls, including json/xml parsing and error handling. Based on Faraday."
12
+ s.require_paths = ["lib"]
13
+ s.version = Maremma::VERSION
14
+ s.extra_rdoc_files = ["README.md"]
15
+ s.license = 'MIT'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.require_paths = ["lib"]
20
+
21
+ # Declary dependencies here, rather than in the Gemfile
22
+ s.add_dependency 'faraday', '~> 0.9.0'
23
+ s.add_dependency 'faraday_middleware', '~> 0.10.0'
24
+ s.add_dependency 'faraday-encoding', '~> 0.0.1'
25
+ s.add_dependency 'excon', '~> 0.45.0'
26
+ s.add_dependency 'nokogiri', '~> 1.6.6.4'
27
+ s.add_dependency 'builder', '~> 3.2', '>= 3.2.2'
28
+ s.add_dependency 'multi_json', '~> 1.11.2'
29
+ s.add_dependency 'oj', '~> 2.13.1'
30
+ s.add_dependency 'activesupport', '~> 4.2', '>= 4.2.5'
31
+ s.add_development_dependency 'bundler', '~> 1.0'
32
+ s.add_development_dependency 'rspec', '~> 3.4'
33
+ s.add_development_dependency 'rake'
34
+ s.add_development_dependency 'rack-test'
35
+ s.add_development_dependency 'vcr'
36
+ s.add_development_dependency 'webmock'
37
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,211 @@
1
+ require 'spec_helper'
2
+
3
+ describe Maremma::Base do
4
+ let(:url) { "http://example.org" }
5
+ let(:data) { { "name" => "Fred" } }
6
+ let(:post_data) { { "name" => "Jack" } }
7
+
8
+ context "get_result" do
9
+ it "get json" do
10
+ stub = stub_request(:get, url).to_return(:body => data.to_json, :status => 200, :headers => { "Content-Type" => "application/json" })
11
+ response = subject.get_result(url)
12
+ expect(response).to eq(data)
13
+ expect(stub).to have_been_requested
14
+ end
15
+
16
+ it "get xml" do
17
+ stub = stub_request(:get, url).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "application/xml" })
18
+ response = subject.get_result(url, content_type: 'xml')
19
+ expect(response).to eq('hash' => data)
20
+ expect(stub).to have_been_requested
21
+ end
22
+
23
+ it "get html" do
24
+ stub = stub_request(:get, url).to_return(:body => data.to_s, :status => 200, :headers => { "Content-Type" => "text/html" })
25
+ response = subject.get_result(url, content_type: 'html')
26
+ expect(response).to eq(data.to_s)
27
+ expect(stub).to have_been_requested
28
+ end
29
+
30
+ it "post xml" do
31
+ stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "text/html" })
32
+ subject.get_result(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(data) }
33
+ expect(stub).to have_been_requested
34
+ end
35
+ end
36
+
37
+ context "empty response" do
38
+ it "get json" do
39
+ stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/json" })
40
+ response = subject.get_result(url)
41
+ expect(response).to be_blank
42
+ expect(stub).to have_been_requested
43
+ end
44
+
45
+ it "get xml" do
46
+ stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
47
+ response = subject.get_result(url, content_type: 'xml')
48
+ expect(response).to be_blank
49
+ expect(stub).to have_been_requested
50
+ end
51
+
52
+ it "get html" do
53
+ stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "text/html" })
54
+ response = subject.get_result(url, content_type: 'html')
55
+ expect(response).to be_blank
56
+ expect(stub).to have_been_requested
57
+ end
58
+
59
+ it "post xml" do
60
+ stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
61
+ subject.get_result(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(response).to be_nil }
62
+ expect(stub).to have_been_requested
63
+ end
64
+ end
65
+
66
+ context "not found" do
67
+ let(:error) { { error: "resource not found", status: 404 } }
68
+
69
+ it "get json" do
70
+ stub = stub_request(:get, url).to_return(:body => error.to_json, :status => [404], :headers => { "Content-Type" => "application/json" })
71
+ expect(subject.get_result(url)).to eq(error)
72
+ expect(stub).to have_been_requested
73
+ end
74
+
75
+ it "get xml" do
76
+ stub = stub_request(:get, url).to_return(:body => error.to_xml, :status => [404], :headers => { "Content-Type" => "application/xml" })
77
+ expect(subject.get_result(url, content_type: 'xml')).to eq(error)
78
+ expect(stub).to have_been_requested
79
+ end
80
+
81
+ it "get html" do
82
+ stub = stub_request(:get, url).to_return(:body => error.to_s, :status => [404], :headers => { "Content-Type" => "text/html" })
83
+ expect(subject.get_result(url, content_type: 'html')).to eq(error)
84
+ expect(stub).to have_been_requested
85
+ end
86
+
87
+ it "post xml" do
88
+ stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => error.to_xml, :status => [404], :headers => { "Content-Type" => "application/xml" })
89
+ subject.get_result(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(error) }
90
+ expect(stub).to have_been_requested
91
+ end
92
+ end
93
+
94
+ context "request timeout" do
95
+ it "get json" do
96
+ stub = stub_request(:get, url).to_return(:status => [408])
97
+ response = subject.get_result(url)
98
+ expect(response).to eq(error: "execution expired", status: 408)
99
+ expect(stub).to have_been_requested
100
+ end
101
+
102
+ it "get xml" do
103
+ stub = stub_request(:get, url).to_return(:status => [408])
104
+ response = subject.get_result(url, content_type: 'xml')
105
+ expect(response).to eq(error: "execution expired", status: 408)
106
+ expect(stub).to have_been_requested
107
+ end
108
+
109
+ it "get html" do
110
+ stub = stub_request(:get, url).to_return(:status => [408])
111
+ response = subject.get_result(url, content_type: 'html')
112
+ expect(response).to eq(error: "execution expired", status: 408)
113
+ expect(stub).to have_been_requested
114
+ end
115
+
116
+ it "post xml" do
117
+ stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:status => [408])
118
+ subject.get_result(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(response).to be_nil }
119
+ expect(stub).to have_been_requested
120
+ end
121
+ end
122
+
123
+ context "request timeout internal" do
124
+ it "get json" do
125
+ stub = stub_request(:get, url).to_timeout
126
+ response = subject.get_result(url)
127
+ expect(response).to eq(error: "execution expired", status: 408)
128
+ expect(stub).to have_been_requested
129
+ end
130
+
131
+ it "get xml" do
132
+ stub = stub_request(:get, url).to_timeout
133
+ response = subject.get_result(url, content_type: 'xml')
134
+ expect(response).to eq(error: "execution expired", status: 408)
135
+ expect(stub).to have_been_requested
136
+ end
137
+
138
+ it "get html" do
139
+ stub = stub_request(:get, url).to_timeout
140
+ response = subject.get_result(url, content_type: 'html')
141
+ expect(response).to eq(error: "execution expired", status: 408)
142
+ expect(stub).to have_been_requested
143
+ end
144
+
145
+ it "post xml" do
146
+ stub = stub_request(:post, url).with(:body => post_data.to_xml).to_timeout
147
+ subject.get_result(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(response).to be_nil }
148
+ expect(stub).to have_been_requested
149
+ end
150
+ end
151
+
152
+ context "redirect requests" do
153
+ let(:redirect_url) { "http://www.example.org/redirect" }
154
+
155
+ it "redirect" do
156
+ stub_request(:get, url).to_return(status: 301, headers: { location: redirect_url })
157
+ stub_request(:get, redirect_url).to_return(status: 200, body: "Test")
158
+ response = subject.get_result(url)
159
+ expect(response).to eq("Test")
160
+ end
161
+
162
+ it "redirect four times" do
163
+ stub_request(:get, url).to_return(status: 301, headers: { location: redirect_url })
164
+ stub_request(:get, redirect_url).to_return(status: 301, headers: { location: redirect_url + "/x" })
165
+ stub_request(:get, redirect_url+ "/x").to_return(status: 301, headers: { location: redirect_url + "/y" })
166
+ stub_request(:get, redirect_url+ "/y").to_return(status: 301, headers: { location: redirect_url + "/z" })
167
+ stub_request(:get, redirect_url + "/z").to_return(status: 200, body: "Test")
168
+ response = subject.get_result(url)
169
+ expect(response).to eq("Test")
170
+ end
171
+ end
172
+
173
+ context 'parse_error_response' do
174
+ it 'json' do
175
+ string = '{ "error": "An error occured." }'
176
+ expect(subject.parse_error_response(string)).to eq("An error occured.")
177
+ end
178
+
179
+ it 'json not error' do
180
+ string = '{ "customError": "An error occured." }'
181
+ expect(subject.parse_error_response(string)).to eq("customError"=>"An error occured.")
182
+ end
183
+
184
+ it 'xml' do
185
+ string = '<error>An error occured.</error>'
186
+ expect(subject.parse_error_response(string)).to eq("An error occured.")
187
+ end
188
+ end
189
+
190
+ context 'parse_response' do
191
+ it 'from_json' do
192
+ string = '{ "word": "abc" }'
193
+ expect(subject.parse_response(string)).to eq("word" => "abc")
194
+ end
195
+
196
+ it 'from_xml' do
197
+ string = "<word>abc</word>"
198
+ expect(subject.parse_response(string)).to eq("word" => "abc")
199
+ end
200
+
201
+ it 'from_string' do
202
+ string = "abc"
203
+ expect(subject.parse_response(string)).to eq("abc")
204
+ end
205
+
206
+ it 'from_string with utf-8' do
207
+ string = "fön "
208
+ expect(subject.parse_response(string)).to eq("fön")
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'maremma'
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'webmock/rspec'
8
+
9
+ RSpec.configure do |config|
10
+ config.include WebMock::API
11
+ config.include Rack::Test::Methods
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,283 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maremma
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Fenner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.10.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-encoding
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: excon
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.45.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.45.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.6.4
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.6.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: builder
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 3.2.2
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '3.2'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 3.2.2
103
+ - !ruby/object:Gem::Dependency
104
+ name: multi_json
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 1.11.2
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 1.11.2
117
+ - !ruby/object:Gem::Dependency
118
+ name: oj
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 2.13.1
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 2.13.1
131
+ - !ruby/object:Gem::Dependency
132
+ name: activesupport
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '4.2'
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 4.2.5
141
+ type: :runtime
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '4.2'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 4.2.5
151
+ - !ruby/object:Gem::Dependency
152
+ name: bundler
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '1.0'
158
+ type: :development
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - "~>"
163
+ - !ruby/object:Gem::Version
164
+ version: '1.0'
165
+ - !ruby/object:Gem::Dependency
166
+ name: rspec
167
+ requirement: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: '3.4'
172
+ type: :development
173
+ prerelease: false
174
+ version_requirements: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - "~>"
177
+ - !ruby/object:Gem::Version
178
+ version: '3.4'
179
+ - !ruby/object:Gem::Dependency
180
+ name: rake
181
+ requirement: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ type: :development
187
+ prerelease: false
188
+ version_requirements: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ - !ruby/object:Gem::Dependency
194
+ name: rack-test
195
+ requirement: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ type: :development
201
+ prerelease: false
202
+ version_requirements: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ - !ruby/object:Gem::Dependency
208
+ name: vcr
209
+ requirement: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ type: :development
215
+ prerelease: false
216
+ version_requirements: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ - !ruby/object:Gem::Dependency
222
+ name: webmock
223
+ requirement: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - ">="
226
+ - !ruby/object:Gem::Version
227
+ version: '0'
228
+ type: :development
229
+ prerelease: false
230
+ version_requirements: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: '0'
235
+ description: Simplifies network calls, including json/xml parsing and error handling.
236
+ Based on Faraday.
237
+ email: mfenner@datacite.org
238
+ executables: []
239
+ extensions: []
240
+ extra_rdoc_files:
241
+ - README.md
242
+ files:
243
+ - ".gitignore"
244
+ - ".travis.yml"
245
+ - CHANGELOG.md
246
+ - Gemfile
247
+ - Gemfile.lock
248
+ - LICENSE.md
249
+ - README.md
250
+ - Rakefile
251
+ - lib/maremma.rb
252
+ - lib/maremma/base.rb
253
+ - lib/maremma/version.rb
254
+ - maremma.gemspec
255
+ - spec/base_spec.rb
256
+ - spec/spec_helper.rb
257
+ homepage: https://github.com/datacite/maremma
258
+ licenses:
259
+ - MIT
260
+ metadata: {}
261
+ post_install_message:
262
+ rdoc_options: []
263
+ require_paths:
264
+ - lib
265
+ required_ruby_version: !ruby/object:Gem::Requirement
266
+ requirements:
267
+ - - ">="
268
+ - !ruby/object:Gem::Version
269
+ version: '0'
270
+ required_rubygems_version: !ruby/object:Gem::Requirement
271
+ requirements:
272
+ - - ">="
273
+ - !ruby/object:Gem::Version
274
+ version: '0'
275
+ requirements: []
276
+ rubyforge_project:
277
+ rubygems_version: 2.4.5.1
278
+ signing_key:
279
+ specification_version: 4
280
+ summary: Simplified network calls
281
+ test_files:
282
+ - spec/base_spec.rb
283
+ - spec/spec_helper.rb