phantom_renderer 0.0.1

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTg1YmUxMTMyY2RiODg2MDc5ZDY3NDc2NmE3YzgxNzQ2M2ZhNWY4MA==
5
+ data.tar.gz: !binary |-
6
+ MzkyYzY3NTg4NjNmMzRkMmJiM2Y0ODEwNWIzMWVlZTU0OTZjN2Q4Zg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ N2RiMGU3YTMzYzViYTI2ZDkxZDA2YTU3Yzg4ZGQ2ZmE2Yzc4NzA2MjJmYzc0
10
+ ZDcwODcxNjAwNjFmZmRhNjUzMTk1ODMzMGM5MGIzZmNjMzI2MzZhM2IwNzdm
11
+ NjExODAwYzI2ZDk1NTBkMTYxNTljYjYxMDYyOTgzODUyNDg2N2U=
12
+ data.tar.gz: !binary |-
13
+ OGY1ZTE4ZWJlYjY0ZDk5ZGVlYjA4OTI2OWJlYWU4ZWU3YTVhYzVmOTczYmVk
14
+ MzgxZTQxOTVmNWZiMWZlODg2ZDhjMWE2YzdlNWM2NmJkOGQ5N2U5N2JjMmY0
15
+ YjdhZmJhMDgwMTM4YWFhNmVhNWFjNzIxN2RlMzA2ZmQzY2U5ZDk=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = PhantomRenderer
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'PhantomRenderer'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,3 @@
1
+ module PhantomRenderer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,103 @@
1
+ module PhantomRenderer
2
+
3
+ def render_via_phantom(opts = {})
4
+ cached = opts[:cache_key] && Rails.cache.read(opts[:cache_key])
5
+ if cached
6
+ ActiveSupport::Notifications.instrument('phantom_renderer.cache.responses.count')
7
+ render text: cached
8
+ elsif !requested_by_phantom?
9
+ uri = get_phantom_uri
10
+ req = assemble_phantom_request(uri)
11
+ res = get_response_from_phantom(uri, req)
12
+ if res
13
+ ActiveSupport::Notifications.instrument('phantom_renderer.renders.count')
14
+ Rails.cache.write(opts[:cache_key], res, expires_in: opts[:expires_in]) if opts[:cache_key]
15
+ render text: res
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ private
22
+
23
+ def requested_by_phantom?
24
+ !phantom_header.blank?
25
+ end
26
+
27
+ def cfg
28
+ PhantomRenderer::Configuration
29
+ end
30
+
31
+ def phantom_header
32
+ request.headers[cfg.request_by_phantom_header]
33
+ end
34
+
35
+ def get_response_from_phantom(uri, req)
36
+ begin
37
+ res = Timeout.timeout(cfg.render_timeout) do
38
+ ActiveSupport::Notifications.instrument('phantom_renderer.renders.time') do
39
+ Net::HTTP.start(uri.host, uri.port) {|http| http.request(req) }
40
+ end
41
+ end
42
+ response.headers[cfg.request_by_phantom_header] = "true"
43
+ return res.body
44
+ rescue Timeout::Error => e
45
+ handle_timeout(e, uri)
46
+ return false
47
+ rescue Exception => e
48
+ handle_other_exception(e, uri)
49
+ return false
50
+ end
51
+ end
52
+
53
+ def assemble_phantom_request(uri)
54
+ req = Net::HTTP::Get.new(uri.request_uri)
55
+ req.add_field cfg.request_by_phantom_header, "true"
56
+ req.add_field cfg.request_backend_header, "#{private_ip_address}:#{cfg.unicorn_port}"
57
+ req
58
+ end
59
+
60
+ def get_phantom_uri
61
+ uri = URI(request.original_url)
62
+ uri.host = cfg.server_ip
63
+ uri.port = cfg.server_port
64
+ uri
65
+ end
66
+
67
+ def handle_timeout(e, uri)
68
+ ActiveSupport::Notifications.instrument('phantom_renderer.errors.timeout', error: e.class.name, message: e.message, url: uri)
69
+ end
70
+
71
+ def handle_other_exception(e, uri)
72
+ ActiveSupport::Notifications.instrument('phantom_renderer.errors.other', error: e.class.name, message: e.message, url: uri)
73
+ end
74
+
75
+ def private_ip_address
76
+ @private_ip_address ||= begin
77
+ require 'socket'
78
+ ip=Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
79
+ ip.ip_address
80
+ end
81
+ end
82
+
83
+
84
+ class TimeoutError < RuntimeError; end;
85
+ class RenderError < RuntimeError; end;
86
+
87
+ class Configuration
88
+ class << self
89
+ [ :server_ip, :server_port, :render_timeout, :request_by_phantom_header, :request_backend_header, :unicorn_port ].each do |attr|
90
+ define_method attr do
91
+ config[attr]
92
+ end
93
+ end
94
+
95
+ def config
96
+ @config ||= HashWithIndifferentAccess.new(YAML.load_file(Rails.root.join("config/phantom_renderer.yml"))[Rails.env])
97
+ end
98
+
99
+
100
+ end
101
+ end
102
+ end
103
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :phantom_renderer do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phantom_renderer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Erez Rabih
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A Ruby on Rails Phantom-Server agent.
56
+ email:
57
+ - erez.rabih@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/phantom_renderer/version.rb
63
+ - lib/phantom_renderer.rb
64
+ - lib/tasks/phantom_renderer_tasks.rake
65
+ - MIT-LICENSE
66
+ - Rakefile
67
+ - README.rdoc
68
+ homepage: https://github.com/FTBpro/phantom_renderer
69
+ licenses: []
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.0.5
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A Ruby on Rails Phantom-Server agent.
91
+ test_files: []
92
+ has_rdoc: