namira 0.1.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: 5d00e69f0acebc986d43bdb7dbbb87cf6b4553bf
4
+ data.tar.gz: 799ce7d533162d292cc7c1cd79d584bd4c4385b7
5
+ SHA512:
6
+ metadata.gz: 6f5a074d77c86c01bf9729ea300e81b7c8a54bf67fe7c856040249b4e7177dcd7621f70cfee91549e71df1502311ec43399262e8f16c659ac2b7866eb137e04c
7
+ data.tar.gz: c55ada08736f2c6bb53c182f1929d5f8be3d1708c7946dbc09005fb0a81fa5580a70bae6205bf07024382c080842a9fe24ee76d31697eec7e91cf6fcc9080264
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in namira.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Skylar Schipper
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Namira
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/namira`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'namira'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install namira
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/namira.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "namira"
5
+ require "pry"
6
+
7
+ Namira.configure do |config|
8
+ config.user_agent = 'test'
9
+ config.headers.accept = 'application/json'
10
+ config.headers.content_type = 'application/json'
11
+ end
12
+
13
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,59 @@
1
+ module Namira
2
+ class Backend
3
+
4
+ #
5
+ # This allows anyone to substitute in their own networking stack.
6
+ #
7
+ # Any class that implements this method and resturns a `Namira::Response` object can be a fully qualified backend
8
+ #
9
+ # The required params are
10
+ #
11
+ # uri: The URI to fetch
12
+ # method: The HTTP method to use, expressed as a Symbol i.e. `:get`
13
+ # headers: The full HTTP headers to send from the request expressed as a Hash
14
+ # max_redirect: The maximum number of redirects to follow. Passed from the Request
15
+ # timeout: The number of seconds before a timeout should occure
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
18
+ #
19
+ def self.send_request(uri:, method:, headers:, max_redirect:, timeout:, body:)
20
+ Backend.new.send_request(uri, method, headers, max_redirect, timeout, body)
21
+ end
22
+
23
+ def send_request(uri, method, headers, max_redirect, timeout, body)
24
+ @redirect_count ||= 0
25
+ fail Errors::TooManyRedirects, "Max number of redirects #{@redirect_count} for #{uri}" if @redirect_count > max_redirect
26
+
27
+ if defined?(::Rails)
28
+ Rails.logger.debug "#{method.to_s.upcase} - #{uri}"
29
+ else
30
+ puts "#{method.to_s.upcase} - #{uri}"
31
+ end
32
+
33
+ http = HTTP.timeout(
34
+ :per_operation,
35
+ write: timeout,
36
+ connect: timeout,
37
+ read: timeout
38
+ )
39
+ .headers(headers)
40
+
41
+ response = http.send(method, uri, body: body)
42
+
43
+ case response.status
44
+ when 200..299
45
+ Namira::Response.new(response)
46
+ when 301, 302
47
+ @redirect_count += 1
48
+ location = response.headers['Location']
49
+ fail Errors::RedirectError, "Request redirected but no location was supplied" if location.nil?
50
+ send_request(location, method, headers, max_redirect, timeout, body)
51
+ else
52
+ fail Errors::HTTPError.new("http_error/#{response.status}", response.status)
53
+ end
54
+
55
+ rescue HTTP::TimeoutError => e
56
+ fail Namira::Errors::Timeout.new(e.message)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,38 @@
1
+ module Namira
2
+ class Config
3
+ attr_accessor :max_redirect, :timeout, :backend, :user_agent
4
+
5
+ DEFAULT_SETTINGS = {
6
+ max_redirect: 3,
7
+ timeout: 5.0,
8
+ backend: nil,
9
+ user_agent: "Namira/#{Namira::VERSION}"
10
+ }
11
+
12
+ def initialize
13
+ DEFAULT_SETTINGS.each do |k, v|
14
+ self.send("#{k}=", v)
15
+ end
16
+ end
17
+
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
30
+ end
31
+ end
32
+
33
+ def self.configure
34
+ @config ||= Config.new
35
+ yield(@config) if block_given?
36
+ @config
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ module Namira
2
+ module Errors
3
+ class Base < StandardError
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Namira
2
+ module Errors
3
+ class HTTPError < Base
4
+ attr_reader :status
5
+
6
+ def initialize(msg, status)
7
+ @status = status
8
+ super(msg)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module Namira
2
+ module Errors
3
+ class RedirectError < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module Namira
2
+ module Errors
3
+ class Timeout < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Namira
2
+ module Errors
3
+ class TooManyRedirects < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'errors/base'
2
+ require_relative 'errors/http_error'
3
+ require_relative 'errors/too_many_redirects'
4
+ require_relative 'errors/redirect_error'
5
+ require_relative 'errors/timeout'
@@ -0,0 +1,23 @@
1
+ module Namira
2
+ module Extensions
3
+ module HashKeyPath
4
+ def value_for_key_path(path)
5
+ components = path.split('.')
6
+ value = self
7
+ components.each do |key|
8
+ break if value.nil?
9
+
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
18
+ end
19
+ value
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
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
+
15
+ def to_s
16
+ @backing.map { |k, v| "#{k}=#{v}" }.join('&')
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,52 @@
1
+ require 'http'
2
+
3
+ module Namira
4
+ class Request
5
+ attr_reader :uri, :http_method
6
+
7
+ def initialize(uri:, http_method: :get, headers: {}, body: nil)
8
+ @uri = uri
9
+ @http_method = http_method
10
+ @headers = headers || {}
11
+ @body = body
12
+ @timeout = Namira.configure.timeout
13
+ @max_redirect = Namira.configure.max_redirect
14
+ @backend = Namira.configure.backend || Namira::Backend
15
+ @user_agent = Namira.configure.user_agent
16
+ end
17
+
18
+ def send_request
19
+ @response ||= _send_request(uri)
20
+ end
21
+
22
+ def response
23
+ send_request
24
+ end
25
+
26
+ private
27
+
28
+ def build_headers
29
+ {}.tap do |headers|
30
+ headers['User-Agent'] = @user_agent
31
+ Namira.configure.headers.each do |k, v|
32
+ key = k.split('_').map { |s| s.capitalize }.join('-')
33
+ headers[key] = v
34
+ end
35
+ @headers.each do |k, v|
36
+ headers[k] = v
37
+ end
38
+ end
39
+ end
40
+
41
+ def _send_request(uri)
42
+ @backend.send_request(
43
+ uri: uri,
44
+ method: http_method,
45
+ headers: build_headers,
46
+ max_redirect: @max_redirect,
47
+ timeout: @timeout,
48
+ body: @body
49
+ )
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,19 @@
1
+ module Namira
2
+ class Response
3
+ def initialize(backing)
4
+ @backing = backing
5
+ end
6
+
7
+ def from_json
8
+ @json ||= JSON.parse(@backing.body)
9
+ end
10
+
11
+ def method_missing(name, *args)
12
+ if @backing.respond_to?(name)
13
+ @backing.send(name, *args)
14
+ else
15
+ super
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Namira
2
+ VERSION = "0.1.0"
3
+ end
data/lib/namira.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'namira/version'
2
+ require 'namira/request'
3
+ require 'namira/response'
4
+ require 'namira/errors'
5
+ require 'namira/config'
6
+ require 'namira/query_builder'
7
+ require 'namira/backend'
8
+ require 'namira/extensions/hash_key_path'
9
+
10
+ Hash.include(Namira::Extensions::HashKeyPath)
data/namira.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'namira/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "namira"
8
+ spec.version = Namira::VERSION
9
+ spec.authors = ["Skylar Schipper"]
10
+ spec.email = ["ss@schipp.co"]
11
+
12
+ spec.summary = "A simple wrapper around HTTP"
13
+ spec.description = "This is a simple wrapper around HTTP"
14
+ spec.homepage = "https://github.com/skylarsch/namira"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "pry"
34
+ spec.add_dependency 'http', '~> 2.0.0'
35
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namira
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Skylar Schipper
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: http
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ description: This is a simple wrapper around HTTP
84
+ email:
85
+ - ss@schipp.co
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/namira.rb
100
+ - lib/namira/backend.rb
101
+ - lib/namira/config.rb
102
+ - lib/namira/errors.rb
103
+ - lib/namira/errors/base.rb
104
+ - lib/namira/errors/http_error.rb
105
+ - lib/namira/errors/redirect_error.rb
106
+ - lib/namira/errors/timeout.rb
107
+ - lib/namira/errors/too_many_redirects.rb
108
+ - lib/namira/extensions/hash_key_path.rb
109
+ - lib/namira/query_builder.rb
110
+ - lib/namira/request.rb
111
+ - lib/namira/response.rb
112
+ - lib/namira/version.rb
113
+ - namira.gemspec
114
+ homepage: https://github.com/skylarsch/namira
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ allowed_push_host: https://rubygems.org
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.5.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: A simple wrapper around HTTP
139
+ test_files: []