Sha256: f96516120e5a4a2700d7e3d5c19aafa6367783bca3ced9a72e446dfa077fee81

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'net/https'
require 'json'

module Godaddy
  class APIError < StandardError; end

  class Api
    def initialize(apikey, apisecret, api_url = "https://api.godaddy.com")
      @api_url = api_url
      @headers = {
        'Authorization' => "sso-key #{apikey}:#{apisecret}",
        'Content-type' => 'application/json'
      }
    end

    [:get, :post, :put, :patch, :delete].each do |method|
      define_method method do |uri, payload = nil|
        uri = URI(@api_url + uri)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        request = build_request method, uri, payload
        response = http.request(request)
        begin
          result = JSON.parse(response.body)
        rescue
          result = response.body
        end

        fail APIError, result unless response.is_a?(Net::HTTPSuccess)

        result
      end
    end

    private

    def build_request(method, uri, payload = nil)
      if payload
        method == :get ? uri.query = URI.encode_www_form(payload) : body = payload.to_json
      end
      request = Net::HTTP.const_get(method.capitalize).new uri, @headers
      request.body = body if body
      request
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
godaddy-api-0.1.2 lib/godaddy/api.rb