Class: Cobranding::Layout
- Inherits:
-
Object
- Object
- Cobranding::Layout
- Defined in:
- lib/cobranding/layout.rb
Overview
This class is used to get layout HTML markup from a service and compile it into Ruby code that can be used as the layout in a Rails view.
Constant Summary collapse
- QUOTED_RELATIVE_URL =
/(<\w+\s((src)|(href))=(['"]))\/(.*?)(\5[^>]*?>)/i- UNQUOTED_RELATIVE_URL =
/(<\w+\s((src)|(href))=)\/(.*?)(>|(\s[^>]*?>))/i
Instance Attribute Summary collapse
-
#src ⇒ Object
Returns the value of attribute src.
Class Method Summary collapse
-
.cache_key(url, options = {}) ⇒ Object
Generate a unique cache key for the layout request.
-
.get(url, options = {}, &block) ⇒ Object
Get the layout HTML from a service.
Instance Method Summary collapse
-
#evaluate(context, options = nil) ⇒ Object
Evaluate the RHTML source code in the specified context.
-
#html=(html) ⇒ Object
Set the src by compiling HTML into RHTML and then into Ruby code.
-
#initialize(html = nil) ⇒ Layout
constructor
Create the Layout.
Constructor Details
#initialize(html = nil) ⇒ Layout
Create the Layout. The src will be defined from the HTML passed in.
157 158 159 |
# File 'lib/cobranding/layout.rb', line 157 def initialize (html = nil) self.html = html if html end |
Instance Attribute Details
#src ⇒ Object
Returns the value of attribute src.
154 155 156 |
# File 'lib/cobranding/layout.rb', line 154 def src @src end |
Class Method Details
.cache_key(url, options = {}) ⇒ Object
Generate a unique cache key for the layout request.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cobranding/layout.rb', line 46 def cache_key (url, = {}) = .is_a?(HashWithIndifferentAccess) ? .dup : .with_indifferent_access full_uri = full_uri(url, ) params = .delete(:params) .delete(:host) .delete(:port) .delete(:scheme) .delete(:base) .delete(:ttl) .delete(:timeout) .delete(:read_timeout) .delete(:open_timeout) append_params_to_uri!(full_uri, params) if params , query = full_uri.to_s.split('?', 2) if query << '?' << query.split('&').sort.join('&') end .keys.sort{|a,b| a.to_s <=> b.to_s}.each do |key| << " #{key}=#{options[key]}" end "#{name}.#{Digest::MD5.hexdigest(options_key)}" end |
.get(url, options = {}, &block) ⇒ Object
Get the layout HTML from a service. The options can be any of the options accepted by RestClient or :base_url. Any relative URLs found in the HTML will be expanded to absolute URLs using either the :base_url option or the url as the base.
If :ttl is specified in the options, the layout will be cached for that many seconds.
By default the request will be a GET request. If you need to do a POST, you can pass :method => :post in the options.
If a block is passed, it will be called with the layout html before the layout is created. This can be used to munge the layout HTML code if necessary.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cobranding/layout.rb', line 26 def get (url, = {}, &block) return nil if url.blank? ||= {} = .is_a?(HashWithIndifferentAccess) ? .dup : .with_indifferent_access ttl = .delete(:ttl) race_ttl = .delete(:race_condition_ttl) if Rails.cache && ttl = {} [:expires_in] = ttl if ttl [:race_condition_ttl] = race_ttl if race_ttl key = cache_key(url, ) Rails.cache.fetch(key, ) do layout = fetch_layout(url, , &block) end else return fetch_layout(url, , &block) end end |
Instance Method Details
#evaluate(context, options = nil) ⇒ Object
Evaluate the RHTML source code in the specified context. Any yields will call a helper method corresponding to the value yielded if it exists. The options :prefix and :suffix can be set to determine the full method name to call. The default is to suffix values with _for_cobranding so that yield title will call title_for_cobranding. Setting a different prefix or suffix can be useful if you are pulling in templates from different sources which use the same variable names but need different values.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cobranding/layout.rb', line 172 def evaluate (context, = nil) if src prefix = [:prefix] if suffix = [:suffix] if suffix = "_for_cobranding" unless prefix or suffix evaluator = Object.new # "src" is erb code, which contains the code `force_encoding(__ENCODING__)`. # __ENCODING__ is the current file's encoding (see magic comment above). eval " def evaluator.evaluate\n \#{src}\n end\n EOS\n evaluator.evaluate do |var|\n method = \"\#{prefix}\#{var}\#{suffix}\"\n context.send(method) if context.respond_to?(method)\n end\n end\nend\n" |
#html=(html) ⇒ Object
Set the src by compiling HTML into RHTML and then into Ruby code.
162 163 164 |
# File 'lib/cobranding/layout.rb', line 162 def html= (html) self.src = compile(html) end |