embed_me 0.1.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c628ce31537a49b18079252bd49617f003742a93221a2ecbf2ba645738f9e7e
4
- data.tar.gz: 46b44b37276d1405fb5de4d1ccb3f3820092a4f3ee920f9e69391190fda3e8d5
3
+ metadata.gz: eba667022e999ab1bc98f4321e5a54b4725ed38dc14ea829b89e22938b3d083b
4
+ data.tar.gz: 67ec8ebc91cc0902b41b1c3f51bffe5f556016a0984dfcfcc440aca29238d9b5
5
5
  SHA512:
6
- metadata.gz: 0c579ab7593c3071b070ca29ec0960b99cc51396d623308b9f129737c2eca0a35be1b05174e8aa1687102ffdfac02cf54b1a7ba641cc784b00b193b746e51b8e
7
- data.tar.gz: '007769ff6db0b0e7b9ef854fc1fa4e05e8f9bd0c122e932d80ae6b3b05aee11fd9108b214c05399fbd9fc9e4a66f84b414c502a79aa427a2747fc6b12576b5db'
6
+ metadata.gz: c6e25e412a79552e4cad0cb81d32813f7ebab05af3aa3dea241a0e38855bf3cadc5b1f2e0a830edd2b74aba81fe3903bfacb2284c5aee01d40009f8a49aa9689
7
+ data.tar.gz: 40fdf41e6dec1bbac477744f6e10b553e411a4b2e21235bd46f12c0c486bb113ec78588a796e37e1675b5f6142be912b2c0635599b6e81efba3f2267e968f03f
@@ -1,4 +1,6 @@
1
1
  require "embed_me/engine"
2
+ require "embed_me/link_helper"
3
+ require "embed_me/code_generator"
2
4
  require "embed_me/rails/routes"
3
5
 
4
6
  module EmbedMe
@@ -0,0 +1,42 @@
1
+ module EmbedMe
2
+ module CodeGenerator
3
+ include EmbedMe::LinkHelper
4
+
5
+ # Generates some HTML code that allows embedding of the resource of the current request.
6
+ # Creates an iframe element with the embed link as src attribute. HTML options can be
7
+ # customized. Returns a string.
8
+ #
9
+ # ==== Examples
10
+ # Assuming the current request is '/' (root):
11
+ #
12
+ # embed_code()
13
+ # # => "<iframe width="560" height="315" src="http://localhost:3000/embed" frameborder="0"
14
+ # sandbox="">Your Browser does not support HTML iFrame Element.</iframe>"
15
+ #
16
+ # embed_code(fallback: "")
17
+ # # => "<iframe width="560" height="315" src="http://localhost:3000/embed" frameborder="0"
18
+ # sandbox=""></iframe>"
19
+ #
20
+ # embed_code(width: 760, height: 500)
21
+ # # => "<iframe width="760" height="500" src="http://localhost:3000/embed" frameborder="0"
22
+ # sandbox="">Your Browser does not support HTML iFrame Element.</iframe>"
23
+ #
24
+ # embed_code(width: nil, height: nil)
25
+ # # => "<iframe src="http://localhost:3000/embed" frameborder="0" sandbox="">Your Browser
26
+ # does not support HTML iFrame Element.</iframe>"
27
+ def embed_code(options = {})
28
+ # get embed link or return if not present
29
+ embed_url = current_page_embed_url
30
+ return nil unless embed_url.present?
31
+
32
+ # set values
33
+ fallback = options.delete(:fallback) || "Your Browser does not support HTML iFrame Element."
34
+ default_html = {width: 560, height: 315, src: embed_url, frameborder: 0, sandbox: ''}
35
+ default_html.merge!(options)
36
+
37
+ # create element
38
+ element = content_tag(:iframe, fallback, default_html)
39
+ "#{element}"
40
+ end
41
+ end
42
+ end
@@ -2,29 +2,32 @@ module EmbedMe
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace EmbedMe
4
4
 
5
- initializer "embed_me" do
5
+ initializer 'embed_me' do
6
6
  ActiveSupport.on_load(:action_controller) do
7
7
  before_action :check_embedding!
8
+ helper_method :embedded?
9
+ helper EmbedMe::LinkHelper
10
+ helper EmbedMe::CodeGenerator
8
11
 
