marfa 0.0.3.9

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 710e3c673087c7b47f6a4e00fba5e4f74821fcb0
4
+ data.tar.gz: 049645723de6b540d9026704aa34575219a63b95
5
+ SHA512:
6
+ metadata.gz: b5dba35208281137476e36436f13b0c288202bffe0405a59803a7f94bf0cf1342800ed1dbd748aab3321365bd494b0d476f1ae54e294bc0d0ce545f901b4a050
7
+ data.tar.gz: 39ff0ce885d00eb46ac056d21ad61e9c33f794bca78e9c0f9d2b97a4ef68efa23c30795ef9b79f4ad33e4e32893feecc823370b2aedf6386ae4c2a493539ee24
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .idea/
2
+ Gemfile.lock
3
+ .yardoc/
4
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,125 @@
1
+ require 'fileutils'
2
+ require 'rake'
3
+
4
+ include FileUtils
5
+
6
+ # Default marfa configuration
7
+ def create_marfa_config_file
8
+ open('config/marfa.rb', 'w') do |file|
9
+ file.puts '# Marfa configuration file'
10
+ file.puts ''
11
+ file.puts '# Specifying API Server is needed'
12
+ file.puts "Marfa.config.api_server = ''"
13
+ file.puts ''
14
+ file.puts '# Views path is needed'
15
+ file.puts "Marfa.config.views = File.expand_path('./app/views')"
16
+ file.puts ''
17
+ file.puts '# Cache config'
18
+ file.puts "Marfa.config.cache = {"
19
+ file.puts ' enabled: false,'
20
+ file.puts " host: '',"
21
+ file.puts ' port: 0,'
22
+ file.puts ' db: 0,'
23
+ file.puts ' expiration_time: 3600'
24
+ file.puts '}'
25
+ file.puts ''
26
+ file.puts '# Static files content path'
27
+ file.puts "Marfa.config.content_path = '/images/content/'"
28
+ file.puts ''
29
+ file.puts '# Public folder'
30
+ file.puts "Marfa.config.public_folder = File.expand_path('./static')"
31
+ file.puts ''
32
+ file.puts '# Static files cache'
33
+ file.puts "Marfa.config.static_cache_control = [:public, :max_age => 2_592_000]"
34
+ file.puts ''
35
+ file.puts '# CSRF Protection'
36
+ file.puts 'Marfa.config.csrf_enabled = false'
37
+ file.puts ''
38
+ file.puts '# HTML Compression'
39
+ file.puts 'Marfa.config.compression_enabled = false'
40
+ file.puts ''
41
+ file.puts '# CSS Minifying'
42
+ file.puts 'Marfa.config.minify_css = true'
43
+ file.puts ''
44
+ file.puts '# JS Minifying'
45
+ file.puts 'Marfa.config.minify_js = true'
46
+ end
47
+ end
48
+
49
+ def create_application_config_file
50
+ open('config/application.rb', 'w') do |file|
51
+ # file.puts "require 'marfa'"
52
+ # file.puts ''
53
+ end
54
+ end
55
+
56
+ # Default DTO file
57
+ def create_base_dto_file
58
+ open('app/models/dto.rb', 'w') do |file|
59
+ file.puts "require 'marfa'"
60
+ file.puts ''
61
+ file.puts 'class DTO < Marfa::Models::BaseDTO'
62
+ file.puts 'end'
63
+ end
64
+ end
65
+
66
+ # Rackup file
67
+ def create_rackup_config_file
68
+ open('config.ru', 'w') do |file|
69
+ file.puts "require 'marfa'"
70
+ file.puts "require File.dirname(__FILE__) + '/app/bootstrap'"
71
+ file.puts "require File.dirname(__FILE__) + '/config/marfa'"
72
+ file.puts ''
73
+ file.puts 'Marfa.configure_app'
74
+ file.puts ''
75
+ file.puts '# Controllers auto-bootstrap'
76
+ file.puts "controllers = Object.constants.select { |c| c.to_s.include? 'Controller' }"
77
+ file.puts 'controllers.map! { |controller| Object.const_get(controller) }'
78
+ file.puts 'controllers += Marfa::Controllers.controllers_list'
79
+ file.puts 'run Rack::Cascade.new(controllers)'
80
+ end
81
+ end
82
+
83
+ def create_bootstrap_file
84
+ open('app/bootstrap.rb', 'w') do |file|
85
+ file.puts "require File.dirname(__FILE__) + '/models/dto'"
86
+ file.puts ''
87
+ file.puts '# requiring all blocks and controllers'
88
+ file.puts "Dir[File.dirname(__FILE__) + '/blocks/**/*.rb'].each { |file| require file }"
89
+ file.puts "Dir[File.dirname(__FILE__) + '/controllers/**/*.rb'].each { |file| require file }"
90
+ end
91
+ end
92
+
93
+ task :default do
94
+ puts 'Please specify command'
95
+ end
96
+
97
+ task :start, [:home_path, :project_dir] do |t, args|
98
+ project_dir = args[:project_dir]
99
+
100
+ puts "Starting project #{project_dir}"
101
+ cd args[:home_path], verbose: true
102
+
103
+ unless Dir.exist?(project_dir)
104
+ puts 'Creating project folder...'
105
+
106
+ mkdir_p(project_dir)
107
+ cd project_dir, verbose: true
108
+
109
+ puts 'Creating project structure...'
110
+ mkdir_p(%w(app config static app/blocks app/controllers app/models app/views))
111
+ puts 'Project structure are created'
112
+
113
+ puts 'Creating dummy model files...'
114
+ create_base_dto_file
115
+ puts 'Dummy model files are created'
116
+
117
+ puts 'Creating config files...'
118
+ create_marfa_config_file
119
+ create_application_config_file
120
+ create_bootstrap_file
121
+ create_rackup_config_file
122
+ puts 'Config files are created'
123
+
124
+ end
125
+ end
data/bin/marfa ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+
4
+ if ARGV.length > 1
5
+ command = ARGV[0]
6
+ rake_path = File.expand_path('../../Rakefile', __FILE__)
7
+ system "rake -f #{rake_path} #{command}[#{FileUtils.pwd},#{ARGV[1]}]"
8
+
9
+ if $?.exitstatus > 0
10
+ puts 'Failed'
11
+ end
12
+
13
+ else
14
+ puts 'Not enough parameters'
15
+ end
@@ -0,0 +1,13 @@
1
+ # Extending Marfa
2
+ module Marfa
3
+ # Extending blocks module
4
+ module Blocks
5
+ # Base Block
6
+ class BaseBlock
7
+ # This method must return data from model
8
+ def get_data(params)
9
+
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,83 @@
1
+ require 'redis'
2
+
3
+ # Redis-cache wrapper
4
+ module Marfa
5
+ # Redis-cache wrapper
6
+ class Cache
7
+ def initialize
8
+ @config = Marfa.config.cache
9
+ @redis = Redis.new(host: @config[:host], port: @config[:port], db: @config[:db])
10
+ end
11
+
12
+ # Write data to cache
13
+ # @example
14
+ # Marfa.cache.set('key', 'value', 7200)
15
+ def set(key, value, _time = @config[:expiration_time])
16
+ return unless @config[:enabled]
17
+ @redis.set(key, value)
18
+ end
19
+
20
+ # Get data from cache
21
+ # @param key [String] cache key
22
+ # @example
23
+ # Marfa.cache.get('key')
24
+ # @return [String] data from cache
25
+ # @return [Nil]
26
+ def get(key)
27
+ @redis.get(key)
28
+ end
29
+
30
+ # Check that key exist in cache
31
+ # @param key [String] ключ
32
+ # @example
33
+ # Marfa.cache.exist?('key')
34
+ # @return [Boolean]
35
+ def exist?(key)
36
+ return unless @config[:enabled]
37
+ @redis.exists(key)
38
+ end
39
+
40
+ # Delete data from cache
41
+ # @param key [String] cache key
42
+ # @example
43
+ # Marfa.cache.delete('key')
44
+ def delete(key)
45
+ @redis.del(key)
46
+ end
47
+
48
+ # Delete data from cache by pattern
49
+ # @param pattern [String] pattern
50
+ # @example
51
+ # Marfa.cache.delete_by_pattern('pattern')
52
+ def delete_by_pattern(pattern)
53
+ keys = @redis.keys("*#{pattern}*")
54
+ @redis.del(*keys) unless keys.empty?
55
+ end
56
+
57
+ # Create key by params
58
+ # @param kind [String] kind (block or page)
59
+ # @param path [String] path
60
+ # @param tags [Array] tag list
61
+ # @example
62
+ # Marfa.cache.create_key('block', 'offer/list', ['tag1', 'tag2'])
63
+ # @return [String] key
64
+ def create_key(kind, path, tags)
65
+ kind + '_' + path.tr('/', '_') + '__' + tags.join('_')
66
+ end
67
+
68
+ # Create key for json urls
69
+ # @param path [String] path
70
+ # @example
71
+ # Marfa.cache.create_json_key('/get_list.json')
72
+ # @return [String] key
73
+ def create_json_key(path)
74
+ path.gsub(%r{[/.]}, '_')
75
+ end
76
+ end
77
+
78
+ def self.cache
79
+ @cache ||= Marfa::Cache.new
80
+ end
81
+
82
+ end
83
+
@@ -0,0 +1,48 @@
1
+ require 'ostruct'
2
+ require 'marfa/controllers/base_controller'
3
+ require 'marfa/controllers/css_controller'
4
+ require 'htmlcompressor'
5
+
6
+ module Marfa
7
+ # Configuration
8
+ def self.config
9
+ @config ||= OpenStruct.new
10
+ end
11
+
12
+ # Configure BaseController - configure Sinatra
13
+ def self.configure_app
14
+ return if @config.to_h.empty?
15
+
16
+ _configure_settings(Marfa::Controllers::BaseController)
17
+ _configure_settings(Marfa::Controllers::CssController)
18
+ _configure_ext_modules(Marfa::Controllers::BaseController)
19
+ end
20
+
21
+ private
22
+
23
+ # Configure controller settings
24
+ def self._configure_settings(app)
25
+ _default_settings
26
+
27
+ @_default_settings.each do |setting|
28
+ app.set setting.to_sym, Marfa.config[setting] unless Marfa.config[setting].nil?
29
+ end
30
+ end
31
+
32
+ # Configure extending modules
33
+ def self._configure_ext_modules(app)
34
+ app.configure do
35
+ app.use Rack::Csrf, raise: true if Marfa.config.csrf_enabled
36
+ app.use HtmlCompressor::Rack if Marfa.config.compression_enabled
37
+ end
38
+ end
39
+
40
+ # Default settings fields for apps
41
+ def self._default_settings
42
+ @_default_settings = [
43
+ 'public_folder',
44
+ 'static_cache_control',
45
+ 'views'
46
+ ]
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ require 'haml'
2
+ require 'rack/csrf'
3
+ require 'device_detector'
4
+ require 'marfa/helpers/controller'
5
+ require 'marfa/helpers/http/vary'
6
+ require 'marfa/helpers/javascript'
7
+
8
+ # Extending Marfa
9
+ module Marfa
10
+ # Extending Controllers
11
+ module Controllers
12
+ # base controller
13
+ class BaseController < Sinatra::Base
14
+ before do
15
+ @device = DeviceDetector.new(request.user_agent).device_type
16
+ end
17
+
18
+ # Not Found page
19
+ not_found do
20
+ status 404
21
+ haml :not_found
22
+ end
23
+
24
+ # All methods defined below might be used in child controllers
25
+ helpers Marfa::Helpers::Controller
26
+ helpers Marfa::Helpers::HTTP::Vary
27
+ helpers Marfa::Helpers::Javascript
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'base_controller'
2
+ require 'marfa/helpers/scss'
3
+ require 'marfa/helpers/style'
4
+
5
+ module Marfa
6
+ module Controllers
7
+ class CssController < BaseController
8
+ helpers Marfa::Helpers::Scss
9
+ include Marfa::Helpers::Style
10
+
11
+ get '/css/main.:device.css' do |device|
12
+ render_style({ section: 'main', device: device })
13
+ end
14
+
15
+ get '/css/:section.:range.:device.css' do |section, range, device|
16
+ render_style({ root_path: '/pages/', section: section, range: range, device: device })
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'marfa/controllers/base_controller'
2
+ require 'marfa/controllers/css_controller'
3
+
4
+ module Marfa
5
+ module Controllers
6
+ # Controllers for Rack run
7
+ # @return [Array] Controllers list
8
+ def self.controllers_list
9
+ @controllers_list = [
10
+ CssController
11
+ ]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,41 @@
1
+ # Additional String functionality
2
+ class String
3
+ # Replaces all '/' to '_'
4
+ # @example
5
+ # "some/path".to_underscore
6
+ # @return [String] changed string
7
+ def to_underscore
8
+ downcase.gsub(%r{/}, '_')
9
+ end
10
+
11
+ # Convert string like 'category/list' to CamelCase
12
+ # @example
13
+ # "some/path".to_class_name
14
+ # @return [String] changed string
15
+ def to_class_name
16
+ parts = downcase.split('/')
17
+ parts.each(&:capitalize!)
18
+ parts.join('')
19
+ end
20
+
21
+ # Convert string to url part
22
+ # @example
23
+ # "some/path".to_url
24
+ # @return [String] changed string
25
+ def to_url
26
+ val = self.strip_tags!
27
+ val = val.gsub(':', '')
28
+ val = val.gsub(' ', '-')
29
+ val = val.gsub('/', '-')
30
+ val.downcase
31
+ end
32
+
33
+ # Remove tags
34
+ # @example
35
+ # "<a>some/path</a>".strip_tags!
36
+ # @return [String] changed string
37
+ def strip_tags!
38
+ self.gsub(/<\/?[^>]*>/, '') # unless self.nil?
39
+ end
40
+
41
+ end
@@ -0,0 +1,126 @@
1
+ module Marfa
2
+ module Helpers
3
+ module Controller
4
+ # Rendering cached content
5
+ # @param cache_key [String] key
6
+ # @param path [String] - URL
7
+ # @param data [Hash] - options hash
8
+ # @example
9
+ # render_cached_content('some_key', 'path/url', {})
10
+ # @return [String] rendered content
11
+ def render_cached_content(cache_key, path, data = {})
12
+ return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key)
13
+ output = haml :"#{path}", locals: data
14
+ Marfa.cache.set(cache_key, output)
15
+ output
16
+ end
17
+
18
+ # Render page from cache, return html
19
+ # @param path [String] - URL
20
+ # @param tags [Array] - tag list
21
+ # @param data [Hash] - options hash
22
+ # @example
23
+ # render_page('index', ['tag1', 'tag2'], {})
24
+ # @return [String] rendered content
25
+ def render_page(path, tags, data)
26
+ cache_key = Marfa.cache.create_key('page', path, tags)
27
+ full_path = 'pages/' + path
28
+ render_cached_content(cache_key, full_path, data)
29
+ end
30
+
31
+ # Render page from cache, store to cache, return html
32
+ # @param kind [String] - kind (block, page)
33
+ # @param path [String] - URL
34
+ # @param tags [Array] - tag list
35
+ # @example
36
+ # get_cached_content('page', 'index/index', ['tag1', 'tag2'])
37
+ # @return [String] data from cache
38
+ # @return [Nil]
39
+ def get_cached_content(kind, path, tags)
40
+ cache_key = Marfa.cache.create_key(kind, path, tags)
41
+ return Marfa.cache.get(cache_key) if Marfa.cache.exist?(cache_key)
42
+ end
43
+
44
+ # Render block from cache, return html
45
+ # @param path [String] - URL
46
+ # @param tags [Array] - tag list
47
+ # @example
48
+ # render_block('index/index', ['tag1', 'tag2'])
49
+ # @return [String] rendered block
50
+ def render_component(path, tags = [])
51
+
52
+ # TODO: Improve caching with parameters
53
+ content = get_cached_content('block', path, tags)
54
+ return content unless content.nil?
55
+
56
+ classname = path.to_class_name + 'Block'
57
+ return unless Object.const_defined?(classname)
58
+
59
+ attrs = {
60
+ user_data: @user_data || {},
61
+ query: params.to_h
62
+ }
63
+
64
+ block = Object.const_get(classname).new
65
+ data = block.get_data(attrs)
66
+ cache_key = Marfa.cache.create_key('block', path, tags)
67
+ full_path = 'components/' + path
68
+
69
+ render_cached_content(cache_key, full_path, data)
70
+ end
71
+
72
+ # Render block from cache, return html
73
+ # DEPRECATED
74
+ # @param path [String] - URL
75
+ # @param tags [Array] - tag list
76
+ # @example
77
+ # render_block('index/index', ['tag1', 'tag2'])
78
+ # @return [String] rendered block
79
+ def render_block(path, tags)
80
+ # TODO: Improve caching with parameters
81
+ content = get_cached_content('block', path, tags)
82
+ return content unless content.nil?
83
+
84
+ classname = path.to_class_name + 'Block'
85
+ return unless Object.const_defined?(classname)
86
+
87
+ attrs = {
88
+ user_data: @user_data || {},
89
+ query: params.to_h
90
+ }
91
+
92
+ block = Object.const_get(classname).new
93
+ data = block.get_data(attrs)
94
+ cache_key = Marfa.cache.create_key('block', path, tags)
95
+ full_path = 'blocks/' + path
96
+
97
+ render_cached_content(cache_key, full_path, data)
98
+ end
99
+
100
+ # Generate CSRF token
101
+ # @return [String] CSRF token
102
+ def csrf_token
103
+ Rack::Csrf.csrf_token(env)
104
+ end
105
+
106
+ # CSRF-tag
107
+ # @return [String] CSRF tag
108
+ def csrf_tag
109
+ Rack::Csrf.csrf_tag(env)
110
+ end
111
+
112
+ # Get HTML from cache or render new
113
+ # @param path [String] - URL
114
+ # @param tags [Array] - tag list
115
+ # @param data [Hash] - data to render
116
+ # @example
117
+ # get_html('index/index', ['tag1', 'tag2'], {})
118
+ # @return [String] HTML
119
+ def get_html(path, tags, data)
120
+ html = get_cached_content('page', path, tags)
121
+ html = render_page(path, tags, data) if html.nil?
122
+ html
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,46 @@
1
+ # see https://github.com/philipp-kempgen/sinatra-helpers-http-vary
2
+ # see Marfa
3
+ module Marfa
4
+ # see Marfa
5
+ module Helpers
6
+ # HTTP helpers
7
+ module HTTP
8
+ # Helpers to set the HTTP "`Vary`" header.
9
+ module Vary
10
+ VARY_HEADER = 'Vary'.freeze
11
+ VARY_UNSPECIFIED = '*'.freeze
12
+
13
+ # Sets the HTTP "`Vary`" header in Sinatra's response
14
+ # `headers`.
15
+ #
16
+ # @param hdr_name [String] The HTTP header name.
17
+ # @return The updated `headers`.
18
+ #
19
+ def vary!(hdr_name)
20
+ hdr_name = hdr_name.to_s if !hdr_name.kind_of?(::String)
21
+
22
+ # Shortcut to avoid expensive splitting in the
23
+ # simple "*" case:
24
+ if hdr_name == VARY_UNSPECIFIED
25
+ headers[VARY_HEADER] = VARY_UNSPECIFIED.dup unless (headers[VARY_HEADER] == VARY_UNSPECIFIED)
26
+ # Normal operation:
27
+ else
28
+ vary_hdrs = headers[VARY_HEADER].to_s.split(/\s*(?:,\s*)+/)
29
+
30
+ if vary_hdrs.include?(VARY_UNSPECIFIED)
31
+ headers[VARY_HEADER] = VARY_UNSPECIFIED.dup
32
+ else
33
+ headers[VARY_HEADER] =
34
+ vary_hdrs
35
+ .push(hdr_name)
36
+ .uniq(& :downcase)
37
+ .join(',')
38
+ end
39
+ end
40
+
41
+ headers[VARY_HEADER]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,44 @@
1
+ require 'babel/transpiler'
2
+ require 'closure-compiler'
3
+
4
+ # Javascript
5
+ module Marfa
6
+ # Helpers for Javascript
7
+ module Helpers
8
+ # Javascript module
9
+ module Javascript
10
+ def js_transpile(path, is_plain_text = true)
11
+ path = File.read(path) unless is_plain_text
12
+ result = Babel::Transpiler.transform(path)
13
+ result['code']
14
+ end
15
+
16
+ # https://developers.google.com/closure/compiler/docs/compilation_levels
17
+ def js_import(path)
18
+ path = settings.views + path
19
+
20
+ closure = Closure::Compiler.new(
21
+ compilation_level: 'SIMPLE_OPTIMIZATIONS',
22
+ language_out: 'ES5_STRICT'
23
+ )
24
+
25
+ code = js_transpile(path, false)
26
+ code = closure.compile(code) if Marfa.config.minify_js
27
+ '<script>' + code + '</script>'
28
+ end
29
+
30
+ # https://developers.google.com/closure/compiler/docs/compilation_levels
31
+ def js_import_from_haml(path)
32
+ closure = Closure::Compiler.new(
33
+ compilation_level: 'SIMPLE_OPTIMIZATIONS',
34
+ language_out: 'ES5_STRICT'
35
+ )
36
+
37
+ template = haml :"#{path}", :layout => false
38
+ code = js_transpile(template)
39
+ code = closure.compile(code)
40
+ '<script>' + code + '</script>'
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ require 'sass'
2
+ require 'sass/plugin'
3
+
4
+ # Sass module extension
5
+ module Sass::Script::Functions
6
+ # SASS-method - device type
7
+ def device
8
+ Sass::Script::Value::String.new(Sass::Plugin.options[:custom][:device])
9
+ end
10
+
11
+ # SASS-method - section
12
+ def section
13
+ Sass::Script::Value::String.new(Sass::Plugin.options[:custom][:section])
14
+ end
15
+
16
+ # SASS-method - content path
17
+ def content_path
18
+ Sass::Script::Value::String.new(Sass::Plugin.options[:custom][:contentPath])
19
+ end
20
+
21
+ declare :get_device, []
22
+ declare :section, []
23
+ declare :content_path, []
24
+ end
@@ -0,0 +1,18 @@
1
+ # Sinatra SCSS customization
2
+ module Marfa
3
+ # Helpers for SCSS customization
4
+ module Helpers
5
+ # SCSS module
6
+ module Scss
7
+ # Overwrite the find_template method
8
+ def find_template(views, name, engine, &block)
9
+ Array(views).each { |v| super(v, name, engine, &block) }
10
+ end
11
+
12
+ # def scss(template, options = {}, locals = {})
13
+ # template = :"#{settings.views}/#{template}" if template.is_a? Symbol
14
+ # super(template, options, locals)
15
+ # end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,71 @@
1
+ require 'sass/plugin'
2
+ require 'csso'
3
+
4
+ # Rendering and caching style
5
+ module Marfa
6
+ module Helpers
7
+ module Style
8
+ # Pass dynamic vars to sass-files
9
+ # @param device [String] - device type
10
+ # @param section [String] - section
11
+ def dynamic_vars(device, section = 'root')
12
+ Sass::Plugin.options[:custom] ||= {}
13
+ Sass::Plugin.options[:custom][:device] = device
14
+ Sass::Plugin.options[:custom][:section] = section
15
+ Sass::Plugin.options[:custom][:contentPath] = Marfa.config.content_path
16
+ end
17
+
18
+ # Rendering style
19
+ # @param [Hash] options - options
20
+ # available options:
21
+ # - device - device type
22
+ # - root_path - root_path to file
23
+ # - section - category page name
24
+ # - range - page name
25
+ # @return styles
26
+ def render_style(options)
27
+ return if options[:device].nil?
28
+
29
+ root_path = options[:root_path] || '/'
30
+
31
+ file_name =
32
+ [options[:section], options[:range], options[:device]]
33
+ .reject { |opt| opt.nil? }
34
+ .join('.') + '.css'
35
+
36
+ path = settings.public_folder.to_s + '/css' + root_path + file_name
37
+
38
+ scss_path =
39
+ root_path +
40
+ [options[:section], options[:range]]
41
+ .reject { |opt| opt.nil? }
42
+ .join('/')
43
+
44
+ if File.exist?(path) && Marfa.config.cache_styles
45
+ send_file(File.join(settings.public_folder.to_s + '/css', File.basename(file_name)), type: 'text/css')
46
+ else
47
+ styles = create_style(scss_path, options[:device])
48
+ File.write(path, styles) if Marfa.config.cache_styles
49
+ content_type 'text/css', charset: 'utf-8', cache: 'false'
50
+ styles
51
+ end
52
+ end
53
+
54
+ # Create styles
55
+ # @param [String] scss_path - path to scss file
56
+ # @param [String] device - device type
57
+ def create_style(scss_path, device)
58
+ dynamic_vars(device)
59
+
60
+ if Marfa.config.minify_css
61
+ output = scss(:"#{scss_path}", { style: :compressed, cache: false })
62
+ output = Csso.optimize(output)
63
+ else
64
+ output = scss(:"#{scss_path}", { style: :expanded, cache: false })
65
+ end
66
+
67
+ output
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,69 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ module Marfa
5
+ # Extend Models
6
+ module Models
7
+ # Base model
8
+ class APIModel
9
+ # Base model error
10
+ class ModelError < StandardError; end
11
+
12
+ @aliases = {}
13
+
14
+ # Get data
15
+ # @param params [Hash] options hash
16
+ # @example
17
+ # BaseModel.get_data({ path: 'category/list' })
18
+ # @return [Hash] data from API
19
+ def self.get_data(params)
20
+ get_raw_data(params)
21
+ end
22
+
23
+ # Method that make request to delete data from API
24
+ # @param model_id [Fixnum] id to delete
25
+ # @example
26
+ # BaseModel.delete(19)
27
+ # @return [Hash]
28
+ def self.delete(model_id)
29
+ model_name = name.downcase
30
+ # data = {}
31
+
32
+ # begin
33
+ # path = "#{Marfa.config.api_server}#{model_name}/#{model_id.to_s}"
34
+ # response = RestClient.delete(path, { params: {}, headers: {} })
35
+ # data = JSON.parse(response.body, symbolize_names: true)
36
+ # rescue
37
+ # p '404 or ParserError'
38
+ # end
39
+
40
+ # Temporary code
41
+ p "#{Marfa.config.api_server}#{model_name}/#{model_id}"
42
+ p model_id
43
+
44
+ data = { deleted: true }
45
+ Marfa.cache.delete_by_pattern(model_name)
46
+ data
47
+ end
48
+
49
+ # "Raw" data getting
50
+ # @param params [Hash] - options hash
51
+ # @example
52
+ # self.get_raw_data({ path: 'category/list' })
53
+ # @return [Hash]
54
+ def self.get_raw_data(params)
55
+ result = {}
56
+ path = params[:path]
57
+
58
+ begin
59
+ response = RestClient.get("#{Marfa.config.api_server}#{path}", { params: params[:query], headers: {} })
60
+ result = JSON.parse(response.body, symbolize_names: true)
61
+ rescue
62
+ p '404 or ParserError'
63
+ end
64
+
65
+ result
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,10 @@
1
+ # Extending Marfa
2
+ module Marfa
3
+ # Extending Models
4
+ module Models
5
+ # Base DTO
6
+ class BaseDTO
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # Extend Marfa
2
+ module Marfa
3
+ # Extend Models
4
+ module Models
5
+ # Base model
6
+ class DBModel
7
+ # Base model error
8
+ class ModelError < StandardError; end
9
+
10
+ # @return [Hash] data from DB
11
+ def self.get_data(params)
12
+ {}
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ # Version constant
2
+ module Marfa
3
+ # The version constant for the current version of Marfa
4
+ VERSION = '0.0.3.9' unless defined?(Marfa::VERSION)
5
+
6
+ # The current Marfa version.
7
+ # @return [String] The version number
8
+ def self.version
9
+ VERSION
10
+ end
11
+ end
data/lib/marfa.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'sinatra/base'
2
+ require 'marfa/configuration'
3
+ require 'marfa/cache'
4
+ require 'marfa/version'
5
+ require 'marfa/helpers/controller'
6
+ require 'marfa/helpers/http/vary'
7
+ require 'marfa/helpers/scss'
8
+ require 'marfa/helpers/style'
9
+ require 'marfa/helpers/javascript'
10
+ require 'marfa/controllers'
11
+ require 'marfa/controllers/base_controller'
12
+ require 'marfa/controllers/css_controller'
13
+ require 'marfa/helpers/classes/string'
14
+ require 'marfa/helpers/sass_functions'
15
+ require 'marfa/models/base_dto'
16
+ require 'marfa/models/api_model'
17
+ require 'marfa/models/db_model'
18
+
19
+ # requires all submodules
20
+ module Marfa
21
+
22
+ end
data/marfa.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env gem build
2
+ # encoding: utf-8
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'marfa'
6
+ s.version = '0.0.3.9'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.date = Time.now.strftime('%Y-%m-%d')
9
+ s.summary = 'Little Sinatra-based framework'
10
+ s.description = 'Little Sinatra-based framework'
11
+ s.authors = ['Max Krechetov', 'Roman Yakushev', 'Anatoly Matov']
12
+ s.email = 'mvkrechetov@gmail.com'
13
+ s.require_paths = ['lib']
14
+ s.files = `git ls-files`.split("\n")
15
+ s.executables = ['marfa']
16
+ s.homepage = 'http://rubygems.org/gems/marfa'
17
+ s.license = 'MIT'
18
+
19
+ s.add_dependency('haml')
20
+ s.add_dependency('puma')
21
+ s.add_dependency('rack_csrf')
22
+ s.add_dependency('rake')
23
+ s.add_dependency('redis')
24
+ s.add_dependency('rest-client')
25
+ s.add_dependency('sass')
26
+ s.add_dependency('babel-transpiler')
27
+ s.add_dependency('closure-compiler')
28
+ s.add_dependency('sinatra')
29
+ s.add_dependency('sinatra-contrib')
30
+ s.add_dependency('device_detector', '~> 1.0')
31
+ s.add_dependency('htmlcompressor', '~> 0.3.1')
32
+ s.add_dependency('csso-rails', '~> 0.5.0')
33
+ s.add_dependency('yard')
34
+ s.add_dependency('yard-sinatra')
35
+ end
metadata ADDED
@@ -0,0 +1,293 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marfa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3.9
5
+ platform: ruby
6
+ authors:
7
+ - Max Krechetov
8
+ - Roman Yakushev
9
+ - Anatoly Matov
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2017-01-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: haml
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: puma
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rack_csrf
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: redis
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rest-client
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: sass
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :runtime
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: babel-transpiler
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :runtime
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: closure-compiler
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: sinatra
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ type: :runtime
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ - !ruby/object:Gem::Dependency
156
+ name: sinatra-contrib
157
+ requirement: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ type: :runtime
163
+ prerelease: false
164
+ version_requirements: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ - !ruby/object:Gem::Dependency
170
+ name: device_detector
171
+ requirement: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - "~>"
174
+ - !ruby/object:Gem::Version
175
+ version: '1.0'
176
+ type: :runtime
177
+ prerelease: false
178
+ version_requirements: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - "~>"
181
+ - !ruby/object:Gem::Version
182
+ version: '1.0'
183
+ - !ruby/object:Gem::Dependency
184
+ name: htmlcompressor
185
+ requirement: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - "~>"
188
+ - !ruby/object:Gem::Version
189
+ version: 0.3.1
190
+ type: :runtime
191
+ prerelease: false
192
+ version_requirements: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - "~>"
195
+ - !ruby/object:Gem::Version
196
+ version: 0.3.1
197
+ - !ruby/object:Gem::Dependency
198
+ name: csso-rails
199
+ requirement: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - "~>"
202
+ - !ruby/object:Gem::Version
203
+ version: 0.5.0
204
+ type: :runtime
205
+ prerelease: false
206
+ version_requirements: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - "~>"
209
+ - !ruby/object:Gem::Version
210
+ version: 0.5.0
211
+ - !ruby/object:Gem::Dependency
212
+ name: yard
213
+ requirement: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ type: :runtime
219
+ prerelease: false
220
+ version_requirements: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
225
+ - !ruby/object:Gem::Dependency
226
+ name: yard-sinatra
227
+ requirement: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ type: :runtime
233
+ prerelease: false
234
+ version_requirements: !ruby/object:Gem::Requirement
235
+ requirements:
236
+ - - ">="
237
+ - !ruby/object:Gem::Version
238
+ version: '0'
239
+ description: Little Sinatra-based framework
240
+ email: mvkrechetov@gmail.com
241
+ executables:
242
+ - marfa
243
+ extensions: []
244
+ extra_rdoc_files: []
245
+ files:
246
+ - ".gitignore"
247
+ - Gemfile
248
+ - Rakefile
249
+ - bin/marfa
250
+ - lib/marfa.rb
251
+ - lib/marfa/blocks/base_block.rb
252
+ - lib/marfa/cache.rb
253
+ - lib/marfa/configuration.rb
254
+ - lib/marfa/controllers.rb
255
+ - lib/marfa/controllers/base_controller.rb
256
+ - lib/marfa/controllers/css_controller.rb
257
+ - lib/marfa/helpers/classes/string.rb
258
+ - lib/marfa/helpers/controller.rb
259
+ - lib/marfa/helpers/http/vary.rb
260
+ - lib/marfa/helpers/javascript.rb
261
+ - lib/marfa/helpers/sass_functions.rb
262
+ - lib/marfa/helpers/scss.rb
263
+ - lib/marfa/helpers/style.rb
264
+ - lib/marfa/models/api_model.rb
265
+ - lib/marfa/models/base_dto.rb
266
+ - lib/marfa/models/db_model.rb
267
+ - lib/marfa/version.rb
268
+ - marfa.gemspec
269
+ homepage: http://rubygems.org/gems/marfa
270
+ licenses:
271
+ - MIT
272
+ metadata: {}
273
+ post_install_message:
274
+ rdoc_options: []
275
+ require_paths:
276
+ - lib
277
+ required_ruby_version: !ruby/object:Gem::Requirement
278
+ requirements:
279
+ - - ">="
280
+ - !ruby/object:Gem::Version
281
+ version: '0'
282
+ required_rubygems_version: !ruby/object:Gem::Requirement
283
+ requirements:
284
+ - - ">="
285
+ - !ruby/object:Gem::Version
286
+ version: '0'
287
+ requirements: []
288
+ rubyforge_project:
289
+ rubygems_version: 2.6.8
290
+ signing_key:
291
+ specification_version: 4
292
+ summary: Little Sinatra-based framework
293
+ test_files: []