Class: UnAPI::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Page

initialize the UnAPI::URI object with a web page url



12
13
14
15
# File 'lib/unapi/page.rb', line 12

def initialize(url)
  @page_uri = URI.parse(url)
  @status_code, @document = Utils.get_html_document(url)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/unapi/page.rb', line 9

def document
  @document
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



9
10
11
# File 'lib/unapi/page.rb', line 9

def status_code
  @status_code
end

Instance Method Details

#serviceObject

get a service object for interacting with the unap service



41
42
43
44
# File 'lib/unapi/page.rb', line 41

def service 
  return Service.new(service_url) if service_url
  return nil
end

#service_urlObject

the unapi service url



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/unapi/page.rb', line 18

def service_url
  return nil if not @document or not @document.head
  link_url = nil
  @document.head.find_all('link') do |link|
    link_url = link['href'] if link['title'] == 'unAPI'
  end
  return nil if not link_url

  # if the host isn't mentioned then we'll assume relative to page_uri 
  service_uri = URI.parse(link_url)
  if service_uri.relative?
    service_uri.host = @page_uri.host 
    service_uri.port = @page_uri.port
    service_uri.scheme = @page_uri.scheme
  end

  # if they included a ? let's get rid of that
  service_uri.query = nil if service_uri.query == ''

  return service_uri.to_s
end

#urisObject

get a list of uris for the page



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/unapi/page.rb', line 47

def uris
  uris = []
  return uris if not @document
  @document.find_all('span') do |span|
    next unless span['class'] and span['title']
    # can have multiple css classes
    classes = span['class'].split(/\s+/)
    if classes.member? 'unapi-uri'
      uris << span['title']
    end
  end
  return uris
end