Module: Onebox::Helpers

Defined in:
lib/onebox/helpers.rb

Defined Under Namespace

Classes: DownloadTooLarge

Class Method Summary collapse

Class Method Details

.blank?(value) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/onebox/helpers.rb', line 74

def self.blank?(value)
  if value.respond_to?(:blank?)
    value.blank?
  else
    value.respond_to?(:empty?) ? !!value.empty? : !value
  end
end

.clean(html) ⇒ Object



17
18
19
# File 'lib/onebox/helpers.rb', line 17

def self.clean(html)
  html.gsub(/<[^>]+>/, ' ').gsub(/\n/, '')
end

.click_to_scroll_div(width = 690, height = 400) ⇒ Object



70
71
72
# File 'lib/onebox/helpers.rb', line 70

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

Raises:

  • (Net::HTTPError)


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/onebox/helpers.rb', line 21

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)
  uri = URI("#{domain}#{location}") if !uri.host

  result = StringIO.new
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.is_a?(URI::HTTPS)) do |http|
    http.open_timeout = Onebox.options.connect_timeout
    http.read_timeout = Onebox.options.timeout
    if uri.is_a?(URI::HTTPS)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    request = Net::HTTP::Get.new(uri.request_uri, headers)
    start_time = Time.now

    size_bytes = Onebox.options.max_download_kb * 1024
    http.request(request) do |response|

      if cookie = response.get_fields('set-cookie')
        header = { 'cookie' => cookie.join }
      end

      header = nil unless header.is_a? Hash

      code = response.code.to_i
      unless code === 200
        response.error! unless [301, 302].include?(code)
        return fetch_response(
          response['location'],
          limit - 1,
          "#{uri.scheme}://#{uri.host}",
          header
        )
      end

      response.read_body do |chunk|
        result.write(chunk)
        raise DownloadTooLarge.new if result.size > size_bytes
        raise Timeout::Error.new if (Time.now - start_time) > Onebox.options.timeout
      end

      return result.string
    end
  end
end

.normalize_url_for_output(url) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/onebox/helpers.rb', line 90

def self.normalize_url_for_output(url)
  return "" unless url
  url = url.dup
  # expect properly encoded url, remove any unsafe chars
  url.gsub!("'", "&apos;")
  url.gsub!('"', "&quot;")
  url.gsub!(/[^\w\-`.~:\/?#\[\]@!$&'\(\)*+,;=]/, "")
  url
end

.symbolize_keys(hash) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/onebox/helpers.rb', line 6

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



86
87
88
# File 'lib/onebox/helpers.rb', line 86

def self.title_attr(meta)
  (meta && !blank?(meta[:title])) ? "title='#{meta[:title]}'" : ""
end

.truncate(string, length = 50) ⇒ Object



82
83
84
# File 'lib/onebox/helpers.rb', line 82

def self.truncate(string, length = 50)
  string.size > length ? string[0...(string.rindex(" ", length)||length)] + "..." : string
end