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.
154 155 156 |
# File 'lib/cobranding/layout.rb', line 154 def initialize (html = nil) self.html = html if html end |
Instance Attribute Details
#src ⇒ Object
Returns the value of attribute src.
151 152 153 |
# File 'lib/cobranding/layout.rb', line 151 def src @src end |
Class Method Details
.cache_key(url, options = {}) ⇒ Object
Generate a unique cache key for the layout request.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cobranding/layout.rb', line 43 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}=#{[key]}" end "#{name}.#{Digest::MD5.hexdigest()}" end |
.get(url, options = {}, &block) ⇒ Object
Get the layout HTML from a service. The options can be any of the options accepted by SimpleHttpClient 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.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cobranding/layout.rb', line 23 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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/cobranding/layout.rb', line 169 def evaluate (context, = nil) if src prefix = [:prefix] if suffix = [:suffix] if suffix = "_for_cobranding" unless prefix or suffix evaluator = Object.new eval <<-EOS def evaluator.evaluate #{src} end EOS evaluator.evaluate do |var| method = "#{prefix}#{var}#{suffix}" context.send(method) if context.respond_to?(method) end end end |
#html=(html) ⇒ Object
Set the src by compiling HTML into RHTML and then into Ruby code.
159 160 161 |
# File 'lib/cobranding/layout.rb', line 159 def html= (html) self.src = compile(html) end |