9
- def default_url_options(options={})
10
- hash_embed = Hash.new
11
- hash_embed[EmbedMe.scope_name] = EmbedMe.scope_name.to_s
12
- hash_normal = Hash.new
13
- hash_normal[EmbedMe.scope_name] = nil
14
- params[EmbedMe.scope_name] == EmbedMe.scope_name.to_s ? hash_embed : hash_normal
12
+ # Checks if the request comes from an embedded resource and removes the
13
+ # X-Frame-Options Header so that the content can be opened in a frame.
14
+ # If the request comes from a resource that is not intended to be embedded,
15
+ # the value remains, so that the frame blocks the rendering.
16
+ def check_embedding!
17
+ if embedded?
18
+ response.headers.except! 'X-Frame-Options'
19
+ end
15
20
  end
16
21
 
22
+ # Checks the path of the request for the existence of the embedded parameter,
23
+ # which is specified by the routes. If this value is available, the resource
24
+ # may be embedded. If the value is not available, the resource must not be
25
+ # embedded. URL parameters remain ignored. Returns a boolean value.
17
26
  def embedded?
18
27
  # using path instead of params because path[:embed] != params[:embed] if
19
28
  # URL: http://localhost:3000/posts?embed=embed
20
29
  path = Rails.application.routes.recognize_path(request.path)
21
- path[EmbedMe.scope_name] == EmbedMe.scope_name.to_s
22
- end
23
-
24
- def check_embedding!
25
- if embedded?
26
- response.headers.except! 'X-Frame-Options'
27
- end
30
+ path[:embedded].present?
28
31
  end
29
32
  end
30
33
  end
@@ -0,0 +1,115 @@
1
+ module EmbedMe
2
+ module LinkHelper
3
+ # Creates an HTML link element. To do this, it checks whether the request comes
4
+ # from an embedded resource and whether there is an embedded version of the
5
+ # requested route. If both are true, a link is generated that redirects to the
6
+ # embedded version. If no embedded version of the route exists, but the request
7
+ # is within an embedding, the link will be opened in a new tab. If the request
8
+ # is not embedded, the link is created as normal.
9
+ #
10
+ # ==== Examples
11
+ # Assuming the current request is NOT embedded, then:
12
+ #
13
+ # embeddable_link_to("Not Embeddable", private_path)
14
+ # # => <a href="/private">Not Embeddable</a>
15
+ #
16
+ # embeddable_link_to("Embeddable", embeddable_path)
17
+ # # => <a href="/embeddable">Embeddable</a>
18
+ #
19
+ # Assuming the current request IS embedded, then:
20
+ #
21
+ # embeddable_link_to("Not Embeddable", private_path)
22
+ # # => <a target="_blank" href="/private">Not Embeddable</a>
23
+ #
24
+ # embeddable_link_to("Embeddable", embeddable_path)
25
+ # # => <a href="/embed/embeddable">Embeddable</a>
26
+ def embeddable_link_to(name = nil, options = nil, html_options = {}, &block)
27
+ if embedded? && embedded_link_available?(options)
28
+ # generate embedded link if possible
29
+ merged_params = merged_embedded(options)
30
+ link_to(name, merged_params, html_options, &block)
31
+ elsif embedded?
32
+ # open link in new tab if no embedded link available
33
+ html_options.merge!({target: "_blank"})
34
+ link_to(name, options, html_options, &block)
35
+ else
36
+ # normal behaviour if not embedded
37
+ link_to(name, options, html_options, &block)
38
+ end
39
+ end
40
+
41
+ # Checks whether there is an embedded version of a specific route.
42
+ # Returns a boolean Value.
43
+ #
44
+ # ==== Examples
45
+ #
46
+ # embedded_link_available?(private_path)
47
+ # # => false
48
+ #
49
+ # embedded_link_available?(root_path)
50
+ # # => true
51
+ #
52
+ # embedded_link_available?(embed_posts_path)
53
+ # # => true
54
+ def embedded_link_available?(url_data)
55
+ merged_params = merged_embedded(url_data)
56
+
57
+ # test if embedded flag is in path or url parameter
58
+ embedded_link = url_for(merged_params)
59
+ embedded_link_params = Rails.application.routes.recognize_path(embedded_link)
60
+ embedded_link_params[:embedded].present?
61
+ end
62
+
63
+ # Extracts the Route generation parameters and merges the embedded value,
64
+ # which is used to generate embedding specific URLs. Returns a hash containing
65
+ # url generation data.
66
+ #
67
+ # ==== Examples
68
+ #
69
+ # merged_embedded(posts_path)
70
+ # # => {:controller=>"posts", :action=>"index", :embedded=>true}
71
+ #
72
+ # merged_embedded(controller: "posts", action: "index")
73
+ # # => {:controller=>"posts", :action=>"index", :embedded=>true}
74
+ def merged_embedded(url_data)
75
+ # extract link parameters
76
+ original_link = url_for(url_data)
77
+ original_link_params = Rails.application.routes.recognize_path(original_link)
78
+
79
+ # merge embedded flag
80
+ merged_params = original_link_params.merge({embedded: true})
81
+ merged_params
82
+ end
83
+
84
+ # Checks if there is an embedded version for the current request. If this is the
85
+ # case, the link to the embedded version is returned. If there is no embedded
86
+ # version, nil is returned.
87
+ #
88
+ # ==== Examples
89
+ # Assuming the current request IS embeddable:
90
+ # (E.g. root_path)
91
+ #
92
+ # current_page_embed_url()
93
+ # # => http://localhost:3000/embed
94
+ #
95
+ # (E.g. post_path(id: 1))
96
+ #
97
+ # current_page_embed_url()
98
+ # # => http://localhost:3000/embed/posts/1
99
+ #
100
+ # Assuming the current request is NOT embeddable:
101
+ # (E.g. private_path)
102
+ #
103
+ # current_page_embed_url()
104
+ # # => nil
105
+ def current_page_embed_url
106
+ # return if no link available
107
+ return nil unless embedded_link_available?(request.path)
108
+
109
+ # get current link
110
+ current_page = merged_embedded(request.path)
111
+ current_page.merge!({only_path: false})
112
+ url_for(current_page)
113
+ end
114
+ end
115
+ end
@@ -1,7 +1,26 @@
1
1
  module ActionDispatch::Routing
