Class: TentClient::Discovery

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

Constant Summary collapse

META_POST_REL =
"https://tent.io/rels/meta-post".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, entity_uri, options = {}) ⇒ Discovery

Returns a new instance of Discovery.



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

def initialize(client, entity_uri, options = {})
  @entity_uri = entity_uri
  @client = client.dup
  @client.faraday_adapter = :net_http
  @options = options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#entity_uriObject (readonly)

Returns the value of attribute entity_uri.



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

def entity_uri
  @entity_uri
end

#last_responseObject

Returns the value of attribute last_response.



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

def last_response
  @last_response
end

Class Method Details

.discover(client, entity_uri, options = {}) ⇒ Object



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

def self.discover(client, entity_uri, options = {})
  new(client, entity_uri, :skip_serialization => false).discover(options)
end

Instance Method Details

#discover(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tent-client/discovery.rb', line 26

def discover(options = {})
  discover_res, meta_post_urls = perform_head_discovery || perform_get_discovery

  if meta_post_urls.empty?
    return options[:return_response] ? last_response : nil
  end

  meta_post_urls.uniq.each do |url|
    url = URI.join(discover_res.env[:url].to_s, url).to_s
    begin
      res = http.get(url) do |request|
        request.headers['Accept'] = POST_CONTENT_TYPE % "https://tent.io/types/meta/v0#"
      end
    rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed
      res = Faraday::Response.new({})
    end

    if options[:return_response]
      return res
    elsif res.success? && (Hash === res.body)
      return res.body['post']
    end
  end
  nil
end