contentful_redis 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 857d3794b9bb00ab99eabfe2d64f95ba6948ff91
4
- data.tar.gz: 3720c767ba68698997c89f877356011a144391a6
3
+ metadata.gz: 62a28f375b611866f2162e41ce48de3153c1c518
4
+ data.tar.gz: 895170f66e0f439d888a23af31936eae7a1b6657
5
5
  SHA512:
6
- metadata.gz: db7d42e5a30c46d4355350d4529eba47e92d8fc1554a694cb17b9f4860d27bf232429308f7022a453a3769ce3d0ac2dd40b5b15650087b6f269c319fd9bcac22
7
- data.tar.gz: b5c378a0b0699cfca7f669383238246e7782a838c5538e84121fcd598789a9815fcfc8854b1e8492d6d6b10ec62910cdf9e772f1746dc379bd1baddb2b233883
6
+ metadata.gz: aa9329a954e232b52f95055fcb78bb54c5e52c62586e87fce92a2dba57b498e5a72697c59b294960221f3337c74a55409b4932ee9fdd016d5f5a54a31b4f0bed
7
+ data.tar.gz: a4a9588e5ffe49506e59cb5d51a4c0fa0ca198b426a11f5a4ca1cf6eee1df44e4cf33d77e95d53ac9485dd28845feb5ee04398922911ae55c0c9f722920ff9ea
@@ -19,7 +19,7 @@ Metrics/BlockLength:
19
19
  Exclude:
20
20
  - 'spec/**/*'
21
21
  Metrics/AbcSize:
22
- Max: 25
22
+ Max: 28
23
23
  Style/SymbolArray:
24
24
  Enabled: false
25
25
  Style/WordArray:
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- contentful_redis (0.0.2)
4
+ contentful_redis (0.1.0)
5
5
  faraday
6
6
  redis-store
7
7
 
@@ -42,8 +42,8 @@ GEM
42
42
  public_suffix (3.0.3)
43
43
  rainbow (3.0.0)
44
44
  rake (12.3.1)
45
- redis (4.0.2)
46
- redis-store (1.5.0)
45
+ redis (4.0.3)
46
+ redis-store (1.6.0)
47
47
  redis (>= 2.2, < 5)
48
48
  rspec (3.8.0)
49
49
  rspec-core (~> 3.8.0)
data/README.md CHANGED
@@ -180,6 +180,19 @@ These attributes are defined in the class declaration as `define_searchable_fiel
180
180
  Contentful::Page.find_by(slug: 'about-us')
181
181
  ```
182
182
 
183
+ Deleting an entry is done by calling destroy on a ContentfulRedis model object or destroy by passing id. This will delete all the redis keys, find and search keys for the entry.
184
+
185
+ ```ruby
186
+ Contentful::Page.destroy('<contentful_uid>')
187
+ ```
188
+
189
+ or
190
+
191
+ ```ruby
192
+ page = Contentful::Page.find('<contentful_uid>')
193
+ page.destroy
194
+ ```
195
+
183
196
  ### Content model overriding
184
197
 
185
198
  Classes should match their content model name, however, if they don't you can override the classes `#name` method.
@@ -7,7 +7,7 @@ require 'contentful_redis'
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'contentful_redis'
9
9
  spec.version = ContentfulRedis::VERSION
10
- spec.authors = ['DanHenton']
10
+ spec.authors = ['DanHenton', 'Edwin Rozario']
11
11
  spec.email = ['Dan.Henton@gmail.com']
12
12
 
13
13
  spec.summary = 'Contentful api wrapper which caches responses from contentful'
@@ -6,7 +6,7 @@ require_relative 'contentful_redis/model_base'
6
6
  # Dir["#{Dir.pwd}/lib/contentful_redis/**/*.rb"].each { |f| require f }
7
7
 
8
8
  module ContentfulRedis
9
- VERSION = '0.0.2'.freeze
9
+ VERSION = '0.1.0'.freeze
10
10
 
11
11
  class << self
12
12
  attr_writer :configuration
@@ -9,8 +9,12 @@ require_relative 'class_finder'
9
9
  # Base class for contentful redis intergation.
10
10
  module ContentfulRedis
11
11
  class ModelBase
12
+ attr_accessor :id
13
+
12
14
  class << self
