Class: UnAPI::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/unapi/utils.rb

Overview

helper stuff used in the rest of the package

Class Method Summary collapse

Class Method Details

.get(url, hops = 1) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unapi/utils.rb', line 10

def Utils.get(url, hops=1)
  return '', '', '' if hops > 10
  begin
    response = Net::HTTP.get_response(URI.parse(url))
    code = Integer(response.code)
    body = response.body == '' ? nil : response.body
    # v1.8.2 has no content_type attribute
    content_type = response.respond_to?('content_type') ?
      response.content_type : response.header['content-type']

    # follow 302 redirects
    if code == 302
      return get(response.header.get_fields('location')[0], hops+1)
    end
    return code, body, content_type
  rescue Exception => e
    return nil, nil, nil
  end
end

.get_document(url) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/unapi/utils.rb', line 30

def Utils.get_document(url)
  begin
    code, xml, content_type = get(url)
    return code, REXML::Document.new(xml), content_type
  rescue Exception => e
    return nil, nil, nil
  end
end

.get_html_document(url) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/unapi/utils.rb', line 39

def Utils.get_html_document(url)
  begin
    code, html, content_type = get(url)
    # hack so that + (or whatever) isn't passed off to the 
    # ruby sgml parser, since it barfs badly
    html.gsub!(/&#0(\d+);/, '&#\1;')
    return code, BeautifulSoup.new(html), content_type
  rescue Exception => e
    return nil, nil, nil
  end
end