2
2
  class Mapper
3
+ # Enables the definition of resources that should be embeddable. The routes defined
4
+ # within the block are transferred to the application in duplicated form. Once
5
+ # normally, as they would always work, and once under an embed scope. The name of
6
+ # the scope can be set in config (default: 'embed').
7
+ #
8
+ # ==== Examples
9
+ # Assuming the following route definition:
10
+ #
11
+ # get 'path_private', to: 'application#path_private'
12
+ # embeddable do
13
+ # get 'path_embed', to: 'application#path_embed'
14
+ # end
15
+ #
16
+ # will produce following routes:
17
+ #
18
+ # path_private GET /path_private(.:format) application#path_private
19
+ # path_embed GET /path_embed(.:format) application#path_embed
20
+ # embed_path_embed GET /embed/path_embed(.:format) application#path_embed {:embedded=>true}
3
21
  def embeddable
4
- scope "(:#{EmbedMe.scope_name.to_s})", embed: /#{EmbedMe.scope_name.to_s}/ do
22
+ yield
23
+ scope EmbedMe.scope_name.to_s, as: EmbedMe.scope_name.to_s, embedded: true do
5
24
  yield
6
25
  end
7
26
  end
@@ -1,3 +1,3 @@
1
1
  module EmbedMe
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embed_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Bohn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-16 00:00:00.000000000 Z
11
+ date: 2021-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,20 +44,6 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
- - !ruby/object:Gem::Dependency
48
- name: byebug
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: '0'
61
47
  description: EmbedMe allows you and your users to easily embed your rails application
62
48
  or parts of it on other websites.
63
49
  email:
@@ -79,7 +65,9 @@ files:
79
65
  - app/views/layouts/embed_me/application.html.erb
80
66
  - config/routes.rb
81
67
  - lib/embed_me.rb
68
+ - lib/embed_me/code_generator.rb
82
69
  - lib/embed_me/engine.rb
70
+ - lib/embed_me/link_helper.rb
83
71
  - lib/embed_me/rails/routes.rb
84
72
  - lib/embed_me/version.rb
85
73
  - lib/tasks/embed_me_tasks.rake