Module: Onebox::Helpers
- Defined in:
- lib/onebox/helpers.rb
Class Method Summary collapse
- .blank?(value) ⇒ Boolean
- .clean(html) ⇒ Object
- .click_to_scroll_div(width = 690, height = 400) ⇒ Object
- .fetch_response(location, limit = 5, domain = nil, headers = nil) ⇒ Object
- .normalize_url_for_output(url) ⇒ Object
- .symbolize_keys(hash) ⇒ Object
- .title_attr(meta) ⇒ Object
- .truncate(string, length = 50) ⇒ Object
Class Method Details
.blank?(value) ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/onebox/helpers.rb', line 53 def self.blank?(value) if value.respond_to?(:blank?) value.blank? else value.respond_to?(:empty?) ? !!value.empty? : !value end end |
.clean(html) ⇒ Object
14 15 16 |
# File 'lib/onebox/helpers.rb', line 14 def self.clean(html) html.gsub(/<[^>]+>/, ' ').gsub(/\n/, '') end |
.click_to_scroll_div(width = 690, height = 400) ⇒ Object
49 50 51 |
# File 'lib/onebox/helpers.rb', line 49 def self.click_to_scroll_div(width = 690, height = 400) "<div style=\"background:transparent;position:relative;width:#{width}px;height:#{height}px;top:#{height}px;margin-top:-#{height}px;\" onClick=\"style.pointerEvents='none'\"></div>" end |
.fetch_response(location, limit = 5, domain = nil, headers = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/onebox/helpers.rb', line 18 def self.fetch_response(location, limit = 5, domain = nil, headers = nil) raise Net::HTTPError.new('HTTP redirect too deep', location) if limit == 0 uri = URI(location) if !uri.host uri = URI("#{domain}#{location}") end http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = Onebox..connect_timeout http.read_timeout = Onebox..timeout if uri.is_a?(URI::HTTPS) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = http.request_get(uri.request_uri,headers) = response.get_fields('set-cookie') if () header = {'cookie' => .join("")} end header = nil unless header.is_a? Hash case response when Net::HTTPSuccess then response when Net::HTTPRedirection then fetch_response(response['location'], limit - 1, "#{uri.scheme}://#{uri.host}",header) else response.error! end end |
.normalize_url_for_output(url) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/onebox/helpers.rb', line 69 def self.normalize_url_for_output(url) url = url.dup # expect properly encoded url, remove any unsafe chars url.gsub!("'", "'") url.gsub!('"', """) url.gsub!(/[^a-zA-Z0-9%\-`._~:\/?#\[\]@!$&'\(\)*+,;=]/, "") url end |
.symbolize_keys(hash) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/onebox/helpers.rb', line 3 def self.symbolize_keys(hash) return {} if hash.nil? hash.inject({}){|result, (key, value)| new_key = key.is_a?(String) ? key.to_sym : key new_value = value.is_a?(Hash) ? symbolize_keys(value) : value result[new_key] = new_value result } end |
.title_attr(meta) ⇒ Object
65 66 67 |
# File 'lib/onebox/helpers.rb', line 65 def self.title_attr() ( && !blank?([:title])) ? "title='#{CGI.escapeHTML([:title])}'" : "" end |
.truncate(string, length = 50) ⇒ Object
61 62 63 |
# File 'lib/onebox/helpers.rb', line 61 def self.truncate(string, length = 50) string.size > length ? string[0...(string.rindex(" ", length)||length)] + "..." : string end |