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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73a718a6245b965921781bcabdf93ce29f5e892a
4
- data.tar.gz: 427e8a0b459c7c48103faacc44576272ffe3a661
3
+ metadata.gz: ceb147177963eed28f00f95af29fc6a946ec3183
4
+ data.tar.gz: d0e5fee286e28d249b4df0c6e3b1c2367df9f6f0
5
5
  SHA512:
6
- metadata.gz: 842ea8719b57163910152d814a01193882b7d5f0d24c9d463cb6d456c4436ed90c774fe51b60bbbdab751389779210623f77ed01444d4adbc91909811456b309
7
- data.tar.gz: 1322d55a14aa409eb316dd91791f07a00abd886ec8373ab1e97c2b0cd7a66a9a9e79e04bf0bb904d9f897dfbda9f42c0aef4f9a425d5844a582abe658a666e32
6
+ metadata.gz: 1834e46264ca4cd04af564c1e5ea9ef78b1f99a077603f4b6d8d20ac5f844485764cb87f6cb43f8c064b67c9d39052bccd3b2d6ab84f797ae92d65f0a19bf622
7
+ data.tar.gz: 313fabbb4fb844d96f8036e304498800f4a1442f27ff323c15fa21f52c52600c743e193465eb3c5090ae87bb7173455e0a7018973fdc282f4907a5fa6cb668bf
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ *.gem
@@ -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
@@ -6,5 +6,6 @@ require 'namira/config'
6
6
  require 'namira/query_builder'
7
7
  require 'namira/backend'
8
8
  require 'namira/extensions/hash_key_path'
9
+ require 'namira/auth'
9
10
 
10
11
  Hash.include(Namira::Extensions::HashKeyPath)
@@ -0,0 +1,2 @@
1
+ require_relative 'auth/base'
2
+ require_relative 'auth/http_basic'
@@ -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
@@ -0,0 +1,16 @@
1
+ module Namira
2
+ module Auth
3
+ class HTTPBasic < Base
4
+ attr_reader :user, :pass
5
+
6
+ def initialize(user:, pass:)
7
+ @user = user
8
+ @pass = pass
9
+ end
10
+
11
+ def sign(request)
12
+ request.basic_auth(user: user, pass: pass)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -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. But this is an option if you really need to provide a custom networking backend
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
- fail Errors::TooManyRedirects, "Max number of redirects #{@redirect_count} for #{uri}" if @redirect_count > max_redirect
26
+ raise Errors::TooManyRedirects, "Max number of redirects #{@redirect_count} for #{uri}" if @redirect_count > max_redirect
26
27
 
27
- if defined?(::Rails)
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
- :per_operation,
35
- write: timeout,
36
- connect: timeout,
37
- read: timeout
38
- )
39
- .headers(headers)
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
- fail Errors::RedirectError, "Request redirected but no location was supplied" if location.nil?
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
- fail Errors::HTTPError.new("http_error/#{response.status}", response.status)
50
+ raise Errors::HTTPError.new("http_error/#{response.status}", response.status)
53
51
  end
54
52
 
55
53
  rescue HTTP::TimeoutError => e
56
- fail Namira::Errors::Timeout.new(e.message)
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
@@ -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
- self.send("#{k}=", v)
14
+ send("#{k}=", v)
15
15
  end
16
16
  end
17
17
 
18
18
  def headers
19
- @headers ||= ConfigHash.new
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
 
@@ -1,7 +1,6 @@
1
1
  module Namira
2
2
  module Errors
3
3
  class Base < StandardError
4
-
5
4
  end
6
5
  end
7
6
  end
@@ -1,7 +1,6 @@
1
1
  module Namira
2
2
  module Errors
3
3
  class Timeout < Base
4
-
5
4
  end
6
5
  end
7
6
  end
@@ -1,7 +1,6 @@
1
1
  module Namira
2
2
  module Errors
3
3
  class TooManyRedirects < Base
4
-
5
4
  end
6
5
  end
7
6
  end
@@ -8,13 +8,11 @@ module Namira
8
8
  break if value.nil?
9
9
 
10
10
  value = case value
11
- when Hash
12
- value[key] || value[key.to_sym]
13
- when Array
14
- value[key.to_i]
15
- else
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
@@ -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
- @backing.map { |k, v| "#{k}=#{v}" }.join('&')
4
+ to_h.map { |k, v| "#{k}=#{v}" }.join('&')
17
5
  end
18
6
  end
19
7
  end
@@ -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 { |s| s.capitalize }.join('-')
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
@@ -15,5 +15,9 @@ module Namira
15
15
  super
16
16
  end
17
17
  end
18
+
19
+ def respond_to_missing?(method_name, include_private = false)
20
+ @bakcing.respond_to?(method_name) || super
21
+ end
18
22
  end
19
23
  end
@@ -1,3 +1,3 @@
1
1
  module Namira
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
@@ -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'] = "https://rubygems.org"
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
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.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-07-20 00:00:00.000000000 Z
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