Skip to content

Commit 8a56bb9

Browse files
committed
Remove HTTP in favor of plain Faraday
1 parent 1fdaacf commit 8a56bb9

File tree

5 files changed

+33
-414
lines changed

5 files changed

+33
-414
lines changed

lib/hyperclient/entry_point.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'hyperclient/link'
2-
require 'hyperclient/http'
2+
require 'faraday_middleware'
3+
require 'faraday/connection'
34

45
module Hyperclient
56
# Public: The EntryPoint is the main public API for Hyperclient. It is used to
@@ -15,21 +16,19 @@ module Hyperclient
1516
# client = Hyperclient::EntryPoint.new('http://my.api.org', options)
1617
#
1718
class EntryPoint
18-
19-
# Public: Returns the Hash with the configuration.
20-
attr_accessor :config
21-
2219
# Public: Initializes an EntryPoint.
2320
#
2421
# url - A String with the entry point of your API.
25-
# config - The Hash options used to setup the HTTP client (default: {})
26-
# See HTTP for more documentation.
27-
def initialize(url, config = {})
28-
@config = config.update(base_uri: url)
22+
def initialize(url)
23+
@url = url
2924
end
3025

3126
def entry
32-
@entry ||= Link.new({'href' => config[:base_uri]}, self).resource
27+
@entry ||= Link.new({'href' => @url}, self).resource
28+
end
29+
30+
def connection
31+
@connection ||= Faraday.new(@url, {headers: default_headers}, &default_faraday_block)
3332
end
3433

3534
# Internal: Delegate the method to the entry point Resource if it exists.
@@ -50,8 +49,17 @@ def respond_to_missing?(method, include_private = false)
5049
entry.respond_to?(method.to_s)
5150
end
5251

53-
def connection
54-
@connection ||= HTTP.new(config)
52+
private
53+
def default_faraday_block
54+
lambda do |faraday|
55+
faraday.request :json
56+
faraday.response :json, content_type: /\bjson$/
57+
faraday.adapter :net_http
58+
end
59+
end
60+
61+
def default_headers
62+
{'Content-Type' => 'application/json'}
5563
end
5664
end
5765
end

lib/hyperclient/http.rb

Lines changed: 0 additions & 180 deletions
This file was deleted.

test/hyperclient/entry_point_test.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@ module Hyperclient
1212
to_return(body: '{"_links": {"self": {"href": "http://my.api.org"}}}', headers: {content_type: 'application/json'})
1313
end
1414

15-
describe 'initialize' do
16-
it 'setups the HTTP config' do
17-
options = {:headers => {'accept-encoding' => 'deflate, gzip'}}
18-
19-
entry_point = EntryPoint.new('http://my.api.org', options)
20-
21-
entry_point.config[:headers].must_include 'accept-encoding'
22-
end
23-
24-
it 'sets the base_uri for HTTP' do
25-
entry_point = EntryPoint.new('http://my.api.org')
15+
describe 'connection' do
16+
it 'creates a Faraday connection with the entry point url'
17+
it 'creates a Faraday connection with the default headers'
18+
it 'creates a Faraday connection with the default block'
19+
end
2620

27-
entry_point.config[:base_uri].must_equal 'http://my.api.org'
28-
end
21+
describe 'entry' do
22+
it 'creates a Link with the entry point url'
23+
it 'returns the entry point Resource'
2924
end
3025

3126
describe 'method missing' do

0 commit comments

Comments
 (0)