Class: YourKarma::Client
- Inherits:
-
Object
- Object
- YourKarma::Client
- 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
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(options = DEFAULT_OPTIONS) ⇒ Client
constructor
A new instance of Client.
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( = DEFAULT_OPTIONS) self. = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/yourkarma/client.rb', line 20 def end |
Instance Method Details
#get ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/yourkarma/client.rb', line 26 def get uri = URI([:url]) response = nil Timeout::timeout([: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. rescue JSON::ParserError => e raise BadResponseError, e. end |