ballonizer_proxy 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8dce4bdcd03abe3f75447233a598d6c7b49da8e
4
+ data.tar.gz: ce4ca0f0e626867707fd5d451e43fff6c56cfa2b
5
+ SHA512:
6
+ metadata.gz: 62b8f945865cd830e4ed62e9924b899e36c2dcc332558731b93b9bb08de5599bdf21d38d3411bfc9551db2c3c1cee20be21a921ed71edfacb6d499d3c35b6f9b
7
+ data.tar.gz: bb25ea19747308172fc0191b1aab7d335ae39204aebd1ec001be79af98e1c71da9d04da9f4c49fdeb06cb1312fc5354e58e564551c36db58de21d48059c16003
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ desc 'Run an example of the gem use with rackup'
2
+ task :example do
3
+ sh 'rackup example/config.ru'
4
+ end
5
+
6
+ task :default => :example
7
+
Binary file
data/example/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ require 'ballonizer_proxy'
2
+ require 'json'
3
+
4
+ path_rake_to_app = 'example'
5
+
6
+ settings = JSON.parse(File.read("#{path_rake_to_app}/settings_example.json"))
7
+
8
+ run BallonizerProxyGenerator.new(settings).app
9
+
@@ -0,0 +1,22 @@
1
+ {
2
+ "proxys_namespace": "/proxys/",
3
+ "ballonizer_proxys": [
4
+ {
5
+ "proxy_path": "/cardboard-crack/pt-br/",
6
+ "original_domain": "http://cardboard-crack.tumblr.com",
7
+ "original_paths": "/",
8
+ "img_to_ballonize_css_selector": ".stat-media-wrapper img",
9
+ "database_uri": "sqlite://example/cardboard-crack-pt-br.db",
10
+ "create_tables_if_none": true
11
+ },
12
+ {
13
+ "proxy_path": "/xkcd/pt-br/",
14
+ "original_domain": "http://xkcd.com",
15
+ "original_paths": "/",
16
+ "img_to_ballonize_css_selector": "#comic img",
17
+ "database_uri": "sqlite://example/xkcd-pt-br.db",
18
+ "create_tables_if_none": true
19
+ }
20
+ ]
21
+ }
22
+
Binary file
@@ -0,0 +1,164 @@
1
+ require 'pretty_proxy'
2
+ require 'ballonizer'
3
+ require 'sequel'
4
+ require 'rack'
5
+ require 'addressable/uri'
6
+
7
+ # A PrettyProxy (that is a Rack::Proxy, that is a rack app) subclass that
8
+ # use an Ballonizer instance to allow the addition of speech bubbles (text
9
+ # ballons) over any (X)HTML page that is requested through it.
10
+ #
11
+ # The first three parameters are equal to the PrettyProxy ones, and the last
12
+ # two equal the Ballonizer ones. This class only deal in a special way with
13
+ # the *_asset_path_for_link and form_handler_url that are joined with the
14
+ # proxy site (scheme+host+port) to make a absolute url if the original
15
+ # values are relative.
16
+ #
17
+ # @author Henrique Becker
18
+ class BallonizerProxy < PrettyProxy
19
+ attr_reader :ballonizer
20
+
21
+ def initialize(proxy_path, original_domain, original_paths, database, settings)
22
+ super(proxy_path, original_domain, original_paths)
23
+ @ballonizer = Ballonizer.new(database, settings)
24
+ end
25
+
26
+ def sugared_rewrite_response(triplet, requested_to_proxy_env, rewritten_env)
27
+ status, headers, page = triplet
28
+ original_url = Rack::Request.new(requested_to_proxy_env).url
29
+ requested_to_proxy_url = Rack::Request.new(requested_to_proxy_env).url
30
+ proxy_site = Addressable::URI.parse(requested_to_proxy_url).site
31
+
32
+ absolute_settings = {}
33
+ [ :css_asset_path_for_link,
34
+ :js_asset_path_for_link,
35
+ :form_handler_url].each do | conf_name |
36
+ relative_uri = @ballonizer.settings[conf_name]
37
+ next unless relative_uri
38
+ absolute_settings[conf_name] = Addressable::URI.parse(proxy_site)
39
+ .join(relative_uri).to_s
40
+ end
41
+
42
+ page = @ballonizer.ballonize_page(
43
+ page, original_url, headers['content-type'], absolute_settings
44
+ )
45
+
46
+ triplet = [status, headers, page]
47
+ end
48
+ end
49
+
50
+ # A class to automate the creation of an rack app that offer multiple
51
+ # BallonizerProxys. The constructor only takes a hash (often parsed from
52
+ # a JSON config file) and create a rack app that can be accessed by #app.
53
+ #
54
+ # The hash format is as follows:
55
+ #
56
+ # h['proxys_namespace'] has a String path (that start and end with slashes)
57
+ # all the proxy apps will be inside this namespace/'virtual directory'.
58
+ #
59
+ # h['ballonizer_proxys'] has an Array with the Hashes of the individual
60
+ # settings of each ballonizer proxy. The 'proxy_path', 'original_domain',
61
+ # 'original_paths' are as the PrettyProxy parameters with the same name.
62
+ # The 'database_uri' is equivalent to the database parameter of the
63
+ # Ballonizer constructor. These four are obrigatory.
64
+ # The rest of the options are as the ones described for the settings hash
65
+ # of the Ballonizer constructor. It's NOT RECOMMENDED to configure the
66
+ # :form_handler_url, :add_required_*, :add_js_for_edition, :*_asset_path_for_link,
67
+ # options, these options are automatically defined. The purpose of this
68
+ # class is mainly to handle this configuration for you, so doesn't make
69
+ # much sense to define these settings.
70
+ #
71
+ # Use 'rake example' to run the example. You can see the results in
72
+ # http://localhost:your_server_port/proxys/xkcd/pt-br/ and
73
+ # http://localhost:your_server_port/proxys/cardboad-crack/pt-br/.
74
+ # The configuration file is in example/settings_example.json
75
+ #
76
+ # @author Henrique Becker
77
+ class BallonizerProxyGenerator
78
+ module PrivateUtils
79
+ def self.deep_freeze(o)
80
+ if o.is_a? Hash
81
+ o.each_pair { | k, v | deep_freeze(k); deep_freeze(v) }
82
+ else
83
+ if o.is_a? Array
84
+ o.each { | e | deep_freeze(e) }
85
+ end
86
+ end
87
+
88
+ o.freeze
89
+ end
90
+
91
+ def self.deep_clone(o)
92
+ JSON.parse(JSON.generate(o))
93
+ end
94
+ end
95
+
96
+ ASSET_PATH = '/assets/'
97
+
98
+ private_constant :PrivateUtils, :ASSET_PATH
99
+
100
+ def initialize(settings)
101
+ @settings = PrivateUtils.deep_freeze(settings)
102
+ @ballonizer_proxys = []
103
+ @settings['ballonizer_proxys'].each do | bp_config |
104
+ puts bp_config
105
+ ballonizer_settings = {}
106
+ Ballonizer::DEFAULT_SETTINGS.each_key do | key |
107
+ value = bp_config[key.to_s]
108
+ if value
109
+ ballonizer_settings[key] = value
110
+ end
111
+ end
112
+ ballonizer_settings = {
113
+ form_handler_url: bp_config['proxy_path'] + 'request_handler',
114
+ add_required_css: true,
115
+ add_required_js_libs_for_edition: true,
116
+ create_tables_if_none: true,
117
+ # the paths for assets are made absolute in BallonizerProxy
118
+ css_asset_path_for_link: ASSET_PATH,
119
+ js_asset_path_for_link: ASSET_PATH
120
+ }.merge(ballonizer_settings)
121
+ @ballonizer_proxys << BallonizerProxy.new(
122
+ @settings['proxys_namespace'] + bp_config['proxy_path'][1..-1],
123
+ bp_config['original_domain'],
124
+ bp_config['original_paths'],
125
+ bp_config['database_uri'],
126
+ ballonizer_settings
127
+ )
128
+ end
129
+
130
+ proxys_namespace = @settings['proxys_namespace']
131
+ ballonizer_proxys = @ballonizer_proxys
132
+
133
+ @app = Rack::Builder.new do
134
+ map ASSET_PATH do
135
+ run Ballonizer.assets_app
136
+ end
137
+ ballonizer_proxys.each do | bp |
138
+ map bp.proxy_path do
139
+ run bp
140
+ end
141
+
142
+ map bp.ballonizer.settings[:form_handler_url] do
143
+ run(lambda do | env |
144
+ begin
145
+ bp.ballonizer.process_submit(env)
146
+ [200, {}, ['your changes are made with success']]
147
+ rescue Ballonizer::SubmitError => e
148
+ [200, {}, [e.message]]
149
+ end
150
+ end)
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ def app
157
+ @app
158
+ end
159
+
160
+ # The proxy namespace can't be assets
161
+ def self.validate_settings(settings)
162
+ end
163
+ end
164
+
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ballonizer_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Henrique Becker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ballonizer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pretty_proxy
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sequel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.48'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.48'
55
+ - !ruby/object:Gem::Dependency
56
+ name: addressable
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ description: ''
84
+ email: henriquebecker91@gmail.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - example/settings_example.json
90
+ - example/xkcd-pt-br.db
91
+ - example/config.ru
92
+ - example/cardboard-crack-pt-br.db
93
+ - lib/ballonizer_proxy.rb
94
+ - Rakefile
95
+ homepage: http://rubygems.org/gems/ballonizer_proxy
96
+ licenses:
97
+ - Public domain
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: 'ballonizer_proxy: rack app to create a proxy version of a site allowing
119
+ the edition of text ballons (speech bubbles) over the images'
120
+ test_files: []
121
+ has_rdoc: true