Class: Cobranding::Layout

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#srcObject

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, options = {})
  options = options.is_a?(HashWithIndifferentAccess) ? options.dup : options.with_indifferent_access
  full_uri = full_uri(url, options)
  params = options.delete(:params)
  options.delete(:host)
  options.delete(:port)
  options.delete(:scheme)
  options.delete(:base)
  options.delete(:ttl)
  options.delete(:timeout)
  options.delete(:read_timeout)
  options.delete(:open_timeout)
  append_params_to_uri!(full_uri, params) if params
  
  options_key, query = full_uri.to_s.split('?', 2)
  if query
    options_key << '?'
    options_key << query.split('&').sort.join('&')
  end
  
  options.keys.sort{|a,b| a.to_s <=> b.to_s}.each do |key|
    options_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 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, options = {}, &block)
  return nil if url.blank?
  options ||= {}
  options = options.is_a?(HashWithIndifferentAccess) ? options.dup : options.with_indifferent_access
  ttl = options.delete(:ttl)
  race_ttl = options.delete(:race_condition_ttl)
  if Rails.cache && ttl
    cache_options = {}
    cache_options[:expires_in] = ttl if ttl
    cache_options[:race_condition_ttl] = race_ttl if race_ttl
    key = cache_key(url, options)
    Rails.cache.fetch(key, cache_options) do
      layout = fetch_layout(url, options, &block)
    end
  else
    return fetch_layout(url, options, &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, options = nil)
  if src
    prefix = options[:prefix] if options
    suffix = options[:suffix] if options
    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