wcc-jsonapi 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +14 -0
- data/Rakefile +9 -2
- data/lib/wcc/jsonapi/concerns/api_action_caching.rb +128 -0
- data/lib/wcc/jsonapi/concerns/pagination_helper.rb +49 -0
- data/lib/wcc/jsonapi/concerns/serializer_with_caching.rb +171 -0
- data/lib/wcc/jsonapi/rspec/api_cache_control_examples.rb +97 -0
- data/lib/wcc/jsonapi/rspec/snapshot_helper.rb +158 -0
- data/lib/wcc/jsonapi/rspec.rb +11 -0
- data/lib/wcc/{json_api → jsonapi}/version.rb +1 -1
- data/lib/wcc/jsonapi.rb +9 -0
- data/lib/wcc-jsonapi.rb +3 -0
- metadata +27 -206
- data/.gitignore +0 -14
- data/Gemfile +0 -6
- data/LICENSE.txt +0 -0
- data/lib/generators/wcc/json_api/client_generator.rb +0 -9
- data/lib/wcc/json_api/active_record_shim.rb +0 -54
- data/lib/wcc/json_api/base_client/api_error.rb +0 -26
- data/lib/wcc/json_api/base_client/base_response.rb +0 -127
- data/lib/wcc/json_api/base_client/http_adapter.rb +0 -19
- data/lib/wcc/json_api/base_client/typhoeus_adapter.rb +0 -63
- data/lib/wcc/json_api/base_client.rb +0 -142
- data/wcc-jsonapi.gemspec +0 -38
@@ -1,142 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'wcc'
|
4
|
-
require_relative 'base_client/response'
|
5
|
-
require_relative 'base_client/builder'
|
6
|
-
require_relative 'active_record_shim'
|
7
|
-
|
8
|
-
module WCC::JsonAPI
|
9
|
-
class BaseClient
|
10
|
-
attr_reader :api_url
|
11
|
-
|
12
|
-
def initialize(api_url:, headers: nil, **options)
|
13
|
-
# normalizes a URL to have a slash on the end
|
14
|
-
@api_url = api_url.gsub(/\/+$/, '') + '/'
|
15
|
-
|
16
|
-
@adapter = BaseClient.load_adapter(options[:adapter])
|
17
|
-
|
18
|
-
@options = options
|
19
|
-
@query_defaults = {}
|
20
|
-
@headers = {
|
21
|
-
'Accept' => 'application/json'
|
22
|
-
}.merge(headers || {}).freeze
|
23
|
-
@response_class = options[:response_class] || BaseResponse
|
24
|
-
end
|
25
|
-
|
26
|
-
# performs an HTTP GET request to the specified path within the configured
|
27
|
-
# space and environment. Query parameters are merged with the defaults and
|
28
|
-
# appended to the request.
|
29
|
-
def get(path, query = {})
|
30
|
-
url = URI.join(@api_url, path)
|
31
|
-
|
32
|
-
@response_class.new(self,
|
33
|
-
{ url: url, query: query },
|
34
|
-
get_http(url, query))
|
35
|
-
end
|
36
|
-
|
37
|
-
def post(path, body = {})
|
38
|
-
url = URI.join(@api_url, path)
|
39
|
-
|
40
|
-
@response_class.new(self,
|
41
|
-
{ url: url },
|
42
|
-
post_http(url,
|
43
|
-
body.to_json,
|
44
|
-
headers: { 'Content-Type': 'application/json' }))
|
45
|
-
end
|
46
|
-
|
47
|
-
def put(path, body = {})
|
48
|
-
url = URI.join(@api_url, path)
|
49
|
-
|
50
|
-
@response_class.new(self,
|
51
|
-
{ url: url },
|
52
|
-
put_http(url,
|
53
|
-
body.to_json,
|
54
|
-
headers: { 'Content-Type': 'application/json' }))
|
55
|
-
end
|
56
|
-
|
57
|
-
def delete(path)
|
58
|
-
url = URI.join(@api_url, path)
|
59
|
-
|
60
|
-
@response_class.new(self,
|
61
|
-
{ url: url },
|
62
|
-
delete_http(url))
|
63
|
-
end
|
64
|
-
|
65
|
-
ADAPTERS = {
|
66
|
-
faraday: ['faraday', '~> 0.9'],
|
67
|
-
typhoeus: ['typhoeus', '~> 1.0']
|
68
|
-
}.freeze
|
69
|
-
|
70
|
-
def self.load_adapter(adapter)
|
71
|
-
case adapter
|
72
|
-
when nil
|
73
|
-
ADAPTERS.each do |a, spec|
|
74
|
-
begin
|
75
|
-
gem(*spec)
|
76
|
-
return load_adapter(a)
|
77
|
-
rescue Gem::LoadError
|
78
|
-
next
|
79
|
-
end
|
80
|
-
end
|
81
|
-
raise ArgumentError, 'Unable to load adapter! Please install one of '\
|
82
|
-
"#{ADAPTERS.values.map(&:join).join(',')}"
|
83
|
-
when :faraday
|
84
|
-
require 'faraday'
|
85
|
-
::Faraday.new do |faraday|
|
86
|
-
faraday.response :logger, (Rails.logger if defined?(Rails)), { headers: false, bodies: false }
|
87
|
-
faraday.adapter :net_http
|
88
|
-
end
|
89
|
-
when :typhoeus
|
90
|
-
require_relative 'base_client/typhoeus_adapter'
|
91
|
-
TyphoeusAdapter.new
|
92
|
-
else
|
93
|
-
unless adapter.respond_to?(:get)
|
94
|
-
raise ArgumentError, "Adapter #{adapter} is not invokeable! Please "\
|
95
|
-
"pass use one of #{ADAPTERS.keys} or create a Faraday-compatible adapter"
|
96
|
-
end
|
97
|
-
adapter
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
|
103
|
-
def get_http(url, query, headers = {})
|
104
|
-
headers = @headers.merge(headers || {})
|
105
|
-
|
106
|
-
q = @query_defaults.dup
|
107
|
-
q = q.merge(query) if query
|
108
|
-
|
109
|
-
resp = @adapter.get(url, q, headers)
|
110
|
-
|
111
|
-
resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
|
112
|
-
resp
|
113
|
-
end
|
114
|
-
|
115
|
-
def post_http(url, body, headers: {})
|
116
|
-
headers = @headers.merge(headers || {})
|
117
|
-
|
118
|
-
resp = @adapter.post(url, body, headers)
|
119
|
-
|
120
|
-
resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
|
121
|
-
resp
|
122
|
-
end
|
123
|
-
|
124
|
-
def put_http(url, body, headers: {})
|
125
|
-
headers = @headers.merge(headers || {})
|
126
|
-
|
127
|
-
resp = @adapter.put(url, body, headers)
|
128
|
-
|
129
|
-
resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
|
130
|
-
resp
|
131
|
-
end
|
132
|
-
|
133
|
-
def delete_http(url, headers: {})
|
134
|
-
headers = @headers.merge(headers || {})
|
135
|
-
|
136
|
-
resp = @adapter.delete(url, {}, headers)
|
137
|
-
|
138
|
-
resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
|
139
|
-
resp
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
data/wcc-jsonapi.gemspec
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
lib = File.expand_path('lib', __dir__)
|
4
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
-
require 'wcc/json_api/version'
|
6
|
-
|
7
|
-
Gem::Specification.new do |spec|
|
8
|
-
spec.name = 'wcc-jsonapi'
|
9
|
-
spec.version = WCC::JsonAPI::VERSION
|
10
|
-
spec.authors = ['Watermark Dev']
|
11
|
-
spec.email = ['dev@watermark.org']
|
12
|
-
spec.summary =
|
13
|
-
spec.description = 'Generates ruby clients for OpenAPI endpoints conforming'\
|
14
|
-
'to the JsonAPI convention'
|
15
|
-
spec.homepage = 'https://github.com/watermarkchurch/wcc-api'
|
16
|
-
spec.license = 'MIT'
|
17
|
-
|
18
|
-
spec.files = `git ls-files -z`.split("\x0")
|
19
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
-
spec.require_paths = ['lib']
|
22
|
-
|
23
|
-
spec.add_dependency "openapi_parser", ">= 0.11.1", "< 1.0"
|
24
|
-
|
25
|
-
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
|
-
spec.add_development_dependency 'dotenv', '~> 0.10.0'
|
27
|
-
spec.add_development_dependency 'faraday', '~> 0.9'
|
28
|
-
spec.add_development_dependency 'guard', '~> 2.15'
|
29
|
-
spec.add_development_dependency 'guard-rspec', '~> 4.7'
|
30
|
-
spec.add_development_dependency 'guard-rubocop', '~> 1.3'
|
31
|
-
spec.add_development_dependency 'httplog', '~> 1.0'
|
32
|
-
spec.add_development_dependency 'rake', '~> 12.3'
|
33
|
-
spec.add_development_dependency 'rspec', '~> 3.3'
|
34
|
-
spec.add_development_dependency 'rspec_junit_formatter', '~> 0.3.0'
|
35
|
-
spec.add_development_dependency 'rubocop', '0.69.0'
|
36
|
-
spec.add_development_dependency 'typhoeus', '~> 1.3'
|
37
|
-
spec.add_development_dependency 'webmock', '~> 3.0'
|
38
|
-
end
|