editmode 0.0.10.1 → 0.0.10.17

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
  SHA256:
3
- metadata.gz: 1dc05bbec2b009d428193136bed21d4691a02b7aa21ce14d791d0b903cc4776d
4
- data.tar.gz: f3eccd293ed87c3f3b6755ff87bb129cdf9a202fb205d453dd968d23b97f527c
3
+ metadata.gz: 84e04c69c8b419dd68ae049388bdb37b578e6294b6cc28eda7cd9abd2c81ee08
4
+ data.tar.gz: a35062691fc6cfbf10b00090cd625e93fad33c73f875ebf89b4dd2280fb23ab4
5
5
  SHA512:
6
- metadata.gz: 9935316b3dc3bbd2f170f1797c7b04dc1b83f23e1161ecc77a07fde824fae2fc1f0cf57742d8942fc6da1d831dc96ed1703ebfbd7dff886f0838a73adc26d7b3
7
- data.tar.gz: d52d827d63a9db3046072c62add60d934b30833374593db1e97706b729b1b4b20cc34c17cd064d988c985df94cdac4a416451486820e11e824a0950e2359effe
6
+ metadata.gz: efd6cafe879df92901fd55b82f399a5e04b24a134c79a4156d5af93d34f202b7a63956b5c16a308effe9fd7994dd827f9cbf793c7cf43da0dcf79d069ce7cf69
7
+ data.tar.gz: ce0fd13d933885a3091dc08f81e7aef749ebf1b7ef9d3345332bd6e73775cef2c962eec7defdf4f6ce76774c05e5bdf8d5533a2b2e805ca5cce0fbeb4abd347b
@@ -4,6 +4,10 @@ class EditmodeController < ApplicationController
4
4
  if params[:full]
5
5
  Rails.cache.clear
6
6
  render status: 200, json: {:response => "success"}
7
+ elsif params[:variable_cache_project_id]
8
+ project_id = params[:variable_cache_project_id]
9
+ Rails.cache.delete("chunk_#{project_id}_variables")
10
+ render status: 200, json: {:response => "success"}
7
11
  elsif params[:identifier]
8
12
  Rails.cache.delete("chunk_#{params[:identifier]}")
9
13
  Rails.cache.delete("chunk_#{params[:identifier]}_type")
@@ -3,12 +3,14 @@ require "editmode/version"
3
3
  require 'editmode/script_tag'
4
4
  require 'editmode/action_view_extensions/editmode_helper'
5
5
  require 'editmode/auto_include_filter'
6
+ require 'editmode/chunk_value'
6
7
  require 'editmode/railtie' if defined? Rails
7
8
  require 'editmode/engine' if defined?(Rails)
8
-
9
+ # Todo: Implement RSPEC
9
10
  module Editmode
10
11
  class << self
11
12
  include Editmode::ActionViewExtensions::EditmodeHelper
13
+
12
14
  def project_id=(id)
13
15
  config.project_id = id
14
16
  end
@@ -22,8 +24,6 @@ module Editmode
22
24
  end
23
25
 
24
26
  def config
25
- # Todo: Instantiate in editmode initializer or base controllers
26
- # Todo: Add a generator to create initializer an file?
27
27
  @config ||= Configuration.new
28
28
  end
29
29
 
@@ -32,36 +32,21 @@ module Editmode
32
32
  end
33
33
 
34
34
  def chunk_value(identifier, **options)
35
- body = options[:values].presence || {}
36
- field_id = options[:field_id].presence
37
- branch_id = options[:branch_id].presence
38
-
39
- branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
40
- cache_identifier = "chunk_#{identifier}#{branch_id}"
41
- url = "#{api_root_url}/chunks/#{identifier}?#{branch_params}"
42
-
43
35
  begin
44
- response = HTTParty.get(url, query: body)
45
- response_received = true if response.code == 200
46
-
47
- if !response_received
48
- raise "No response received"
49
- else
50
- if field_id.present?
51
- chunk = response["content"].detect {|f| f["custom_field_identifier"] == field_id }
52
- chunk['content']
53
- else
54
- response['content']
55
- end
56
- end
57
- rescue => error
58
- # Todo: Send a log to editmode prob like sentry
59
- return "No response received"
36
+ Editmode::ChunkValue.new(identifier, **options )
37
+ rescue => er
38
+ raise er
60
39
  end
61
40
  end
62
41
  end
63
42
 
64
43
  class Configuration
65
- attr_accessor :project_id, :access_token
44
+ attr_accessor :access_token, :variable
45
+ attr_reader :project_id
46
+
47
+ def project_id=(id)
48
+ @project_id = id
49
+
50
+ end
66
51
  end
67
52
  end
@@ -12,13 +12,29 @@ module Editmode
12
12
  ENV["EDITMODE_OVERRIDE_API_URL"] || "https://api.editmode.com"
13
13
  end
14
14
 
15
- def chunk_collection(collection_identifier,has_tags=[])
15
+ def chunk_collection(collection_identifier, **options)
16
16
  branch_params = params[:em_branch_id].present? ? "branch_id=#{params[:em_branch_id]}" : ""
17
+ branch_id = params[:em_branch_id].presence
18
+ tags = options[:tags].presence || []
19
+ limit = options[:limit].presence
20
+
17
21
  begin
18
- url = "#{api_root_url}/chunks?collection_identifier=#{collection_identifier}&#{branch_params}"
22
+ url_params = {
23
+ :collection_identifier => collection_identifier,
24
+ :branch_id => branch_id,
25
+ :limit => limit,
26
+ :tags => tags
27
+ }.to_query
28
+
29
+ url = URI(api_root_url)
30
+ url.path = '/chunks'
31
+ url.query = url_params
32
+
19
33
  response = HTTParty.get(url)
34
+
20
35
  raise "No response received" unless response.code == 200
21
36
  chunks = response["chunks"]
37
+
22
38
  return chunks
23
39
  rescue => error
24
40
  puts error
@@ -53,7 +69,7 @@ module Editmode
53
69
  # Always sanitize the content!!
54
70
  chunk_content = ActionController::Base.helpers.sanitize(chunk_content)
55
71
 
56
- css_class = options[:css_class]
72
+ css_class = options[:class]
57
73
 
58
74
  if chunk_type == "image"
59
75
  display_type = "image"
@@ -133,6 +149,28 @@ module Editmode
133
149
 
134
150
  alias_method :chunk, :chunk_display
135
151
 
152
+ def variable_parse!(content, variables, values)
153
+ tokens = content.scan(/\{{(.*?)\}}/)
154
+ if tokens.any?
155
+ tokens.flatten!
156
+ tokens.each do |token|
157
+ token_value = values[token.to_sym] || variables[token] || ""
158
+ sanitized_value = ActionController::Base.helpers.sanitize(token_value)
159
+
160
+ content.gsub!("{{#{token}}}", sanitized_value)
161
+ end
162
+ end
163
+
164
+ content
165
+ end
166
+
167
+ def no_response_received(id = "")
168
+ "Sorry, we can't find a chunk using this identifier: \"#{id}\". This can happen if you've deleted a chunk on editmode.com or if your local cache is out of date. If it persists, try running Rails.cache clear."
169
+ end
170
+
171
+ def require_field_id
172
+ "Field ID or Field Name is required to retrieve a collection item"
173
+ end
136
174
  end
137
175
  end
138
176
  end
@@ -0,0 +1,67 @@
1
+ module Editmode
2
+ class ChunkValue
3
+ include Editmode::ActionViewExtensions::EditmodeHelper
4
+
5
+ attr_accessor :identifier, :variable_values, :branch_id,
6
+ :variable_fallbacks, :chunk_type, :project_id,
7
+ :response, :content
8
+
9
+ def initialize(identifier, **options)
10
+ @identifier = identifier
11
+ @branch_id = options[:branch_id].presence
12
+ @variable_values = options[:values].presence || {}
13
+ get_content
14
+ end
15
+
16
+ def field(field = nil)
17
+ # Field ID can be a slug or field_name
18
+ if chunk_type == 'collection_item'
19
+ if field.present?
20
+ field.downcase!
21
+ field_content = content.detect {|f| f["custom_field_identifier"].downcase == field || f["custom_field_name"].downcase == field }
22
+ if field_content.present?
23
+ result = field_content['content']
24
+ result = variable_parse!(result, variable_fallbacks, variable_values)
25
+ else
26
+ raise no_response_received(field)
27
+ end
28
+ else
29
+ raise require_field_id
30
+ end
31
+ else
32
+ raise "undefined method field for chunk_type: #{chunk_type}"
33
+ end
34
+ result ||= content
35
+ end
36
+
37
+ private
38
+ def get_content
39
+ branch_params = branch_id.present? ? "branch_id=#{branch_id}" : ""
40
+ url = "#{api_root_url}/chunks/#{identifier}?#{branch_params}"
41
+
42
+ cache_identifier = "chunk_value_#{identifier}#{branch_id}"
43
+ cached_content_present = Rails.cache.exist?(cache_identifier)
44
+
45
+ if !cached_content_present
46
+ http_response = HTTParty.get(url)
47
+ response_received = true if http_response.code == 200
48
+ end
49
+
50
+ if !cached_content_present && !response_received
51
+ raise no_response_received(identifier)
52
+ else
53
+ @response = Rails.cache.fetch(cache_identifier) do
54
+ http_response
55
+ end
56
+
57
+ @content = response['content']
58
+ @chunk_type = response['chunk_type']
59
+ @project_id = response['project_id']
60
+ @variable_fallbacks = Rails.cache.fetch("chunk_#{project_id}_variables") do
61
+ response['variable_fallbacks']
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module Editmode
2
- VERSION = "0.0.10.1"
2
+ VERSION = "0.0.10.17"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: editmode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10.1
4
+ version: 0.0.10.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Ennis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-21 00:00:00.000000000 Z
11
+ date: 2020-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,7 @@ files:
82
82
  - lib/editmode.rb
83
83
  - lib/editmode/action_view_extensions/editmode_helper.rb
84
84
  - lib/editmode/auto_include_filter.rb
85
+ - lib/editmode/chunk_value.rb
85
86
  - lib/editmode/engine.rb
86
87
  - lib/editmode/railtie.rb
87
88
  - lib/editmode/script_tag.rb