Sha256: 78f2d2e5373081c4d18a3ef6c5ae23658e1cca74b0ef2a0c23fe4ffac2f54e88

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module URLVoid
  class Client

    attr_reader :api_key, :identifier

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

    # URLVoid API endpoint URL
    #
    # @return [String] Endpoint URL
    def endpoint
      "http://api.urlvoid.com/#{identifier}/#{api_key}"
    end

    # Handling HTTP request that raise an exception
    #
    # @raise URLVoidError
    # @yield Give a block that handling HTTP request error
    def with_http_error_handling
      yield
    rescue RestClient::ExceptionWithResponse => e
      raise URLVoidError, e.message
    end

    # Send a query to URLVoid API
    #
    # @param [String] path URL path
    # @return [Hash] Parsed response
    def query_api(path)
      with_http_error_handling do
        res = RestClient.get(endpoint + path)
        h = Hash.from_xml(res.body)
        h["response"]
      end
    end

    # Send a query to URLVoid API
    #
    # @param [String] path URL path
    # @return [Hash] Parsed response
    def self.query_api(path)
      new.query_api path
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rb-urlvoid-0.1.0 lib/rb-urlvoid/client.rb