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)
  @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

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#idsObject

get a list of ids for the page



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/unapi/page.rb', line 54

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

#serviceObject

get a service object for interacting with the unap service



48
49
50
51
# File 'lib/unapi/page.rb', line 48

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
39
40
41
42
43
44
45
# 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['rel'] == 'unapi-server'
  end
  return nil if not link_url

  # if it's a relative link it must be made non-relative using 
  # the url for the page
  service_uri = URI.parse(link_url)
  if service_uri.relative?
    service_uri.host = @uri.host 
    service_uri.port = @uri.port unless @uri.port == 80
    service_uri.scheme = @uri.scheme

    # some code I'm probably going to regret that pulls the
    # constructs the relative path from the page uri by 
    # removing everyting after the last slash on the page uri
    truncated_page_path = @uri.path.split('/')[0..-2].join('/')
    service_uri.path = truncated_page_path + '/' + service_uri.path
  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