Sha256: 82a6e58eea7f4baca92184036b2965e20e04ffa5364af50ffdfbf35c97cc95bf

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'net/https'
require 'json'

module Godaddy
  class APIError < StandardError; end

  class Api
    API_URL = 'https://api.godaddy.com'

    def initialize(apikey, apisecret)
      @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)
        result = JSON.parse(response.body)

        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.0 lib/godaddy/api.rb