Class: TentClient::Discovery

Inherits:
Object
  • Object
show all
Defined in:
lib/tent-client/discovery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, url) ⇒ Discovery

Returns a new instance of Discovery.



7
8
9
# File 'lib/tent-client/discovery.rb', line 7

def initialize(client, url)
  @client, @url = client, url
end

Instance Attribute Details

#primary_profile_urlObject

Returns the value of attribute primary_profile_url.



5
6
7
# File 'lib/tent-client/discovery.rb', line 5

def primary_profile_url
  @primary_profile_url
end

#profileObject

Returns the value of attribute profile.



5
6
7
# File 'lib/tent-client/discovery.rb', line 5

def profile
  @profile
end

#profile_urlsObject

Returns the value of attribute profile_urls.



5
6
7
# File 'lib/tent-client/discovery.rb', line 5

def profile_urls
  @profile_urls
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/tent-client/discovery.rb', line 5

def url
  @url
end

Instance Method Details

#get_profileObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tent-client/discovery.rb', line 23

def get_profile
  profile_urls.each do |url|
    res = @client.http.get(url)
    if res['Content-Type'].split(';').first == MEDIA_TYPE
      @profile = res.body
      @primary_profile_url = url
      break
    end
  end
  [@profile, @primary_profile_url.to_s.sub(%r{/profile$}, '')]
end

#httpObject



11
12
13
14
15
16
# File 'lib/tent-client/discovery.rb', line 11

def http
  @http ||= Faraday.new do |f|
    f.response :follow_redirects
    f.adapter *Array(@client.faraday_adapter)
  end
end

#performObject



18
19
20
21
# File 'lib/tent-client/discovery.rb', line 18

def perform
  @profile_urls = perform_head_discovery || perform_get_discovery || []
  @profile_urls.map! { |l| l =~ %r{\A/} ? URI.join(url, l).to_s : l }
end

#perform_get_discoveryObject



39
40
41
42
# File 'lib/tent-client/discovery.rb', line 39

def perform_get_discovery
  res = http.get(url)
  perform_header_discovery(res) || perform_html_discovery(res)
end

#perform_head_discoveryObject



35
36
37
# File 'lib/tent-client/discovery.rb', line 35

def perform_head_discovery
  perform_header_discovery http.head(url)
end

#perform_header_discovery(res) ⇒ Object



44
45
46
47
48
49
# File 'lib/tent-client/discovery.rb', line 44

def perform_header_discovery(res)
  if header = res['Link']
    links = LinkHeader.parse(header).links.select { |l| l[:rel] == PROFILE_REL }.map { |l| l.uri }
    links unless links.empty?
  end
end

#perform_html_discovery(res) ⇒ Object



51
52
53
54
# File 'lib/tent-client/discovery.rb', line 51

def perform_html_discovery(res)
  return unless res['Content-Type'] == 'text/html'
  Nokogiri::HTML(res.body).css(%(link[rel="#{PROFILE_REL}"])).map { |l| l['href'] }
end