Class: Powncer::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/powncer/connection.rb

Direct Known Subclasses

Authentication::Basic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(is_auth = false) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
# File 'lib/powncer/connection.rb', line 7

def initialize(is_auth=false)
  @url = "#{Powncer::Version::API_URL}/#{Powncer::Version::API_VERSION}"
  @authenticated = is_auth
end

Instance Attribute Details

#authenticatedObject

Returns the value of attribute authenticated.



5
6
7
# File 'lib/powncer/connection.rb', line 5

def authenticated
  @authenticated
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/powncer/connection.rb', line 5

def url
  @url
end

Instance Method Details

#get(uri, use_auth = false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/powncer/connection.rb', line 12

def get(uri, use_auth=false)
  endpoint = "#{@url}#{uri}"    
  begin
    if use_auth
      response = Authentication::Basic.open(endpoint, Powncer.connect!.options)
    else
      response = open(endpoint)
    end
    case response.status
    when ["200", "OK"]
      response.read
    else
      raise BadResponse
    end
  rescue
    raise ConnectionUnavailable
  end
end