fragments 0.1.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6d28fa6573e083f8919cc3ee9bc699b3ba9be79
4
+ data.tar.gz: d42bfe69397e74b5347c31946fa2eda15ed66f9d
5
+ SHA512:
6
+ metadata.gz: 348337f6e8be8c6371d76acc7375f832de2207fc49c6eaac563e3e8c2593bdffa617086747cf6de28b90531615e2cc32de557e7f32b11baf5b73ef4123574f52
7
+ data.tar.gz: 27bf7fb197caa399d520f36317546b97ec30368aa3d03b4a1165ec22357c875943636c5d1c7441263e86cfb9137cc3bcab11ce48d0edaa57dfd759f7117e9361
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ *.swp
12
+ /*.gem
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fragments.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Sergio Gernyak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Fragments
2
+
3
+ Provides a common functionality to be able to use layout fragments inside Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fragments'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fragments
20
+
21
+ ## Usage
22
+
23
+ Provide configuration for the library:
24
+
25
+ ```ruby
26
+ ::Fragments.config do |config|
27
+ config.host = ENV["FRAGMENT_HOST"]
28
+ config.uri_prefixes = {
29
+ nl: ENV["FRAGMENT_URI_PREFIX_NL"],
30
+ fr: ENV["FRAGMENT_URI_PREFIX_FR"]
31
+ }
32
+ config.is_enabled = ENV["USE_FRAGMENTS"] == "true"
33
+ end
34
+ ```
35
+
36
+ Include extention for controller, which provides needed view helpers:
37
+
38
+ ```ruby
39
+ class ApplicationController
40
+ include Fragments::Helpers::Controller
41
+ end
42
+ ```
43
+
44
+ Inside view you will have the following helper methods:
45
+
46
+ * `header_html` - provides a HTML structure for the header
47
+ * `common_html` - provides common assets
48
+ * `footer_html` - provides a HTML structure for the footer
49
+
50
+ Also you will have an ability to check are fragments enabled or not by calling:
51
+
52
+ `::Fragments.enabled?`
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fragments"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fragments/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fragments"
8
+ spec.version = Fragments::VERSION
9
+ spec.authors = ["Sergio Gernyak"]
10
+ spec.email = ["sergey.gernyak@diymaxeda.com"]
11
+
12
+ spec.summary = "Provides an ability to use HTML fragments inside Rails applications"
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://bitbucket.org/maxedadiy/fragments_rb"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
@@ -0,0 +1,40 @@
1
+ require "fragments/version"
2
+ require "fragments/config"
3
+ require "fragments/requester"
4
+ require "fragments/helpers/controller"
5
+
6
+ # Top level module for the Fragments library
7
+ #
8
+ # This module should be considered as a Facade, so some internal functionality
9
+ # should not be used from the outside. So, use an API, which is provided by this
10
+ # module instead!
11
+ module Fragments
12
+ # Get or set a configuration for the library
13
+ def self.config(&block)
14
+ @config ||= ::Fragments::Config.new
15
+ block_given? ? yield(@config) : @config
16
+ end
17
+
18
+ # Specifies either should fragments be used or not
19
+ #
20
+ # @return [Boolean] - a value which specifies enableness
21
+ def self.enabled?
22
+ config.is_enabled
23
+ end
24
+
25
+ # Provides a html structure for a fragment with a specified code
26
+ #
27
+ # @param code [String] - a code of a fragment which you want to request
28
+ # @param request [Request] - a reference to an object which represents an income HTTP request
29
+ # @return [String] a html structure of a needed fragment
30
+ def self.get_html(code, request)
31
+ ::Fragments::Requester.call(
32
+ fragment_code: code,
33
+ current_host: ::Fragments.config.host,
34
+ cookies: request.headers["HTTP_COOKIE"],
35
+ current_url: request.original_url
36
+ )
37
+ rescue Exception => e
38
+ ""
39
+ end
40
+ end
@@ -0,0 +1,54 @@
1
+ module Fragments
2
+ # Contains a current config to request fragments properly
3
+ #
4
+ # @author Sergey Gernyak
5
+ class Config
6
+ # Set or get a host to which request should be sent to get fragment's html structure
7
+ attr_accessor :host
8
+
9
+ # Contains URL prefixes
10
+ # It should be a hash where:
11
+ # - key is a locale in ISO2 format
12
+ # - value is an URL prefix
13
+ attr_accessor :uri_prefixes
14
+
15
+ # Specifies either fragments usage is enabled or not
16
+ attr_accessor :is_enabled
17
+
18
+ # Provides an URI prefix for a given locale
19
+ # @param locale [String] - A locale for which URI prefix should be returned
20
+ # @return [String] an URI prefix
21
+ def uri_prefix(locale)
22
+ uri_prefixes[normalize_key(locale)] || ""
23
+ end
24
+
25
+ # Setter for the URI prefixes mapping hash
26
+ # @param value [Hash] - A hash with URI prefix per locale mappings
27
+ # @raise ArgumentError - when an invalid value has been passed
28
+ def uri_prefixes=(value)
29
+ raise ::ArgumentError, "Value should be a valid Hash!" unless value.is_a?(Hash)
30
+ @uri_prefixes = Hash[value.map { |k, v| [normalize_key(k), v] }]
31
+ end
32
+
33
+ # Setter for enableness flag
34
+ # @param value [Boolean] - A value of the flag
35
+ # @raise ArgumentError - when an invalid boolean value has been passed
36
+ def is_enabled=(value)
37
+ raise ::ArgumentError, "Value should be a valid boolean one!" if !value.is_a?(TrueClass) && !value.is_a?(FalseClass)
38
+ @is_enabled = value
39
+ end
40
+
41
+ # Provides a value which specifies are fragments enabled or not
42
+ # By default it returns false.
43
+ # @return [Boolean]
44
+ def is_enabled
45
+ !!@is_enabled
46
+ end
47
+
48
+ private
49
+
50
+ def normalize_key(value)
51
+ value.downcase.to_sym
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ module Fragments
2
+ module Helpers
3
+ module Controller
4
+ def self.included(base)
5
+ base.send :include, InstanceMethods
6
+ base.class_eval do
7
+ helper_method :header_html, :common_html, :footer_html
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+ def header_html
13
+ ::Fragments.get_html('header', request)
14
+ end
15
+
16
+ def common_html
17
+ ::Fragments.get_html('service', request)
18
+ end
19
+
20
+ def footer_html
21
+ ::Fragments.get_html('footer', request)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'open-uri'
2
+ require 'net/http'
3
+ require 'net/https'
4
+
5
+ module Fragments
6
+ class Requester
7
+ attr_reader :fragment_code, :current_host, :cookies, :current_url
8
+
9
+ def initialize(fragment_code:, current_host:, cookies:, current_url:)
10
+ @fragment_code = fragment_code
11
+ @current_host = current_host
12
+ @cookies = cookies
13
+ @current_url = current_url
14
+ end
15
+
16
+ def self.call(*opts, &block)
17
+ new(*opts).call(&block)
18
+ end
19
+
20
+ def call
21
+ uri = build_uri
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ get_request = Net::HTTP::Get.new(uri.path)
24
+ apply_https(http, uri)
25
+ apply_headers(get_request)
26
+ get_response_body(http, get_request)
27
+ end
28
+
29
+ private
30
+
31
+ def build_uri
32
+ path = current_host + '/' + fragment_code;
33
+ URI.parse(path)
34
+ end
35
+
36
+ def apply_https(http, uri)
37
+ return if uri.scheme != "https"
38
+ http.use_ssl = true
39
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
40
+ end
41
+
42
+ def apply_headers(request)
43
+ request.add_field("Cookie", cookies)
44
+ request.add_field("frontend-uri", fetch_fragment_uri_prefix)
45
+ request.add_field("current-url", current_url)
46
+ request.add_field("host", current_host.gsub(/^https?\:\/\/(www.)?/,''))
47
+ end
48
+
49
+ def get_response_body(http, request)
50
+ get_response = http.request(request)
51
+ get_response.body
52
+ end
53
+
54
+ def fetch_fragment_uri_prefix
55
+ ::Fragments.config.uri_prefix(I18n.locale)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Fragments
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fragments
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Gernyak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Provides an ability to use HTML fragments inside Rails applications
56
+ email:
57
+ - sergey.gernyak@diymaxeda.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - fragments.gemspec
71
+ - lib/fragments.rb
72
+ - lib/fragments/config.rb
73
+ - lib/fragments/helpers/controller.rb
74
+ - lib/fragments/requester.rb
75
+ - lib/fragments/version.rb
76
+ homepage: https://bitbucket.org/maxedadiy/fragments_rb
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.11
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Provides an ability to use HTML fragments inside Rails applications
100
+ test_files: []