Class: GetResponse::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/get_response/connection.rb

Overview

Simple class that simulates connection to service

Constant Summary collapse

API_URI =
"http://api2.getresponse.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Connection

Returns a new instance of Connection.



11
12
13
# File 'lib/get_response/connection.rb', line 11

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/get_response/connection.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#accountObject

Get basic info about your account.

returns

GetResponse::Account



28
29
30
31
# File 'lib/get_response/connection.rb', line 28

def 
  resp = self.send_request("get_account_info")
  GetResponse::Account.new(resp["result"], self)
end

#campaignsObject

Method returns proxy to execute all campaign related operations.

returns

GetResponse::CampaignProxy



37
38
39
# File 'lib/get_response/connection.rb', line 37

def campaigns
  @campaign_proxy ||= GetResponse::CampaignProxy.new(self)
end

#confirmation_bodiesConfirmationBodyProxy

Method returnx proxy to execute all confirmation body related operations.



61
62
63
# File 'lib/get_response/connection.rb', line 61

def confirmation_bodies
  @confirmation_body_proxy ||= GetResponse::ConfirmationBodyProxy.new(self)
end

#confirmation_subjectsConfirmationSubjectProxy

Method returnx proxy to execute all confirmation subject related operations.



69
70
71
# File 'lib/get_response/connection.rb', line 69

def confirmation_subjects
  @confirmation_subject_proxy ||= GetResponse::ConfirmationSubjectProxy.new(self)
end

#contactsObject

Method returns proxy to execute all contact related operations.

returns

GetResponse::ContactProxy



45
46
47
# File 'lib/get_response/connection.rb', line 45

def contacts
  @contact_proxy ||= GetResponse::ContactProxy.new(self)
end

#messagesObject

Method returns proxy to execute all message related operations.

returns

GetResponse::MessageProxy



53
54
55
# File 'lib/get_response/connection.rb', line 53

def messages
  @message_proxy ||= GetResponse::MessageProxy.new(self)
end

#pingObject

Test connection with API.

returns

Boolean



19
20
21
22
# File 'lib/get_response/connection.rb', line 19

def ping
  result = self.send_request("ping")
  return result["errors"].nil?
end

#send_request(method, params = {}) ⇒ Object

Send request to JSON-RPC service.

method

String

params

Hash

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/get_response/connection.rb', line 79

def send_request(method, params = {})
  request_params = {
    :method => method,
    :params => [@api_key, params]
  }.to_json

  uri = URI.parse(API_URI)
  resp = Net::HTTP.start(uri.host, uri.port) do |conn|
    conn.post("/", request_params)
  end
  raise GetResponseError.new("API key verification failed") if resp.code.to_i == 403
  response = JSON.parse(resp.body)
  if response["error"]
    raise GetResponse::GetResponseError.new(response["error"])
  end
  response
end