maremma 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -0
- data/README.md +1 -1
- data/lib/maremma.rb +119 -1
- data/lib/maremma/version.rb +1 -1
- data/spec/{base_spec.rb → maremma_spec.rb} +25 -24
- metadata +3 -4
- data/lib/maremma/base.rb +0 -121
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2afaf83b4f0e5b91eb5b2a74e56a7292919b84d4
|
4
|
+
data.tar.gz: c01ce052e14830d112fdc43e4f6f7cd2da6cfd1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57c2cc958450f2afd128563bfe44226c9a8455cb006d40b3f413349d681347092a4128366f1248618f1211a8306e1fd945adb7cefbda57cc270e86cd89398df1
|
7
|
+
data.tar.gz: ea99d5bfd797b0823f1b6fc4ee8297a69aff7f403056fa83130c0d06da007421350008cb5dd5f1c4ae93684c7b89f4307779568f9c9a3a537fc9c6bc0d61fd37
|
data/Gemfile.lock
CHANGED
@@ -46,6 +46,7 @@ GEM
|
|
46
46
|
rack (1.6.4)
|
47
47
|
rack-test (0.6.3)
|
48
48
|
rack (>= 1.0)
|
49
|
+
rake (10.4.2)
|
49
50
|
rspec (3.4.0)
|
50
51
|
rspec-core (~> 3.4.0)
|
51
52
|
rspec-expectations (~> 3.4.0)
|
@@ -76,6 +77,7 @@ DEPENDENCIES
|
|
76
77
|
bundler (~> 1.0)
|
77
78
|
maremma!
|
78
79
|
rack-test
|
80
|
+
rake
|
79
81
|
rspec (~> 3.4)
|
80
82
|
vcr
|
81
83
|
webmock
|
data/README.md
CHANGED
data/lib/maremma.rb
CHANGED
@@ -1 +1,119 @@
|
|
1
|
-
require '
|
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
|
+
def self.get(url, content_type: 'json', headers: {}, **options)
|
23
|
+
conn = faraday_conn(content_type, options)
|
24
|
+
conn = auth_conn(conn, options)
|
25
|
+
|
26
|
+
conn.options[:timeout] = options[:timeout] || DEFAULT_TIMEOUT
|
27
|
+
|
28
|
+
# make sure we use a 'Host' header
|
29
|
+
headers['Host'] = URI.parse(url).host
|
30
|
+
|
31
|
+
if options[:data]
|
32
|
+
response = conn.post url, {}, headers do |request|
|
33
|
+
request.body = options[:data]
|
34
|
+
end
|
35
|
+
else
|
36
|
+
response = conn.get url, {}, headers
|
37
|
+
end
|
38
|
+
parse_response(response.body)
|
39
|
+
rescue *NETWORKABLE_EXCEPTIONS => error
|
40
|
+
rescue_faraday_error(error)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.faraday_conn(content_type = 'json', options = {})
|
44
|
+
# use short version for html, xml and json
|
45
|
+
content_types = { "html" => 'text/html; charset=UTF-8',
|
46
|
+
"xml" => 'application/xml',
|
47
|
+
"json" => 'application/json' }
|
48
|
+
accept_header = content_types.fetch(content_type, content_type)
|
49
|
+
|
50
|
+
# redirect limit
|
51
|
+
limit = options[:limit] || 10
|
52
|
+
|
53
|
+
Faraday.new do |c|
|
54
|
+
c.headers['Accept'] = accept_header
|
55
|
+
c.headers['User-Agent'] = "spinone - http://#{ENV['HOSTNAME']}"
|
56
|
+
c.use FaradayMiddleware::FollowRedirects, limit: limit, cookie: :all
|
57
|
+
c.request :multipart
|
58
|
+
c.request :json if accept_header == 'application/json'
|
59
|
+
c.use Faraday::Response::RaiseError
|
60
|
+
c.response :encoding
|
61
|
+
c.adapter :excon
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.auth_conn(conn, options)
|
66
|
+
if options[:bearer]
|
67
|
+
conn.authorization :Bearer, options[:bearer]
|
68
|
+
elsif options[:token]
|
69
|
+
conn.authorization :Token, token: options[:token]
|
70
|
+
elsif options[:username]
|
71
|
+
conn.basic_auth(options[:username], options[:password])
|
72
|
+
end
|
73
|
+
conn
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.rescue_faraday_error(error)
|
77
|
+
if error.is_a?(Faraday::ResourceNotFound)
|
78
|
+
{ error: "resource not found", status: 404 }
|
79
|
+
elsif error.is_a?(Faraday::TimeoutError) || error.is_a?(Faraday::ConnectionFailed) || (error.try(:response) && error.response[:status] == 408)
|
80
|
+
{ error: "execution expired", status: 408 }
|
81
|
+
else
|
82
|
+
{ error: parse_error_response(error.message), status: 400 }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.parse_error_response(string)
|
87
|
+
string = parse_response(string)
|
88
|
+
|
89
|
+
if string.is_a?(Hash) && string['error']
|
90
|
+
string['error']
|
91
|
+
else
|
92
|
+
string
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.parse_response(string)
|
97
|
+
from_json(string) || from_xml(string) || from_string(string)
|
98
|
+
end
|
99
|
+
|
100
|
+
protected
|
101
|
+
|
102
|
+
def self.from_xml(string)
|
103
|
+
if Nokogiri::XML(string).errors.empty?
|
104
|
+
Hash.from_xml(string)
|
105
|
+
else
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.from_json(string)
|
111
|
+
JSON.parse(string)
|
112
|
+
rescue JSON::ParserError
|
113
|
+
nil
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.from_string(string)
|
117
|
+
string.gsub(/\s+\n/, "\n").strip.force_encoding('UTF-8')
|
118
|
+
end
|
119
|
+
end
|
data/lib/maremma/version.rb
CHANGED
@@ -1,35 +1,36 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Maremma
|
3
|
+
describe Maremma do
|
4
|
+
subject { Maremma }
|
4
5
|
let(:url) { "http://example.org" }
|
5
6
|
let(:data) { { "name" => "Fred" } }
|
6
7
|
let(:post_data) { { "name" => "Jack" } }
|
7
8
|
|
8
|
-
context "
|
9
|
+
context "get" do
|
9
10
|
it "get json" do
|
10
11
|
stub = stub_request(:get, url).to_return(:body => data.to_json, :status => 200, :headers => { "Content-Type" => "application/json" })
|
11
|
-
response = subject.
|
12
|
+
response = subject.get(url)
|
12
13
|
expect(response).to eq(data)
|
13
14
|
expect(stub).to have_been_requested
|
14
15
|
end
|
15
16
|
|
16
17
|
it "get xml" do
|
17
18
|
stub = stub_request(:get, url).to_return(:body => data.to_xml, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
18
|
-
response = subject.
|
19
|
+
response = subject.get(url, content_type: 'xml')
|
19
20
|
expect(response).to eq('hash' => data)
|
20
21
|
expect(stub).to have_been_requested
|
21
22
|
end
|
22
23
|
|
23
24
|
it "get html" do
|
24
25
|
stub = stub_request(:get, url).to_return(:body => data.to_s, :status => 200, :headers => { "Content-Type" => "text/html" })
|
25
|
-
response = subject.
|
26
|
+
response = subject.get(url, content_type: 'html')
|
26
27
|
expect(response).to eq(data.to_s)
|
27
28
|
expect(stub).to have_been_requested
|
28
29
|
end
|
29
30
|
|
30
31
|
it "post xml" do
|
31
32
|
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.
|
33
|
+
subject.get(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(data) }
|
33
34
|
expect(stub).to have_been_requested
|
34
35
|
end
|
35
36
|
end
|
@@ -37,28 +38,28 @@ describe Maremma::Base do
|
|
37
38
|
context "empty response" do
|
38
39
|
it "get json" do
|
39
40
|
stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/json" })
|
40
|
-
response = subject.
|
41
|
+
response = subject.get(url)
|
41
42
|
expect(response).to be_blank
|
42
43
|
expect(stub).to have_been_requested
|
43
44
|
end
|
44
45
|
|
45
46
|
it "get xml" do
|
46
47
|
stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
47
|
-
response = subject.
|
48
|
+
response = subject.get(url, content_type: 'xml')
|
48
49
|
expect(response).to be_blank
|
49
50
|
expect(stub).to have_been_requested
|
50
51
|
end
|
51
52
|
|
52
53
|
it "get html" do
|
53
54
|
stub = stub_request(:get, url).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "text/html" })
|
54
|
-
response = subject.
|
55
|
+
response = subject.get(url, content_type: 'html')
|
55
56
|
expect(response).to be_blank
|
56
57
|
expect(stub).to have_been_requested
|
57
58
|
end
|
58
59
|
|
59
60
|
it "post xml" do
|
60
61
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:body => nil, :status => 200, :headers => { "Content-Type" => "application/xml" })
|
61
|
-
subject.
|
62
|
+
subject.get(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(response).to be_nil }
|
62
63
|
expect(stub).to have_been_requested
|
63
64
|
end
|
64
65
|
end
|
@@ -68,25 +69,25 @@ describe Maremma::Base do
|
|
68
69
|
|
69
70
|
it "get json" do
|
70
71
|
stub = stub_request(:get, url).to_return(:body => error.to_json, :status => [404], :headers => { "Content-Type" => "application/json" })
|
71
|
-
expect(subject.
|
72
|
+
expect(subject.get(url)).to eq(error)
|
72
73
|
expect(stub).to have_been_requested
|
73
74
|
end
|
74
75
|
|
75
76
|
it "get xml" do
|
76
77
|
stub = stub_request(:get, url).to_return(:body => error.to_xml, :status => [404], :headers => { "Content-Type" => "application/xml" })
|
77
|
-
expect(subject.
|
78
|
+
expect(subject.get(url, content_type: 'xml')).to eq(error)
|
78
79
|
expect(stub).to have_been_requested
|
79
80
|
end
|
80
81
|
|
81
82
|
it "get html" do
|
82
83
|
stub = stub_request(:get, url).to_return(:body => error.to_s, :status => [404], :headers => { "Content-Type" => "text/html" })
|
83
|
-
expect(subject.
|
84
|
+
expect(subject.get(url, content_type: 'html')).to eq(error)
|
84
85
|
expect(stub).to have_been_requested
|
85
86
|
end
|
86
87
|
|
87
88
|
it "post xml" do
|
88
89
|
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.
|
90
|
+
subject.get(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(Hash.from_xml(response.to_s)["hash"]).to eq(error) }
|
90
91
|
expect(stub).to have_been_requested
|
91
92
|
end
|
92
93
|
end
|
@@ -94,28 +95,28 @@ describe Maremma::Base do
|
|
94
95
|
context "request timeout" do
|
95
96
|
it "get json" do
|
96
97
|
stub = stub_request(:get, url).to_return(:status => [408])
|
97
|
-
response = subject.
|
98
|
+
response = subject.get(url)
|
98
99
|
expect(response).to eq(error: "execution expired", status: 408)
|
99
100
|
expect(stub).to have_been_requested
|
100
101
|
end
|
101
102
|
|
102
103
|
it "get xml" do
|
103
104
|
stub = stub_request(:get, url).to_return(:status => [408])
|
104
|
-
response = subject.
|
105
|
+
response = subject.get(url, content_type: 'xml')
|
105
106
|
expect(response).to eq(error: "execution expired", status: 408)
|
106
107
|
expect(stub).to have_been_requested
|
107
108
|
end
|
108
109
|
|
109
110
|
it "get html" do
|
110
111
|
stub = stub_request(:get, url).to_return(:status => [408])
|
111
|
-
response = subject.
|
112
|
+
response = subject.get(url, content_type: 'html')
|
112
113
|
expect(response).to eq(error: "execution expired", status: 408)
|
113
114
|
expect(stub).to have_been_requested
|
114
115
|
end
|
115
116
|
|
116
117
|
it "post xml" do
|
117
118
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_return(:status => [408])
|
118
|
-
subject.
|
119
|
+
subject.get(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(response).to be_nil }
|
119
120
|
expect(stub).to have_been_requested
|
120
121
|
end
|
121
122
|
end
|
@@ -123,28 +124,28 @@ describe Maremma::Base do
|
|
123
124
|
context "request timeout internal" do
|
124
125
|
it "get json" do
|
125
126
|
stub = stub_request(:get, url).to_timeout
|
126
|
-
response = subject.
|
127
|
+
response = subject.get(url)
|
127
128
|
expect(response).to eq(error: "execution expired", status: 408)
|
128
129
|
expect(stub).to have_been_requested
|
129
130
|
end
|
130
131
|
|
131
132
|
it "get xml" do
|
132
133
|
stub = stub_request(:get, url).to_timeout
|
133
|
-
response = subject.
|
134
|
+
response = subject.get(url, content_type: 'xml')
|
134
135
|
expect(response).to eq(error: "execution expired", status: 408)
|
135
136
|
expect(stub).to have_been_requested
|
136
137
|
end
|
137
138
|
|
138
139
|
it "get html" do
|
139
140
|
stub = stub_request(:get, url).to_timeout
|
140
|
-
response = subject.
|
141
|
+
response = subject.get(url, content_type: 'html')
|
141
142
|
expect(response).to eq(error: "execution expired", status: 408)
|
142
143
|
expect(stub).to have_been_requested
|
143
144
|
end
|
144
145
|
|
145
146
|
it "post xml" do
|
146
147
|
stub = stub_request(:post, url).with(:body => post_data.to_xml).to_timeout
|
147
|
-
subject.
|
148
|
+
subject.get(url, content_type: 'xml', data: post_data.to_xml) { |response| expect(response).to be_nil }
|
148
149
|
expect(stub).to have_been_requested
|
149
150
|
end
|
150
151
|
end
|
@@ -155,7 +156,7 @@ describe Maremma::Base do
|
|
155
156
|
it "redirect" do
|
156
157
|
stub_request(:get, url).to_return(status: 301, headers: { location: redirect_url })
|
157
158
|
stub_request(:get, redirect_url).to_return(status: 200, body: "Test")
|
158
|
-
response = subject.
|
159
|
+
response = subject.get(url)
|
159
160
|
expect(response).to eq("Test")
|
160
161
|
end
|
161
162
|
|
@@ -165,7 +166,7 @@ describe Maremma::Base do
|
|
165
166
|
stub_request(:get, redirect_url+ "/x").to_return(status: 301, headers: { location: redirect_url + "/y" })
|
166
167
|
stub_request(:get, redirect_url+ "/y").to_return(status: 301, headers: { location: redirect_url + "/z" })
|
167
168
|
stub_request(:get, redirect_url + "/z").to_return(status: 200, body: "Test")
|
168
|
-
response = subject.
|
169
|
+
response = subject.get(url)
|
169
170
|
expect(response).to eq("Test")
|
170
171
|
end
|
171
172
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maremma
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Fenner
|
@@ -249,10 +249,9 @@ files:
|
|
249
249
|
- README.md
|
250
250
|
- Rakefile
|
251
251
|
- lib/maremma.rb
|
252
|
-
- lib/maremma/base.rb
|
253
252
|
- lib/maremma/version.rb
|
254
253
|
- maremma.gemspec
|
255
|
-
- spec/
|
254
|
+
- spec/maremma_spec.rb
|
256
255
|
- spec/spec_helper.rb
|
257
256
|
homepage: https://github.com/datacite/maremma
|
258
257
|
licenses:
|
@@ -279,5 +278,5 @@ signing_key:
|
|
279
278
|
specification_version: 4
|
280
279
|
summary: Simplified network calls
|
281
280
|
test_files:
|
282
|
-
- spec/
|
281
|
+
- spec/maremma_spec.rb
|
283
282
|
- spec/spec_helper.rb
|
data/lib/maremma/base.rb
DELETED
@@ -1,121 +0,0 @@
|
|
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
|