Class: GetResponse::Connection

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

Overview

Simple class that simulates connection to service

Direct Known Subclasses

DedicatedPlConnection, DedicatedUsConnection

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
14
15
# File 'lib/get_response/connection.rb', line 11

def initialize(api_key)
  @api_key = api_key
  @request_id_prefix = "#{Time.now.to_i}-#{rand(1_000_000_000)}"
  @request_number = -1
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



30
31
32
33
# File 'lib/get_response/connection.rb', line 30

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



39
40
41
# File 'lib/get_response/connection.rb', line 39

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

#confirmation_bodiesConfirmationBodyProxy

Method returnx proxy to execute all confirmation body related operations.



63
64
65
# File 'lib/get_response/connection.rb', line 63

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

#confirmation_subjectsConfirmationSubjectProxy

Method returnx proxy to execute all confirmation subject related operations.



71
72
73
# File 'lib/get_response/connection.rb', line 71

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

#contactsObject

Method returns proxy to execute all contact related operations.

returns

GetResponse::ContactProxy



47
48
49
# File 'lib/get_response/connection.rb', line 47

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

Method return proxy to execute all links related operations.

Returns:



106
107
108
# File 'lib/get_response/connection.rb', line 106

def links
  @links_proxy ||= LinksProxy.new(self)
end

#messagesObject

Method returns proxy to execute all message related operations.

returns

GetResponse::MessageProxy



55
56
57
# File 'lib/get_response/connection.rb', line 55

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

#pingObject

Test connection with API.

returns

Boolean



21
22
23
24
# File 'lib/get_response/connection.rb', line 21

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:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/get_response/connection.rb', line 81

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

  uri = URI.parse(self.class::API_URI)
  resp = Net::HTTP.start(uri.host, uri.port) do |conn|
    puts uri.inspect
    conn.post(uri.path, request_params)
  end
  raise GetResponseError.new("API key verification failed") if resp.code.to_i == 403
  raise GetResponseError.new("204 No content response received which signifies interpreting request as notification") if resp.code.to_i == 204
  response = JSON.parse(resp.body)
  if response["error"]
    raise GetResponse::GetResponseError.new(response["error"])
  end
  response
end