json_api_resource 0.4.0 → 0.4.8

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: 8de53cf54fc4d7b2dac22f9412edab5c7e9f468c
4
- data.tar.gz: 6d3d904ef873a695ec4140739de2c89e29523c9f
3
+ metadata.gz: 8e3b2206af1a260afc2ef92679b96598f28ecea5
4
+ data.tar.gz: 34c28aac26ed5497995f59d3f4fdb24a41d1c079
5
5
  SHA512:
6
- metadata.gz: 203292b024a04149a33e06d93bbf0dbe4623c9db4aff903f23d1d2f5a96215879a7c531b962cab75b1b14a1bbf493d0d437a723e0f279e3b539c20e8c758e284
7
- data.tar.gz: 94e8199d4670a43fb50a1ef5d2da834ea30822a4f8f617386d3940b1242a4c6868fcd738f573c7b9d8fce3cafe53f7f13185405526de7e7637e58fce4265798f
6
+ metadata.gz: 01fadb7785f52b4082dec41558ed850c88be616b968feaf2a024d047d77ebb158147f027d85804b6240ad38998b699dc35aca20d577bfb8349d21cef798eeb38
7
+ data.tar.gz: c72e307cd241889cd37ee040369f05aa7dec9b9a2bbc771e96220337c2bc9e6d38a8590fc3eed829ae68afd8b0ff3a2755e48c944fbee57ff74eae5018660fe5
@@ -2,7 +2,7 @@ module JsonApiResource
2
2
  module Cacheable
3
3
  extend ActiveSupport::Concern
4
4
  def cache_key
5
- @cache_key ||= Digest::SHA256.hexdigest(self.to_json)
5
+ @cache_key ||= Digest::SHA256.hexdigest(self.attributes.to_s)
6
6
  end
7
7
  end
8
8
  end
@@ -6,6 +6,8 @@ module JsonApiResource
6
6
  attr_accessor :linked_data
7
7
  attr_accessor :errors
8
8
 
9
+ MAX_PAGES_FOR_ALL = 25
10
+
9
11
  module ClassMethods
10
12
 
11
13
  include JsonApiResource::Conversions
@@ -21,13 +23,11 @@ module JsonApiResource
21
23
  end
22
24
 
23
25
  def create(attr = {})
24
- run_callbacks :create do
25
- new(:client => self.client_klass.create(attr))
26
- end
26
+ new(:client => self.client_klass.create(attr)).save
27
27
  end
28
28
 
29
29
  def where(opts = {})
30
- opts[:per_page] = opts.fetch(:per_page, self.per_page)
30
+ opts[:per_page] = opts.fetch(:per_page, self.per_page) if self.per_page
31
31
  (self.client_klass.where(opts).all).map! do |result|
32
32
  self.new(:client => result)
33
33
  end
@@ -35,6 +35,19 @@ module JsonApiResource
35
35
  pretty_error e
36
36
  end
37
37
 
38
+ def all_without_pagination(opts = {})
39
+ page_total = 1
40
+ current_page = 1
41
+ all_results = []
42
+ until (current_page > page_total) || (current_page > MAX_PAGES_FOR_ALL)
43
+ page_of_results = where({:page => current_page}.merge(opts))
44
+ all_results << page_of_results
45
+ page_total = page_of_results.meta[:total_pages] || 1
46
+ current_page = current_page + 1
47
+ end
48
+ JsonApiClient::ResultSet.new(all_results.flatten.compact)
49
+ end
50
+
38
51
  private
39
52
 
40
53
  # When we return a collection, these extra attributes on top of the result array from JsonApiClient are present.
@@ -12,11 +12,10 @@ module JsonApiResource
12
12
  attr_accessor :client, :cache_expires_in
13
13
  class_attribute :client_klass, :per_page
14
14
 
15
- define_model_callbacks :save, :create, :update_attributes
15
+ define_model_callbacks :save, :update_attributes
16
16
 
17
- around_create :catch_errors
18
- around_save :catch_errors
19
- around_update_attributes :catch_errors
17
+ around_save :update_meta
18
+ around_update_attributes :update_meta
20
19
 
21
20
  def initialize(opts={})
22
21
  self.client = self.client_klass.new(self.schema)
@@ -36,12 +35,16 @@ module JsonApiResource
36
35
  run_callbacks :save do
37
36
  self.client.save
38
37
  end
38
+ rescue JsonApiClient::Errors::ServerError => e
39
+ pretty_error e
39
40
  end
40
41
 
41
42
  def update_attributes(attrs = {})
42
43
  run_callbacks :update_attributes do
43
44
  self.client.update_attributes(attrs)
44
45
  end
46
+ rescue JsonApiClient::Errors::ServerError => e
47
+ pretty_error e
45
48
  end
46
49
 
47
50
  def attributes=(attr = {})
@@ -72,9 +75,12 @@ module JsonApiResource
72
75
  else
73
76
  super
74
77
  end
78
+
79
+ rescue JsonApiClient::Errors::ServerError => e
80
+ pretty_error e
75
81
  end
76
82
 
77
- def catch_errors
83
+ def update_meta
78
84
  yield
79
85
 
80
86
  self.errors ||= ActiveModel::Errors.new(self)
@@ -82,6 +88,8 @@ module JsonApiResource
82
88
  self.errors.add(k.to_sym, Array(messages).join(', '))
83
89
  end
84
90
  self.errors
91
+
92
+ self.meta = self.client.last_request_meta
85
93
  end
86
94
 
87
95
  def self.method_missing(method, *args, &block)
@@ -104,5 +112,13 @@ module JsonApiResource
104
112
  rescue JsonApiClient::Errors::ServerError => e
105
113
  pretty_error e
106
114
  end
115
+
116
+ def respond_to_missing?(method_name, include_private = false)
117
+ client.respond_to?(method_name.to_sym) || super
118
+ end
119
+
120
+ def self.respond_to_missing?(method_name, include_private = false)
121
+ client_klass.respond_to?(method_name.to_sym) || super
122
+ end
107
123
  end
108
124
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiResource
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Sislow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-06 00:00:00.000000000 Z
11
+ date: 2016-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json_api_client
@@ -93,4 +93,3 @@ signing_key:
93
93
  specification_version: 4
94
94
  summary: Build wrapper/adapter objects around JsonApiClient instances
95
95
  test_files: []
96
- has_rdoc: