marfa 0.6.3 → 0.7.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 +4 -4
- data/lib/marfa/cache.rb +11 -11
- data/lib/marfa/helpers/controller.rb +30 -83
- data/lib/marfa/models/api_model.rb +54 -26
- data/lib/marfa/version.rb +1 -1
- data/marfa.gemspec +1 -1
- data/spec/cache_spec.rb +13 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 657f0a9d3d85b0b329080c1cb91e86c5a05de998
|
4
|
+
data.tar.gz: e50e17bdf590634901eef9e5429dfd733dee3f1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13f178284f4405e9a8a06c9e1bbcbc38ce8fa3d7ed32a6468a5e009fdfb4f6c313648bc004d957c9d6515c41f010fa9c791868d6e94b0c4c8a56636fbff22eab
|
7
|
+
data.tar.gz: e2bc6fef84fdc241a1ae5c2670946d8d319ddd56677363791a9a6d8d810ffe7091b8787c50b2b2994f8dbece31601bc6410f6034449ea469e098d9664f77ed42
|
data/lib/marfa/cache.rb
CHANGED
@@ -12,15 +12,15 @@ module Marfa
|
|
12
12
|
# Write data to cache
|
13
13
|
# @example
|
14
14
|
# Marfa.cache.set('key', 'value', 7200)
|
15
|
-
def set(key, value,
|
15
|
+
def set(key, value, time = nil)
|
16
16
|
return unless @config[:enabled]
|
17
|
-
return if
|
18
|
-
|
19
|
-
|
17
|
+
return if time.zero?
|
18
|
+
|
19
|
+
if time.is_a? Numeric
|
20
|
+
@redis.set(key, value, ex: time) # ex - is seconds
|
20
21
|
else
|
21
22
|
@redis.set(key, value) #infinite
|
22
23
|
end
|
23
|
-
|
24
24
|
end
|
25
25
|
|
26
26
|
# Get data from cache
|
@@ -67,18 +67,18 @@ module Marfa
|
|
67
67
|
# @example
|
68
68
|
# Marfa.cache.create_key('block', 'offer/list', ['tag1', 'tag2'])
|
69
69
|
# @return [String] key
|
70
|
-
def create_key(kind, path, tags = [])
|
71
|
-
|
72
|
-
end
|
70
|
+
# def create_key(kind, path, tags = [])
|
71
|
+
# kind + '_' + path.tr('/', '_') + '__' + tags.join('_')
|
72
|
+
# end
|
73
73
|
|
74
74
|
# Create key for json urls
|
75
75
|
# @param path [String] path
|
76
76
|
# @example
|
77
77
|
# Marfa.cache.create_json_key('/get_list.json')
|
78
78
|
# @return [String] key
|
79
|
-
def create_json_key(path)
|
80
|
-
|
81
|
-
end
|
79
|
+
# def create_json_key(path)
|
80
|
+
# path.gsub(%r{[/.]}, '_')
|
81
|
+
# end
|
82
82
|
end
|
83
83
|
|
84
84
|
def self.cache
|
@@ -33,41 +33,32 @@ module Marfa
|
|
33
33
|
# render_page({ path: 'index', tags: ['tag1', 'tag2'], data: {} })
|
34
34
|
# @return [String] rendered content
|
35
35
|
def render_page(options)
|
36
|
-
cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]
|
37
|
-
|
38
|
-
kind = 'page'
|
39
|
-
kind += "-#{@device}" if Marfa.config.cache[:use_device]
|
40
|
-
|
41
36
|
full_path = 'pages/' + options[:path]
|
42
|
-
return render_content(full_path, options[:data]) if
|
37
|
+
return render_content(full_path, options[:data]) if options[:cache_page].blank? || options[:cache_key].blank?
|
43
38
|
|
44
|
-
|
45
|
-
render_cached_content(cache_key, full_path, options[:data])
|
39
|
+
render_cached_content(options[:cache_key], full_path, options[:data])
|
46
40
|
end
|
47
41
|
|
48
42
|
# Render page from cache, store to cache, return html
|
49
|
-
# @param
|
50
|
-
# @param path [String] - URL
|
51
|
-
# @param tags [Array] - tag list
|
43
|
+
# @param cache_key [String] - cache key
|
52
44
|
# @example
|
53
45
|
# get_cached_content('page', 'index/index', ['tag1', 'tag2'])
|
54
46
|
# @return [String] data from cache
|
55
47
|
# @return [Nil]
|
56
|
-
def get_cached_content(
|
57
|
-
cache_key = Marfa.cache.create_key(kind, path, tags)
|
48
|
+
def get_cached_content(cache_key)
|
58
49
|
return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key)
|
59
50
|
nil
|
60
51
|
end
|
61
52
|
|
62
53
|
# convert query json to tags
|
63
54
|
# @param query [Hash] - hash of params
|
64
|
-
# @return [
|
55
|
+
# @return [String]
|
65
56
|
def query_to_tags(query)
|
66
57
|
result = []
|
67
58
|
if query.is_a? Hash
|
68
59
|
query.each { |key, value| result << "#{key}-#{value}" }
|
69
60
|
end
|
70
|
-
result
|
61
|
+
result.join('_')
|
71
62
|
end
|
72
63
|
|
73
64
|
# Render block from cache, return html
|
@@ -76,54 +67,36 @@ module Marfa
|
|
76
67
|
# render_block({ path: 'index/index', tags: ['tag1', 'tag2'] })
|
77
68
|
# @return [String] rendered block
|
78
69
|
def render_block(options)
|
79
|
-
|
80
|
-
cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]
|
81
|
-
tags = options[:tags] || []
|
70
|
+
cache_block = options[:cache_block]
|
82
71
|
|
83
|
-
|
84
|
-
|
85
|
-
tags += query_to_tags(options[:query])
|
86
|
-
|
87
|
-
if cache_time > 0
|
88
|
-
content = get_cached_content(kind, options[:path], tags)
|
72
|
+
if cache_block
|
73
|
+
content = get_cached_content(options[:cache_key])
|
89
74
|
return content unless content.nil?
|
90
75
|
end
|
91
76
|
|
92
|
-
|
93
|
-
return unless Object.const_defined?(
|
77
|
+
model_name = options[:model]
|
78
|
+
return unless Object.const_defined?(model_name)
|
94
79
|
|
95
|
-
|
96
|
-
|
97
|
-
query: options[:query] || {},
|
98
|
-
locals: options[:locals] || {}
|
99
|
-
}
|
80
|
+
model = Object.const_get(model_name)
|
81
|
+
return unless model.respond_to? options[:method].to_sym
|
100
82
|
|
101
|
-
|
102
|
-
data = block.get_data(attrs)
|
83
|
+
data = model.send(options[:method].to_sym, options[:params])
|
103
84
|
data = data.merge(options[:locals]) unless options[:locals].nil?
|
104
85
|
|
105
86
|
full_path = Marfa.config.block_templates_path + '/' + options[:path]
|
106
87
|
|
107
|
-
return render_content(full_path, data)
|
88
|
+
return render_content(full_path, data) unless cache_block
|
108
89
|
|
109
|
-
|
110
|
-
render_cached_content(cache_key, full_path, data)
|
90
|
+
render_cached_content(options[:cache_key], full_path, data)
|
111
91
|
end
|
112
92
|
|
113
93
|
# Render block from cache, return html without class eval
|
114
|
-
# @param
|
115
|
-
# @param data [Hash] - data to render
|
116
|
-
# @example
|
117
|
-
# render_static_block('index/index', ['tag1', 'tag2'])
|
94
|
+
# @param options [Hash] - params
|
118
95
|
# @return [String] rendered block
|
119
|
-
def render_static_block(
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
cache_key = Marfa.cache.create_key('block', path)
|
124
|
-
full_path = Marfa.config.block_templates_path + '/' + path
|
125
|
-
|
126
|
-
render_cached_content(cache_key, full_path, data)
|
96
|
+
def render_static_block(options)
|
97
|
+
return get_cached_content(options[:cache_key]) unless options[:cache_block].blank?
|
98
|
+
full_path = "#{Marfa.config.block_templates_path}/#{options[:path]}"
|
99
|
+
render_cached_content(options[:cache_key], full_path, options[:data])
|
127
100
|
end
|
128
101
|
|
129
102
|
# Generate CSRF token
|
@@ -138,32 +111,12 @@ module Marfa
|
|
138
111
|
Rack::Csrf.csrf_tag(env)
|
139
112
|
end
|
140
113
|
|
141
|
-
# Get HTML from cache or render new
|
142
|
-
# @param options [Hash] - params
|
143
|
-
# @example
|
144
|
-
# get_html({ path: 'index', tags: ['tag1', 'tag2'], data: {} })
|
145
|
-
# @return [String] HTML
|
146
|
-
def get_html(options)
|
147
|
-
cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]
|
148
|
-
|
149
|
-
if cache_time > 0
|
150
|
-
kind = 'page'
|
151
|
-
kind += "-#{@device}" if Marfa.config.cache[:use_device]
|
152
|
-
|
153
|
-
html = get_cached_content(kind, options[:path], options[:tags])
|
154
|
-
html = render_page(options) if html.nil?
|
155
|
-
else
|
156
|
-
html = render_page(options)
|
157
|
-
end
|
158
|
-
html
|
159
|
-
end
|
160
|
-
|
161
114
|
# Render pagination panel
|
162
115
|
# @param data [Hash] - pages info data
|
163
|
-
# @param
|
116
|
+
# @param template [String] - template to render
|
164
117
|
# @return [String] HTML
|
165
|
-
def render_pagination(data,
|
166
|
-
template
|
118
|
+
def render_pagination(data, template = nil)
|
119
|
+
template ||= Marfa.config.pagination_template
|
167
120
|
haml :"#{template}", locals: data
|
168
121
|
end
|
169
122
|
|
@@ -173,16 +126,11 @@ module Marfa
|
|
173
126
|
# render_block_with_data({ path: 'index/index', tags: ['tag1', 'tag2'] })
|
174
127
|
# @return [String] rendered block
|
175
128
|
def render_block_with_data(options)
|
176
|
-
|
177
|
-
cache_time = options[:cache_time] || Marfa.config.cache[:expiration_time]
|
178
|
-
tags = options[:tags] || []
|
179
|
-
|
180
|
-
kind = 'block'
|
181
|
-
kind += "-#{@device}" if Marfa.config.cache[:use_device]
|
182
|
-
tags += query_to_tags(options[:query])
|
129
|
+
cache_block = options[:cache_block]
|
183
130
|
|
184
|
-
|
185
|
-
|
131
|
+
# tags += query_to_tags(options[:query])
|
132
|
+
if cache_block
|
133
|
+
content = get_cached_content(options[:cache_key])
|
186
134
|
return content unless content.nil?
|
187
135
|
end
|
188
136
|
|
@@ -191,10 +139,9 @@ module Marfa
|
|
191
139
|
|
192
140
|
full_path = Marfa.config.block_templates_path + '/' + options[:path]
|
193
141
|
|
194
|
-
return render_content(full_path, data)
|
142
|
+
return render_content(full_path, data) unless cache_block
|
195
143
|
|
196
|
-
|
197
|
-
render_cached_content(cache_key, full_path, data)
|
144
|
+
render_cached_content(options[:cache_key], full_path, data)
|
198
145
|
end
|
199
146
|
|
200
147
|
alias_method :render_component, :render_block
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rest-client'
|
2
2
|
require 'json'
|
3
|
+
require 'marfa/configuration'
|
3
4
|
|
4
5
|
module Marfa
|
5
6
|
# Extend Models
|
@@ -37,56 +38,59 @@ module Marfa
|
|
37
38
|
# self.get_raw_data({ path: 'category/list' })
|
38
39
|
# @return [Hash]
|
39
40
|
def self.get_raw_data(params)
|
40
|
-
params[:query] = params[:query].delete_if { |
|
41
|
-
result = {}
|
41
|
+
params[:query] = params[:query].delete_if { |_, v| v.nil? } unless params[:query].nil?
|
42
42
|
path = params[:path]
|
43
|
-
cache_key =
|
44
|
-
cache_key = params[:cache_key] unless params[:cache_key].nil?
|
43
|
+
cache_key = params[:cache_key]
|
45
44
|
|
46
45
|
begin
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
# don't need cache
|
47
|
+
return _get_request_result(params) if params[:cache_data].blank? || params[:cache_key].blank?
|
48
|
+
|
49
|
+
# cached data exists
|
50
|
+
return JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true) if Marfa.cache.exist?(cache_key)
|
51
|
+
|
52
|
+
# get result and set to cache
|
53
|
+
result = _get_request_result(params)
|
54
|
+
Marfa.cache.set(cache_key, result.to_json, params[:cache_time] || Marfa.config.cache[:expiration_time])
|
55
|
+
|
56
|
+
return result
|
57
|
+
|
54
58
|
rescue => exception
|
55
59
|
if [:development, :test].include? Marfa.config.environment
|
56
60
|
_log_exception(exception, path, params)
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
60
|
-
|
64
|
+
{}
|
61
65
|
end
|
62
66
|
|
63
67
|
# "Raw" data getting w/pagination headers x_count / x_pages
|
64
68
|
# @param params [Hash] - options hash
|
65
69
|
# @return [Hash]
|
66
70
|
def self.get_raw_data_with_pagination(params)
|
67
|
-
params[:query] = params[:query].delete_if { |
|
68
|
-
result = {}
|
71
|
+
params[:query] = params[:query].delete_if { |_, v| v.nil? } unless params[:query].nil?
|
69
72
|
path = params[:path]
|
70
|
-
cache_key =
|
71
|
-
cache_key = params[:cache_key] unless params[:cache_key].nil?
|
73
|
+
cache_key = params[:cache_key]
|
72
74
|
|
73
75
|
begin
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
76
|
+
# don't need cache
|
77
|
+
return _get_request_result_with_headers(params) if params[:cache_data].blank? || params[:cache_key].blank?
|
78
|
+
|
79
|
+
# cached data exists
|
80
|
+
return JSON.parse(Marfa.cache.get(cache_key), symbolize_names: true) if Marfa.cache.exist?(cache_key)
|
81
|
+
|
82
|
+
result = _get_request_result_with_headers(params)
|
83
|
+
Marfa.cache.set(cache_key, result.to_json, params[:cache_time] || Marfa.config.cache[:expiration_time])
|
84
|
+
|
85
|
+
return result
|
86
|
+
|
83
87
|
rescue => exception
|
84
88
|
if [:development, :test].include? Marfa.config.environment
|
85
89
|
_log_exception(exception, path, params)
|
86
90
|
end
|
87
91
|
end
|
88
92
|
|
89
|
-
|
93
|
+
{}
|
90
94
|
end
|
91
95
|
|
92
96
|
# Log exceptions to console
|
@@ -98,6 +102,30 @@ module Marfa
|
|
98
102
|
p "#{e.message} (#{e.class})"
|
99
103
|
p '=================================================='
|
100
104
|
end
|
105
|
+
|
106
|
+
# request result without headers
|
107
|
+
# @return [Hash]
|
108
|
+
def self._get_request_result(params)
|
109
|
+
response = Rest.get("#{Marfa.config.api_server}#{params[:path]}", { params: params[:query], headers: {} })
|
110
|
+
JSON.parse(response.body, symbolize_names: true)
|
111
|
+
end
|
112
|
+
|
113
|
+
# request result with headers
|
114
|
+
# @return [Hash]
|
115
|
+
def self._get_request_result_with_headers(params)
|
116
|
+
result = {}
|
117
|
+
|
118
|
+
thread = Thread.new do
|
119
|
+
response = Rest.get("#{Marfa.config.api_server}#{params[:path]}", { params: params[:query], headers: {} })
|
120
|
+
result[:data] = JSON.parse(response.body, symbolize_names: true)
|
121
|
+
result[:data_count] = response.headers[:x_count].to_i unless response.headers[:x_count].nil?
|
122
|
+
result[:data_pages] = response.headers[:x_pages].to_i unless response.headers[:x_pages].nil?
|
123
|
+
end
|
124
|
+
|
125
|
+
thread.join
|
126
|
+
|
127
|
+
result
|
128
|
+
end
|
101
129
|
end
|
102
130
|
end
|
103
131
|
end
|
data/lib/marfa/version.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Version constant
|
2
2
|
module Marfa
|
3
3
|
# The version constant for the current version of Marfa
|
4
|
-
VERSION = '0.
|
4
|
+
VERSION = '0.7.0'.freeze unless defined?(Marfa::VERSION)
|
5
5
|
|
6
6
|
# The current Marfa version.
|
7
7
|
# @return [String] The version number
|
data/marfa.gemspec
CHANGED
data/spec/cache_spec.rb
CHANGED
@@ -34,17 +34,17 @@ describe Marfa::Cache do
|
|
34
34
|
expect(@cache.exist?(@key)).to eql false
|
35
35
|
end
|
36
36
|
|
37
|
-
it 'checks_create_cache_key' do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'checks_create_json_cache_key' do
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
37
|
+
# it 'checks_create_cache_key' do
|
38
|
+
# kind = 'block'
|
39
|
+
# path = 'offer/list'
|
40
|
+
# tags = %w(offers cities)
|
41
|
+
# cache_key = @cache.create_key(kind, path, tags)
|
42
|
+
# expect(cache_key.length).to satisfy { |len| len > 20 }
|
43
|
+
# end
|
44
|
+
|
45
|
+
# it 'checks_create_json_cache_key' do
|
46
|
+
# path = 'offer/list.json'
|
47
|
+
# cache_key = @cache.create_json_key(path)
|
48
|
+
# expect(cache_key.length).to eql path.length
|
49
|
+
# end
|
50
50
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marfa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Krechetov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-
|
13
|
+
date: 2017-10-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: haml
|