Class: YourKarma::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yourkarma/client.rb

Defined Under Namespace

Classes: BadResponseError, ConnectionError

Constant Summary collapse

HTTP_ERRORS =
[Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
Errno::ENETUNREACH, Errno::ETIMEDOUT, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
Net::ProtocolError, SocketError]
DEFAULT_OPTIONS =
{
  timeout: 10,
  url: "http://hotspot.yourkarma.com/api/status.json",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = DEFAULT_OPTIONS) ⇒ Client

Returns a new instance of Client.



22
23
24
# File 'lib/yourkarma/client.rb', line 22

def initialize(options = DEFAULT_OPTIONS)
  self.options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/yourkarma/client.rb', line 20

def options
  @options
end

Instance Method Details

#getObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yourkarma/client.rb', line 26

def get
  uri      = URI(options[:url])
  response = nil
  Timeout::timeout(options[:timeout]) do
    response = Net::HTTP.get_response(uri)
  end
  raise ConnectionError unless response.code == '200'
  JSON::load(response.body).fetch("device")
rescue *HTTP_ERRORS => e
  raise ConnectionError, e.message
rescue JSON::ParserError => e
  raise BadResponseError, e.message
end