13
15
  def find(id, env = nil)
16
+ raise ContentfulRedis::Error::ArgumentError, 'Expected Contentful model ID' unless id.is_a?(String)
17
+
14
18
  parameters = { 'sys.id': id, content_type: content_model }
15
19
 
16
20
  new(ContentfulRedis::Request.new(space, parameters, :get, request_env(env)).call)
@@ -36,14 +40,7 @@ module ContentfulRedis
36
40
  end
37
41
 
38
42
  def destroy(id, env = nil)
39
- keys = []
40
- keys << ContentfulRedis::KeyManager.content_model_key(space, request_env(env), 'sys.id': id, content_type: content_model)
41
-
42
- searchable_fields.each do |field|
43
- keys << ContentfulRedis::KeyManager.attribute_index(self, field)
44
- end
45
-
46
- ContentfulRedis.redis.del(*keys)
43
+ find(id, env).destroy
47
44
  end
48
45
 
49
46
  def space
@@ -72,8 +69,7 @@ module ContentfulRedis
72
69
  end
73
70
 
74
71
  def initialize(model)
75
- instance_variable_set(:@id, model['items'].first.dig('sys', 'id'))
76
- self.class.send(:attr_reader, :id)
72
+ @id = model['items'].first.dig('sys', 'id')
77
73
 
78
74
  entries = entries_as_objects(model)
79
75
 
@@ -87,18 +83,42 @@ module ContentfulRedis
87
83
  value
88
84
  end
89
85
 
90
- instance_variable_set("@#{key.underscore}", value)
86
+ instance_variable_set("@#{key.underscore}", value) unless value.nil?
91
87
  end
92
88
 
93
89
  create_searchable_attribute_links if self.class.searchable_fields.any?
94
90
  end
95
91
 
92
+ def destroy
93
+ keys = [
94
+ ContentfulRedis::KeyManager.content_model_key(
95
+ self.class.space,
96
+ endpoint,
97
+ 'sys.id': id,
98
+ content_type: self.class.content_model,
99
+ include: 1
100
+ )
101
+ ]
102
+
103
+ self.class.send(:searchable_fields).each do |field|
104
+ keys << ContentfulRedis::KeyManager.attribute_index(self.class, send(field.to_sym))
105
+ end
106
+
107
+ ContentfulRedis.redis.del(*keys)
108
+ end
109
+
96
110
  def content_type
97
111
  self.class.name.demodulize.underscore
98
112
  end
99
113
 
100
114
  private
101
115
 
116
+ def endpoint
117
+ env = self.class.send(:request_env, nil)
118
+
119
+ env.to_s.downcase == 'published' ? 'cdn' : 'preview'
120
+ end
121
+
102
122
  def entries_as_objects(model)
103
123
  entries = model.dig('includes', 'Entry')
104
124
 
@@ -108,24 +128,25 @@ module ContentfulRedis
108
128
  type = entry.dig('sys', 'contentType', 'sys', 'id')
109
129
  id = entry.dig('sys', 'id')
110
130
 
111
- hash[id] = ContentfulRedis::ClassFinder.search(type).find(id)
112
- end
131
+ # Catch references to deleted or archived content.
132
+ begin
133
+ hash[id] = ContentfulRedis::ClassFinder.search(type).find(id)
134
+ rescue ContentfulRedis::Error::RecordNotFound => _e
135
+ next
136
+ end
137
+ end.compact
113
138
  end
114
139
 
115
140
  def extract_object_from_hash(model, value, entries)
116
141
  entry_id = value.dig('sys', 'id')
117
142
 
118
143
  assets = model.dig('includes', 'Asset')
119
- asset = if !assets.nil? && assets.is_a?(Array)
120
- model.dig('includes', 'Asset').first
121
- end
144
+ asset = assets.first if !assets.nil? && assets.is_a?(Array)
122
145
 
123
146
  if entries.key?(entry_id)
124
147
  entries[entry_id]
125
148
  elsif !asset.nil?
126
149
  ContentfulRedis::Asset.new(asset)
127
- else
128
- value
129
150
  end
130
151
  end
131
152
 
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DanHenton
8
+ - Edwin Rozario
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2018-10-01 00:00:00.000000000 Z
12
+ date: 2018-11-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: faraday