json_api_client 0.9.6 → 1.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +274 -84
- data/lib/json_api_client.rb +9 -4
- data/lib/json_api_client/connection.rb +5 -5
- data/lib/json_api_client/error_collector.rb +29 -0
- data/lib/json_api_client/errors.rb +7 -5
- data/lib/json_api_client/helpers.rb +3 -0
- data/lib/json_api_client/helpers/associable.rb +4 -3
- data/lib/json_api_client/helpers/attributable.rb +15 -46
- data/lib/json_api_client/helpers/custom_endpoints.rb +3 -10
- data/lib/json_api_client/helpers/dynamic_attributes.rb +61 -0
- data/lib/json_api_client/helpers/linkable.rb +28 -18
- data/lib/json_api_client/helpers/paginatable.rb +13 -0
- data/lib/json_api_client/helpers/parsable.rb +1 -1
- data/lib/json_api_client/helpers/queryable.rb +4 -3
- data/lib/json_api_client/helpers/requestable.rb +60 -0
- data/lib/json_api_client/linking.rb +7 -0
- data/lib/json_api_client/linking/included_data.rb +40 -0
- data/lib/json_api_client/linking/links.rb +29 -0
- data/lib/json_api_client/linking/top_level_links.rb +30 -0
- data/lib/json_api_client/meta_data.rb +10 -0
- data/lib/json_api_client/middleware/json_request.rb +3 -4
- data/lib/json_api_client/middleware/status.rb +10 -1
- data/lib/json_api_client/paginating.rb +5 -0
- data/lib/json_api_client/paginating/paginator.rb +80 -0
- data/lib/json_api_client/parsers.rb +5 -0
- data/lib/json_api_client/parsers/parser.rb +55 -0
- data/lib/json_api_client/query.rb +2 -7
- data/lib/json_api_client/query/builder.rb +126 -0
- data/lib/json_api_client/query/requestor.rb +77 -0
- data/lib/json_api_client/resource.rb +3 -59
- data/lib/json_api_client/result_set.rb +11 -29
- data/lib/json_api_client/schema.rb +15 -30
- data/lib/json_api_client/version.rb +1 -1
- metadata +36 -19
- data/lib/json_api_client/link.rb +0 -11
- data/lib/json_api_client/link_definition.rb +0 -27
- data/lib/json_api_client/linked_data.rb +0 -75
- data/lib/json_api_client/parser.rb +0 -63
- data/lib/json_api_client/query/base.rb +0 -38
- data/lib/json_api_client/query/create.rb +0 -17
- data/lib/json_api_client/query/custom.rb +0 -22
- data/lib/json_api_client/query/destroy.rb +0 -12
- data/lib/json_api_client/query/find.rb +0 -19
- data/lib/json_api_client/query/linked.rb +0 -24
- data/lib/json_api_client/query/update.rb +0 -13
- data/lib/json_api_client/scope.rb +0 -48
@@ -1,63 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
class Parser
|
3
|
-
|
4
|
-
class << self
|
5
|
-
def parse(klass, response)
|
6
|
-
data = response.body
|
7
|
-
ResultSet.build(klass, data) do |result_set|
|
8
|
-
handle_meta(result_set, data)
|
9
|
-
handle_pagination(result_set, data)
|
10
|
-
handle_links(result_set, data)
|
11
|
-
handle_errors(result_set, data)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def handle_meta(result_set, data)
|
18
|
-
result_set.meta = HashWithIndifferentAccess.new(data.fetch("meta", nil))
|
19
|
-
end
|
20
|
-
|
21
|
-
def handle_pagination(result_set, data)
|
22
|
-
result_set.per_page = result_set.meta.fetch("per_page") do
|
23
|
-
result_set.length
|
24
|
-
end
|
25
|
-
result_set.total_entries = result_set.meta.fetch("total_entries") do
|
26
|
-
result_set.length
|
27
|
-
end
|
28
|
-
result_set.current_page = result_set.meta.fetch("current_page") do
|
29
|
-
result_set.meta.fetch("page", 1)
|
30
|
-
end
|
31
|
-
|
32
|
-
# can fall back to calculating via total entries and per_page
|
33
|
-
result_set.total_pages = result_set.meta.fetch("total_pages") do
|
34
|
-
(1.0 * result_set.total_entries / result_set.per_page).ceil rescue 1
|
35
|
-
end
|
36
|
-
|
37
|
-
# can fall back to calculating via per_page and current_page
|
38
|
-
result_set.offset = result_set.meta.fetch("offset") do
|
39
|
-
result_set.per_page * (result_set.current_page - 1)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def handle_links(result_set, data)
|
44
|
-
return if result_set.empty?
|
45
|
-
|
46
|
-
linked_data = LinkedData.new(
|
47
|
-
data.fetch("linked", {}),
|
48
|
-
LinkDefinition.new(data.fetch("links", {})),
|
49
|
-
result_set.record_class
|
50
|
-
)
|
51
|
-
|
52
|
-
result_set.each do |resource|
|
53
|
-
resource.linked_data = linked_data
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def handle_errors(result_set, data)
|
58
|
-
result_set.errors = result_set.meta.fetch("errors", [])
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require "addressable/uri"
|
2
|
-
|
3
|
-
module JsonApiClient
|
4
|
-
module Query
|
5
|
-
class Base
|
6
|
-
class_attribute :request_method
|
7
|
-
attr_reader :klass, :headers, :path, :params
|
8
|
-
|
9
|
-
def initialize(klass, args)
|
10
|
-
@klass = klass
|
11
|
-
build_params(args)
|
12
|
-
@headers = klass.default_headers.dup
|
13
|
-
build_path(@params)
|
14
|
-
end
|
15
|
-
|
16
|
-
def build_path(parameters)
|
17
|
-
@path = begin
|
18
|
-
p = klass.path(parameters)
|
19
|
-
if parameters.has_key?(klass.primary_key) && !parameters[klass.primary_key].is_a?(Array)
|
20
|
-
resource_id = parameters.delete(klass.primary_key).to_s
|
21
|
-
encoded_resource_id = Addressable::URI.encode_component(resource_id, Addressable::URI::CharacterClasses::UNRESERVED)
|
22
|
-
p = File.join(p, encoded_resource_id)
|
23
|
-
end
|
24
|
-
p
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def build_params(args)
|
29
|
-
@params = args.dup
|
30
|
-
end
|
31
|
-
|
32
|
-
def inspect
|
33
|
-
"#{self.class.name}: method: #{request_method}; path: #{path}; params: #{params}, headers: #{headers}"
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
module Query
|
3
|
-
class Create < Base
|
4
|
-
self.request_method = :post
|
5
|
-
|
6
|
-
def build_params(args)
|
7
|
-
@params = {klass.resource_name => args.except(klass.primary_key)}
|
8
|
-
end
|
9
|
-
|
10
|
-
# we've nested the parameters, so un-nest them
|
11
|
-
def build_path(parameters)
|
12
|
-
super(parameters[klass.resource_name])
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
module Query
|
3
|
-
class Custom < Base
|
4
|
-
|
5
|
-
def request_method
|
6
|
-
@request_method
|
7
|
-
end
|
8
|
-
|
9
|
-
def path
|
10
|
-
[@path, @options[:name]].join("/")
|
11
|
-
end
|
12
|
-
|
13
|
-
def build_params(params)
|
14
|
-
opts = params.dup
|
15
|
-
@request_method = opts.delete(:request_method) || :get
|
16
|
-
@params = opts.delete(:params) || {}
|
17
|
-
@options = opts
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
module Query
|
3
|
-
class Find < Base
|
4
|
-
self.request_method = :get
|
5
|
-
|
6
|
-
def build_params(args)
|
7
|
-
@params = case args
|
8
|
-
when Hash
|
9
|
-
args
|
10
|
-
when Array
|
11
|
-
{klass.primary_key.to_s.pluralize.to_sym => args.join(",")}
|
12
|
-
else
|
13
|
-
{klass.primary_key => args}
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
module Query
|
3
|
-
class Linked
|
4
|
-
attr_accessor :path
|
5
|
-
|
6
|
-
def initialize(path)
|
7
|
-
@path = path
|
8
|
-
end
|
9
|
-
|
10
|
-
def request_method
|
11
|
-
:get
|
12
|
-
end
|
13
|
-
|
14
|
-
def headers
|
15
|
-
{}
|
16
|
-
end
|
17
|
-
|
18
|
-
def params
|
19
|
-
{}
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
module Query
|
3
|
-
class Update < Base
|
4
|
-
self.request_method = :put
|
5
|
-
|
6
|
-
def build_params(args)
|
7
|
-
args = args.dup
|
8
|
-
@params = {klass.primary_key => args.delete(klass.primary_key), klass.resource_name => args}
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module JsonApiClient
|
2
|
-
class Scope
|
3
|
-
attr_reader :klass, :params
|
4
|
-
|
5
|
-
def initialize(klass)
|
6
|
-
@klass = klass
|
7
|
-
@params = {}
|
8
|
-
end
|
9
|
-
|
10
|
-
def where(conditions = {})
|
11
|
-
@params.merge!(conditions)
|
12
|
-
self
|
13
|
-
end
|
14
|
-
alias paginate where
|
15
|
-
|
16
|
-
def order(conditions)
|
17
|
-
where(order: conditions)
|
18
|
-
end
|
19
|
-
|
20
|
-
def includes(*tables)
|
21
|
-
@params[:includes] ||= []
|
22
|
-
@params[:includes] += tables.flatten
|
23
|
-
self
|
24
|
-
end
|
25
|
-
|
26
|
-
def page(number)
|
27
|
-
where(page: number)
|
28
|
-
end
|
29
|
-
|
30
|
-
def first
|
31
|
-
paginate(page: 1, per_page: 1).to_a.first
|
32
|
-
end
|
33
|
-
|
34
|
-
def build
|
35
|
-
klass.new(params)
|
36
|
-
end
|
37
|
-
|
38
|
-
def to_a
|
39
|
-
@to_a ||= klass.find(params)
|
40
|
-
end
|
41
|
-
alias all to_a
|
42
|
-
|
43
|
-
def method_missing(method_name, *args, &block)
|
44
|
-
to_a.send(method_name, *args, &block)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|