Class: Coca::Delegate

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/coca/delegate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '') {|_self| ... } ⇒ Delegate

Returns a new instance of Delegate.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
# File 'lib/coca/delegate.rb', line 9

def initialize(name='')
  @name = name
  yield self if block_given?
end

Instance Attribute Details

#hostObject



18
19
20
# File 'lib/coca/delegate.rb', line 18

def host
  @host ||= "localhost"
end

#name=(value) ⇒ Object (writeonly)

Sets the attribute name

Parameters:

  • value

    the value to set the attribute name to.



7
8
9
# File 'lib/coca/delegate.rb', line 7

def name=(value)
  @name = value
end

#portObject



22
23
24
# File 'lib/coca/delegate.rb', line 22

def port
  @port
end

#protocolObject



26
27
28
# File 'lib/coca/delegate.rb', line 26

def protocol
  @protocol ||= 'https'
end

#ttlObject



14
15
16
# File 'lib/coca/delegate.rb', line 14

def ttl
  @ttl ||= Coca.token_ttl
end

Instance Method Details

#authenticate(scope, credentials) ⇒ Object



56
57
58
59
# File 'lib/coca/delegate.rb', line 56

def authenticate(scope, credentials)
  response = HTTParty.get url, :scope => scope, :"#{scope}" => credentials
  response.body if response.code != 401
end

#base_uriObject



30
31
32
# File 'lib/coca/delegate.rb', line 30

def base_uri
  [host, port].compact.join(':')
end

#host_url_with_portObject



46
47
48
49
50
# File 'lib/coca/delegate.rb', line 46

def host_url_with_port
  hup = "#{protocol}://#{host}"
  hup << ":#{port}" if port
  hup
end

#ip_addressObject



38
39
40
# File 'lib/coca/delegate.rb', line 38

def ip_address
  @ip ||= Resolv.new.getaddress(host)
end

#pathObject



34
35
36
# File 'lib/coca/delegate.rb', line 34

def path
  @path ||= "/coca/1/check"
end

#urlObject



52
53
54
# File 'lib/coca/delegate.rb', line 52

def url
  @url ||= URI.join(host_url_with_port, path).to_s
end

#valid_referer?(referer) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/coca/delegate.rb', line 42

def valid_referer?(referer)
  !!referer && !referer.blank? && referer == ip_address
end