Skip to content

Commit a2bcb6d

Browse files
committed
Start factoring out http query building from execution.
1 parent 081cd41 commit a2bcb6d

File tree

2 files changed

+65
-48
lines changed

2 files changed

+65
-48
lines changed

β€Žlib/jsonapi/client.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ class Client
99
def initialize(base_url: nil, headers: {})
1010
@base_url = base_url
1111
@headers = {
12-
'Content-Type' => 'application/vnd.api+json',
13-
'Accept' => 'application/vnd.api+json'
12+
:'Content-Type' => 'application/vnd.api+json',
13+
:Accept => 'application/vnd.api+json'
1414
}.merge!(headers)
1515
end
1616

1717
def [](endpoint)
1818
request(@base_url, endpoint)
1919
end
2020

21-
def request(base_url, endpoint, headers = {})
22-
Request.new(base_url, endpoint, @headers.merge(headers))
21+
def request(base_url, endpoint)
22+
req = Request.new(base_url, endpoint)
23+
req.headers(@headers) if @headers.any?
24+
25+
req
2326
end
2427
end
2528
end

β€Žlib/jsonapi/client/request.rb

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,90 +6,104 @@
66
module JSONAPI
77
class Client
88
class Request
9-
METHOD_CLASS = {
10-
get: Net::HTTP::Get,
11-
post: Net::HTTP::Post,
12-
patch: Net::HTTP::Patch,
13-
delete: Net::HTTP::Delete
14-
}.freeze
15-
16-
def initialize(base_url, endpoint, headers)
9+
def initialize(base_url, endpoint = nil)
1710
@base_url = base_url
1811
@endpoint = endpoint
19-
@params = []
20-
@headers = headers
12+
@params = {}
13+
@headers = {}
2114
end
2215

2316
def list
24-
request(:get, uri)
25-
end
26-
27-
def request(method, uri, data = {})
28-
uri = URI(uri)
29-
req = METHOD_CLASS[method].new(uri, @headers)
30-
req.body = data if data
31-
32-
http_res = Net::HTTP.start(uri.host, uri.port) do |http|
33-
http.request(req)
34-
end
35-
36-
Response.new(http_res)
17+
@method = :get
18+
@uri = uri
19+
self
3720
end
3821

3922
def find(id)
40-
request(:get, uri(id))
23+
@method = :get
24+
@uri = uri(id)
25+
self
4126
end
4227

4328
def create(hash)
4429
hash[:type] ||= @endpoint
45-
request(:post, uri, hash)
30+
@method = :post
31+
@uri = uri
32+
@data = hash
33+
self
4634
end
4735

4836
def update(id, hash)
4937
hash[:type] ||= @endpoint
50-
request(:patch, uri(id), hash)
38+
@method = :patch
39+
@uri = uri(id)
40+
@data = hash
41+
self
5142
end
5243

5344
def delete(id)
54-
request(:delete, uri(id))
45+
@method = :delete
46+
@uri = uri(id)
47+
self
5548
end
5649

57-
def headers(hash)
58-
@params.merge!(hash)
50+
def include(hash)
51+
include = JSONAPI::IncludeDirective.new(hash).to_string
52+
@params[:include] = include
5953
self
6054
end
6155

62-
def params(hash)
63-
@params <<
64-
if hash.is_a?(String)
65-
hash
66-
elsif hash.is_a?(Hash)
67-
hash.map { |k, v| "#{k}=#{v}" }
68-
end
56+
def fields(hash)
57+
hash = { @endpoint => hash } if hash.is_a?(Array)
58+
hash.each do |k, v|
59+
@params["fields[#{k}]".to_sym] = v.join(',')
60+
end
6961
self
7062
end
7163

72-
def include(hash)
73-
include = JSONAPI::IncludeDirective.new(hash).to_string
74-
@params << "include=#{include}"
64+
def params(hash)
65+
@params.merge!(hash)
7566
self
7667
end
7768

78-
def fields(hash)
79-
hash = { @endpoint => hash } if hash.is_a?(Array)
80-
@params += hash.map { |k, v| "fields[#{k}]=#{v.join(',')}" }
69+
def headers(hash)
70+
@headers.merge!(hash)
8171
self
8272
end
8373

74+
def to_h
75+
{
76+
method: @method,
77+
uri: full_uri,
78+
base_uri: @uri,
79+
data: @data,
80+
params: @params,
81+
headers: @headers
82+
}
83+
end
84+
8485
private
8586

87+
def query
88+
@params.map { |k, v| "#{k}=#{v}" }.join('&')
89+
end
90+
8691
def uri(id = nil)
87-
uri = "#{@base_url}/#{@endpoint}"
92+
uri = @base_url
93+
uri += "/#{@endpoint}" unless @endpoint.nil?
8894
uri += "/#{id}" if id
89-
uri += "?#{@params.join('&')}" if @params.any?
9095

9196
uri
9297
end
98+
99+
def full_uri
100+
full_uri = @uri
101+
query.tap do |q|
102+
full_uri += "?#{q}" unless q.empty?
103+
end
104+
105+
full_uri
106+
end
93107
end
94108
end
95109
end

0 commit comments

Comments
 (0)