json_api_client 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8e9a53aa57c72df6c4dc78894649007649a0324
4
- data.tar.gz: 354fdef483cc69ca4b46d86604c4a8b3115c1e2b
3
+ metadata.gz: 12b14973ffe9a4a87d4981a8b56d0817e65c8816
4
+ data.tar.gz: 975fa606cb2b0bea5af663ed098ad45990327efe
5
5
  SHA512:
6
- metadata.gz: 8bf7d6c2a780c673c66f9b976a5005a511d97b07388285713c0f22aca574166d9e21674b556bb3cc92208938cd6d6c54dd3ee5746ad061ee92b406cf62a62978
7
- data.tar.gz: b6bfb230fbb8f55562edd4f4c66acf4443c4539e4f1c4802e180ba519a5df91dca8d59125629a68ad4d0469a8745453bb08e2874a72911f9bfa5f79d037ce407
6
+ metadata.gz: 6b51a687c23ea5a3f4bfddb16c621724d9e41870567c4386a68db8d0486c6003118a83fb657fab6b3a19699c093db0e951a7f2dffdc22b30f893ca916f482610
7
+ data.tar.gz: c6d1d89ccae79e4c9a17f861eb98f1a7030ee3d816304680d84ae17c7ed8b594b271a3378644b2b0816549e89354d875622468842dfcd4b54d1e3f44d8aa0c8e
@@ -5,6 +5,7 @@ module JsonApiClient
5
5
  def parse(klass, response)
6
6
  data = response.body
7
7
  ResultSet.build(klass, data) do |result_set|
8
+ handle_meta(result_set, data)
8
9
  handle_pagination(result_set, data)
9
10
  handle_links(result_set, data)
10
11
  handle_errors(result_set, data)
@@ -13,23 +14,26 @@ module JsonApiClient
13
14
 
14
15
  private
15
16
 
17
+ def handle_meta(result_set, data)
18
+ result_set.meta = data.fetch("meta", {})
19
+ end
20
+
16
21
  def handle_pagination(result_set, data)
17
- meta = data.fetch("meta", {})
18
- result_set.per_page = meta.fetch("per_page") do
22
+ result_set.per_page = result_set.meta.fetch("per_page") do
19
23
  result_set.length
20
24
  end
21
- result_set.total_entries = meta.fetch("total_entries") do
25
+ result_set.total_entries = result_set.meta.fetch("total_entries") do
22
26
  result_set.length
23
27
  end
24
- result_set.current_page = meta.fetch("current_page", 1)
28
+ result_set.current_page = result_set.meta.fetch("current_page", 1)
25
29
 
26
30
  # can fall back to calculating via total entries and per_page
27
- result_set.total_pages = meta.fetch("total_pages") do
31
+ result_set.total_pages = result_set.meta.fetch("total_pages") do
28
32
  (1.0 * result_set.total_entries / result_set.per_page).ceil rescue 1
29
33
  end
30
34
 
31
35
  # can fall back to calculating via per_page and current_page
32
- result_set.offset = meta.fetch("offset") do
36
+ result_set.offset = result_set.meta.fetch("offset") do
33
37
  result_set.per_page * (result_set.current_page - 1)
34
38
  end
35
39
  end
@@ -49,9 +53,9 @@ module JsonApiClient
49
53
  end
50
54
 
51
55
  def handle_errors(result_set, data)
52
- result_set.errors = data.fetch("meta", {}).fetch("errors", [])
56
+ result_set.errors = result_set.meta.fetch("errors", [])
53
57
  end
54
58
  end
55
59
 
56
60
  end
57
- end
61
+ end
@@ -23,9 +23,9 @@ module JsonApiClient
23
23
  end
24
24
 
25
25
  def inspect
26
- "method: #{request_method}; path: #{path}; params: #{params}, headers: #{headers}"
26
+ "#{self.class.name}: method: #{request_method}; path: #{path}; params: #{params}, headers: #{headers}"
27
27
  end
28
28
 
29
29
  end
30
30
  end
31
- end
31
+ end
@@ -56,6 +56,8 @@ module JsonApiClient
56
56
  include Helpers::CustomEndpoints
57
57
  include Helpers::Schemable
58
58
 
59
+ attr_reader :last_request_meta
60
+
59
61
  def save
60
62
  query = persisted? ?
61
63
  Query::Update.new(self.class, attributes) :
@@ -78,9 +80,10 @@ module JsonApiClient
78
80
  def run_request(query)
79
81
  # reset errors if a new request is being made
80
82
  self.errors.clear if self.errors
81
-
83
+
82
84
  result = self.class.run_request(query)
83
85
  self.errors = result.errors
86
+ @last_request_meta = result.meta
84
87
  if result.has_errors?
85
88
  return false
86
89
  else
@@ -92,4 +95,4 @@ module JsonApiClient
92
95
  end
93
96
 
94
97
  end
95
- end
98
+ end
@@ -7,7 +7,8 @@ module JsonApiClient
7
7
  :per_page,
8
8
  :current_page,
9
9
  :errors,
10
- :record_class
10
+ :record_class,
11
+ :meta
11
12
  alias_attribute :limit_value, :per_page
12
13
 
13
14
  def self.build(klass, data)
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-26 00:00:00.000000000 Z
11
+ date: 2014-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport