namira 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +56 -0
- data/lib/namira.rb +1 -0
- data/lib/namira/auth.rb +2 -0
- data/lib/namira/auth/base.rb +24 -0
- data/lib/namira/auth/http_basic.rb +16 -0
- data/lib/namira/backend.rb +29 -21
- data/lib/namira/config.rb +3 -13
- data/lib/namira/errors/base.rb +0 -1
- data/lib/namira/errors/timeout.rb +0 -1
- data/lib/namira/errors/too_many_redirects.rb +0 -1
- data/lib/namira/extensions/hash_key_path.rb +5 -7
- data/lib/namira/query_builder.rb +2 -14
- data/lib/namira/request.rb +5 -3
- data/lib/namira/response.rb +4 -0
- data/lib/namira/version.rb +1 -1
- data/namira.gemspec +2 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ceb147177963eed28f00f95af29fc6a946ec3183
|
4
|
+
data.tar.gz: d0e5fee286e28d249b4df0c6e3b1c2367df9f6f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1834e46264ca4cd04af564c1e5ea9ef78b1f99a077603f4b6d8d20ac5f844485764cb87f6cb43f8c064b67c9d39052bccd3b2d6ab84f797ae92d65f0a19bf622
|
7
|
+
data.tar.gz: 313fabbb4fb844d96f8036e304498800f4a1442f27ff323c15fa21f52c52600c743e193465eb3c5090ae87bb7173455e0a7018973fdc282f4907a5fa6cb668bf
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.2
|
3
|
+
Exclude:
|
4
|
+
- Rakefile
|
5
|
+
- bin/**/*
|
6
|
+
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Max: 30
|
9
|
+
|
10
|
+
Metrics/LineLength:
|
11
|
+
Max: 160
|
12
|
+
|
13
|
+
Metrics/MethodLength:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Metrics/ParameterLists:
|
17
|
+
Max: 10
|
18
|
+
|
19
|
+
Metrics/CyclomaticComplexity:
|
20
|
+
Max: 10
|
21
|
+
|
22
|
+
DotPosition:
|
23
|
+
EnforcedStyle: leading
|
24
|
+
|
25
|
+
StringLiterals:
|
26
|
+
EnforcedStyle: single_quotes
|
27
|
+
|
28
|
+
Style/AlignParameters:
|
29
|
+
EnforcedStyle: with_fixed_indentation
|
30
|
+
|
31
|
+
Style/MultilineOperationIndentation:
|
32
|
+
EnforcedStyle: indented
|
33
|
+
|
34
|
+
Style/MultilineMethodCallIndentation:
|
35
|
+
EnforcedStyle: indented
|
36
|
+
|
37
|
+
Style/ExtraSpacing:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Style/AndOr:
|
41
|
+
EnforcedStyle: conditionals
|
42
|
+
|
43
|
+
Style/Documentation:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/ClassAndModuleChildren:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Style/ConditionalAssignment:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Style/GuardClause:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/RaiseArgs:
|
56
|
+
Enabled: false
|
data/lib/namira.rb
CHANGED
data/lib/namira/auth.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Namira
|
2
|
+
module Auth
|
3
|
+
class Base
|
4
|
+
attr_accessor :sign_redirects
|
5
|
+
|
6
|
+
def sign_redirects?
|
7
|
+
if @sign_redirects.nil?
|
8
|
+
true
|
9
|
+
else
|
10
|
+
@sign_redirects == true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def sign_request(backend, redirect_count)
|
15
|
+
return if redirect_count > 0 && !sign_redirects?
|
16
|
+
sign(backend)
|
17
|
+
end
|
18
|
+
|
19
|
+
def sign(_backend)
|
20
|
+
raise NotImplementedError, 'Auth should override the `sign` method'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/namira/backend.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
module Namira
|
2
2
|
class Backend
|
3
|
-
|
4
3
|
#
|
5
4
|
# This allows anyone to substitute in their own networking stack.
|
6
5
|
#
|
7
|
-
# Any class that implements this method and resturns a `Namira::Response` object can be a fully qualified backend
|
6
|
+
# Any class that implements this method and resturns a `Namira::Response` object can be a fully qualified backend.
|
8
7
|
#
|
9
8
|
# The required params are
|
10
9
|
#
|
@@ -13,30 +12,29 @@ module Namira
|
|
13
12
|
# headers: The full HTTP headers to send from the request expressed as a Hash
|
14
13
|
# max_redirect: The maximum number of redirects to follow. Passed from the Request
|
15
14
|
# timeout: The number of seconds before a timeout should occure
|
15
|
+
# auth: The Namira::Auth::Base subclass instance or nil to sign the request with
|
16
16
|
#
|
17
|
-
# This class is 100% capable of fufilling all Namira's needs.
|
17
|
+
# This class is 100% capable of fufilling all Namira's needs.
|
18
|
+
# But this is an option if you really need to provide a custom networking backend
|
18
19
|
#
|
19
|
-
def self.send_request(uri:, method:, headers:, max_redirect:, timeout:, body:)
|
20
|
-
Backend.new.send_request(uri, method, headers, max_redirect, timeout, body)
|
20
|
+
def self.send_request(uri:, method:, headers:, max_redirect:, timeout:, body:, auth:)
|
21
|
+
Backend.new.send_request(uri, method, headers, max_redirect, timeout, body, auth)
|
21
22
|
end
|
22
23
|
|
23
|
-
def send_request(uri, method, headers, max_redirect, timeout, body)
|
24
|
+
def send_request(uri, method, headers, max_redirect, timeout, body, auth)
|
24
25
|
@redirect_count ||= 0
|
25
|
-
|
26
|
+
raise Errors::TooManyRedirects, "Max number of redirects #{@redirect_count} for #{uri}" if @redirect_count > max_redirect
|
26
27
|
|
27
|
-
|
28
|
-
Rails.logger.debug "#{method.to_s.upcase} - #{uri}"
|
29
|
-
else
|
30
|
-
puts "#{method.to_s.upcase} - #{uri}"
|
31
|
-
end
|
28
|
+
log_request(method, uri)
|
32
29
|
|
33
30
|
http = HTTP.timeout(
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
31
|
+
:per_operation,
|
32
|
+
write: timeout,
|
33
|
+
connect: timeout,
|
34
|
+
read: timeout
|
35
|
+
).headers(headers)
|
36
|
+
|
37
|
+
http = auth.sign_request(http, @redirect_count) unless auth.nil?
|
40
38
|
|
41
39
|
response = http.send(method, uri, body: body)
|
42
40
|
|
@@ -46,14 +44,24 @@ module Namira
|
|
46
44
|
when 301, 302
|
47
45
|
@redirect_count += 1
|
48
46
|
location = response.headers['Location']
|
49
|
-
|
47
|
+
raise Errors::RedirectError, 'Request redirected but no location was supplied' if location.nil?
|
50
48
|
send_request(location, method, headers, max_redirect, timeout, body)
|
51
49
|
else
|
52
|
-
|
50
|
+
raise Errors::HTTPError.new("http_error/#{response.status}", response.status)
|
53
51
|
end
|
54
52
|
|
55
53
|
rescue HTTP::TimeoutError => e
|
56
|
-
|
54
|
+
raise Namira::Errors::Timeout.new(e.message)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def log_request(method, uri)
|
60
|
+
if defined?(::Rails)
|
61
|
+
Rails.logger.debug "#{method.to_s.upcase} - #{uri}"
|
62
|
+
else
|
63
|
+
puts "#{method.to_s.upcase} - #{uri}"
|
64
|
+
end
|
57
65
|
end
|
58
66
|
end
|
59
67
|
end
|
data/lib/namira/config.rb
CHANGED
@@ -7,26 +7,16 @@ module Namira
|
|
7
7
|
timeout: 5.0,
|
8
8
|
backend: nil,
|
9
9
|
user_agent: "Namira/#{Namira::VERSION}"
|
10
|
-
}
|
10
|
+
}.freeze
|
11
11
|
|
12
12
|
def initialize
|
13
13
|
DEFAULT_SETTINGS.each do |k, v|
|
14
|
-
|
14
|
+
send("#{k}=", v)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
def headers
|
19
|
-
@headers ||=
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
class ConfigHash < Hash
|
24
|
-
def method_missing(name, *args)
|
25
|
-
if name.to_s =~ /=$/
|
26
|
-
self[name.to_s.gsub(/=$/, '')] = args.first
|
27
|
-
else
|
28
|
-
self[name]
|
29
|
-
end
|
19
|
+
@headers ||= OpenStruct.new
|
30
20
|
end
|
31
21
|
end
|
32
22
|
|
data/lib/namira/errors/base.rb
CHANGED
@@ -8,13 +8,11 @@ module Namira
|
|
8
8
|
break if value.nil?
|
9
9
|
|
10
10
|
value = case value
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
nil
|
17
|
-
end
|
11
|
+
when Hash
|
12
|
+
value[key] || value[key.to_sym]
|
13
|
+
when Array
|
14
|
+
value[key.to_i]
|
15
|
+
end
|
18
16
|
end
|
19
17
|
value
|
20
18
|
end
|
data/lib/namira/query_builder.rb
CHANGED
@@ -1,19 +1,7 @@
|
|
1
1
|
module Namira
|
2
|
-
class QueryBuilder
|
3
|
-
def initialize
|
4
|
-
@backing = {}
|
5
|
-
end
|
6
|
-
|
7
|
-
def method_missing(name, *args)
|
8
|
-
if name.to_s =~ /=$/
|
9
|
-
@backing[name.to_s.gsub(/=$/, '')] = args.first
|
10
|
-
else
|
11
|
-
@backing[name.to_s]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
2
|
+
class QueryBuilder < OpenStruct
|
15
3
|
def to_s
|
16
|
-
|
4
|
+
to_h.map { |k, v| "#{k}=#{v}" }.join('&')
|
17
5
|
end
|
18
6
|
end
|
19
7
|
end
|
data/lib/namira/request.rb
CHANGED
@@ -4,11 +4,12 @@ module Namira
|
|
4
4
|
class Request
|
5
5
|
attr_reader :uri, :http_method
|
6
6
|
|
7
|
-
def initialize(uri:, http_method: :get, headers: {}, body: nil)
|
7
|
+
def initialize(uri:, http_method: :get, headers: {}, body: nil, auth: nil)
|
8
8
|
@uri = uri
|
9
9
|
@http_method = http_method
|
10
10
|
@headers = headers || {}
|
11
11
|
@body = body
|
12
|
+
@auth = auth
|
12
13
|
@timeout = Namira.configure.timeout
|
13
14
|
@max_redirect = Namira.configure.max_redirect
|
14
15
|
@backend = Namira.configure.backend || Namira::Backend
|
@@ -29,7 +30,7 @@ module Namira
|
|
29
30
|
{}.tap do |headers|
|
30
31
|
headers['User-Agent'] = @user_agent
|
31
32
|
Namira.configure.headers.each do |k, v|
|
32
|
-
key = k.split('_').map
|
33
|
+
key = k.split('_').map(&:capitalize).join('-')
|
33
34
|
headers[key] = v
|
34
35
|
end
|
35
36
|
@headers.each do |k, v|
|
@@ -45,7 +46,8 @@ module Namira
|
|
45
46
|
headers: build_headers,
|
46
47
|
max_redirect: @max_redirect,
|
47
48
|
timeout: @timeout,
|
48
|
-
body: @body
|
49
|
+
body: @body,
|
50
|
+
auth: @auth
|
49
51
|
)
|
50
52
|
end
|
51
53
|
end
|
data/lib/namira/response.rb
CHANGED
data/lib/namira/version.rb
CHANGED
data/namira.gemspec
CHANGED
@@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
18
|
# delete this section to allow pushing this gem to any host.
|
19
19
|
if spec.respond_to?(:metadata)
|
20
|
-
spec.metadata['allowed_push_host'] =
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
21
|
else
|
22
|
-
raise
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
23
|
end
|
24
24
|
|
25
25
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: namira
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Skylar Schipper
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,6 +95,7 @@ extra_rdoc_files: []
|
|
95
95
|
files:
|
96
96
|
- ".gitignore"
|
97
97
|
- ".rspec"
|
98
|
+
- ".rubocop.yml"
|
98
99
|
- ".travis.yml"
|
99
100
|
- Gemfile
|
100
101
|
- LICENSE.txt
|
@@ -103,6 +104,9 @@ files:
|
|
103
104
|
- bin/console
|
104
105
|
- bin/setup
|
105
106
|
- lib/namira.rb
|
107
|
+
- lib/namira/auth.rb
|
108
|
+
- lib/namira/auth/base.rb
|
109
|
+
- lib/namira/auth/http_basic.rb
|
106
110
|
- lib/namira/backend.rb
|
107
111
|
- lib/namira/config.rb
|
108
112
|
- lib/namira/errors.rb
|