Skip to content

Commit

Permalink
Add HTTP.rb adapter (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Mar 11, 2019
1 parent 13e8494 commit e7c3403
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 4 deletions.
66 changes: 66 additions & 0 deletions .circleci/config.yml
@@ -0,0 +1,66 @@
# Ruby CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/ruby:2.4.1-node-browsers
environment:
BUNDLER_VERSION: 2.0.1

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/postgres:9.4

working_directory: ~/repo

steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "Gemfile.lock" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run:
name: install bundler 2.0
command: |
gem install bundler
- run:
name: install dependencies
command: |
bundle install --jobs=4 --retry=3 --path vendor/bundle
- save_cache:
paths:
- ./vendor/bundle
key: v1-dependencies-{{ checksum "Gemfile.lock" }}

# run tests!
- run:
name: run tests
command: |
mkdir /tmp/test-results
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
circleci tests split --split-by=timings)"
bundle exec rspec \
--format progress \
--format RspecJunitFormatter \
--out /tmp/test-results/rspec.xml \
--format progress \
$TEST_FILES
# collect reports
- store_test_results:
path: /tmp/test-results
- store_artifacts:
path: /tmp/test-results
destination: test-results
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,6 +8,7 @@ tmp
.rvmrc
.ruby-version
.yardoc
.rspec_status

## BUNDLER
*.gem
Expand Down
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--require spec_helper
--format documentation
--color
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -4,3 +4,5 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in faraday-http.gemspec
gemspec

gem 'faraday', github: 'lostisland/faraday'
47 changes: 47 additions & 0 deletions Gemfile.lock
@@ -1,19 +1,47 @@
GIT
remote: git://github.com/lostisland/faraday.git
revision: 224dd98ebedd35de284b19893e0f34f3fc4b289f
specs:
faraday (0.15.3)
multipart-post (>= 1.2, < 3)

PATH
remote: .
specs:
faraday-http (0.1.0)
http (~> 3.0)

GEM
remote: https://rubygems.org/
specs:
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0)
crack (0.4.3)
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
docile (1.3.1)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
hashdiff (0.3.8)
http (3.3.0)
addressable (~> 2.3)
http-cookie (~> 1.0)
http-form_data (~> 2.0)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
http-form_data (2.1.1)
http_parser.rb (0.6.0)
jaro_winkler (1.5.2)
json (2.2.0)
multipart-post (2.0.0)
parallel (1.14.0)
parser (2.6.0.0)
ast (~> 2.4.0)
powerpack (0.1.2)
psych (3.1.0)
public_suffix (3.0.3)
rainbow (3.0.0)
rake (10.5.0)
rspec (3.8.0)
Expand All @@ -29,6 +57,8 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rspec_junit_formatter (0.4.1)
rspec-core (>= 2, < 4, != 2.12.0)
rubocop (0.65.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
Expand All @@ -39,17 +69,34 @@ GEM
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.4.0)
ruby-progressbar (1.10.0)
safe_yaml (1.0.5)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
unicode-display_width (1.4.1)
webmock (3.5.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
faraday!
faraday-http!
rake (~> 10.0)
rspec (~> 3.0)
rspec_junit_formatter (~> 0.4)
rubocop (~> 0.65)
simplecov
webmock (~> 3.4)

BUNDLED WITH
2.0.1
5 changes: 5 additions & 0 deletions faraday-http.gemspec
Expand Up @@ -37,8 +37,13 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'http', '~> 3.0'

spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rspec_junit_formatter', '~> 0.4'
spec.add_development_dependency 'rubocop', '~> 0.65'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'webmock', '~> 3.4'
end
55 changes: 55 additions & 0 deletions lib/faraday/adapter/http.rb
@@ -0,0 +1,55 @@
# frozen_string_literal: true

module Faraday
class Adapter
# HTTP.rb adapter.
class HTTP < Faraday::Adapter
# Takes the environment and performs the request.
#
# @param env [Faraday::Env] the request environment.
#
# @return [Faraday::Response] the response.
def call(env)
super
perform_request(env)
@app.call(env)
end

private

def perform_request(env)
conn = setup_connection(env)

resp = conn.request env[:method], env[:url], body: env[:body]
save_response(env, resp.code, resp.body.to_s, resp.headers, resp.status.reason)
rescue ::HTTP::TimeoutError
raise Faraday::TimeoutError, $ERROR_INFO
rescue ::HTTP::ConnectionError
raise Faraday::ConnectionFailed, $ERROR_INFO
end

def setup_connection(env)
conn = ::HTTP

request_config(conn, env[:request]) if env[:request]
conn.headers(env.request_headers)
end

def request_config(conn, config)
if (timeout = config[:timeout])
conn = conn.timeout(connect: timeout, read: timeout, write: timeout)
end

if (timeout = config[:open_timeout])
conn = conn.timeout(connect: timeout, write: timeout)
end

if (proxy = config[:proxy])
conn = conn.via(proxy.uri.host, proxy.uri.port, proxy.user, proxy.password)
end

conn
end
end
end
end
5 changes: 5 additions & 0 deletions lib/faraday/http.rb
@@ -1,10 +1,15 @@
# frozen_string_literal: true

require 'faraday'
require 'faraday/http/version'
require 'faraday/adapter/http'

# Extend the main Faraday module.
module Faraday
module Http
class Error < StandardError; end
# Your code goes here...
end

Faraday::Adapter.register_middleware http: Faraday::Adapter::HTTP
end
8 changes: 8 additions & 0 deletions spec/faraday/http/adapter_spec.rb
@@ -0,0 +1,8 @@
# frozen_string_literal: true

RSpec.describe Faraday::Adapter::HTTP do
features :request_body_on_query_methods, :reason_phrase_parse, :trace_method, :connect_method,
:skip_response_body_on_head, :local_socket_binding

it_behaves_like 'an adapter'
end
4 changes: 0 additions & 4 deletions spec/faraday/http_spec.rb
Expand Up @@ -4,8 +4,4 @@
it 'has a version number' do
expect(Faraday::Http::VERSION).not_to be nil
end

it 'does something useful' do
expect(false).to eq(true)
end
end
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
@@ -1,7 +1,15 @@
# frozen_string_literal: true

require 'simplecov'
SimpleCov.start do
add_filter '/spec/'
minimum_coverage 95
minimum_coverage_by_file 80
end

require 'bundler/setup'
require 'faraday/http'
require 'faraday_specs_setup'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -13,4 +21,7 @@
config.expect_with :rspec do |c|
c.syntax = :expect
end

config.order = :random
Kernel.srand config.seed
end

0 comments on commit e7c3403

Please sign in to comment.