Sha256: 6c9e0c6a8f753e908429793370830ce7f749d3f23f1170931325557496e5766e

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'nokogiri'

class TentClient
  class Discovery
    attr_accessor :url, :profile_urls, :primary_profile_url, :profile

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

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

    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

    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

    def perform_head_discovery
      perform_header_discovery http.head(url)
    end

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

    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

    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
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
tent-client-joebadmo-0.0.1 lib/tent-client/discovery.rb
tent-client-0.0.1 lib/tent-client/discovery.rb