Class: URLVoid::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.

Raises:



6
7
8
9
10
# File 'lib/rb-urlvoid/client.rb', line 6

def initialize
  raise URLVoidError, "API key not found" unless configatron.key?("api_key")
  @api_key = configatron.api_key
  @identifier = configatron.identifier
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



4
5
6
# File 'lib/rb-urlvoid/client.rb', line 4

def api_key
  @api_key
end

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/rb-urlvoid/client.rb', line 4

def identifier
  @identifier
end

Class Method Details

.query_api(path) ⇒ Hash

Send a query to URLVoid API

Parameters:

  • path (String)

    URL path

Returns:

  • (Hash)

    Parsed response



45
46
47
# File 'lib/rb-urlvoid/client.rb', line 45

def self.query_api(path)
  new.query_api path
end

Instance Method Details

#endpointString

URLVoid API endpoint URL

Returns:

  • (String)

    Endpoint URL



15
16
17
# File 'lib/rb-urlvoid/client.rb', line 15

def endpoint
  "http://api.urlvoid.com/#{identifier}/#{api_key}"
end

#query_api(path) ⇒ Hash

Send a query to URLVoid API

Parameters:

  • path (String)

    URL path

Returns:

  • (Hash)

    Parsed response



33
34
35
36
37
38
39
# File 'lib/rb-urlvoid/client.rb', line 33

def query_api(path)
  with_http_error_handling do
    res = RestClient.get(endpoint + path)
    h = Hash.from_xml(res.body)
    h["response"]
  end
end

#with_http_error_handling { ... } ⇒ Object

Handling HTTP request that raise an exception

Yields:

  • Give a block that handling HTTP request error

Raises:

  • URLVoidError



23
24
25
26
27
# File 'lib/rb-urlvoid/client.rb', line 23

def with_http_error_handling
  yield
rescue RestClient::ExceptionWithResponse => e
  raise URLVoidError, e.message
end