Skip to content

Instantly share code, notes, and snippets.

@seban
Created February 12, 2010 12:25
Show Gist options
  • Save seban/302521 to your computer and use it in GitHub Desktop.
Save seban/302521 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/http'
require 'json'
module GetResponse
class Connection
API_URI = "http://api2.getresponse.com"
attr_reader :api_key
def initialize(api_key)
@api_key = api_key
end
# Test connection with API.
#
# returns:: Boolean
def ping
result = self.send_request("ping")
return result["errors"].nil?
end
protected
# Send request to JSON-RPC service.
#
# method:: String
#
# params:: Hash
def send_request(method, params = {})
request_params = {
:method => method,
:params => [@api_key]
}.to_json
uri = URI.parse(API_URI)
resp = Net::HTTP.start(uri.host, uri.port) do |conn|
conn.post("/", request_params)
end
JSON.parse(resp.body)
end
end
end
# Usage:
> con = GetResponse::Connection.new('secret_api_key')
=> #<GetResponse::Connection:0x1018e5628 @api_key="secret_api_key">
> con.ping
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment