Class: GetResponse::Connection
- Inherits:
-
Object
- Object
- GetResponse::Connection
- Defined in:
- lib/get_response/connection.rb
Overview
Simple class that simulates connection to service
Direct Known Subclasses
Constant Summary collapse
- API_URI =
"http://api2.getresponse.com/"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#account ⇒ Object
Get basic info about your account.
-
#campaigns ⇒ Object
Method returns proxy to execute all campaign related operations.
-
#confirmation_bodies ⇒ ConfirmationBodyProxy
Method returnx proxy to execute all confirmation body related operations.
-
#confirmation_subjects ⇒ ConfirmationSubjectProxy
Method returnx proxy to execute all confirmation subject related operations.
-
#contacts ⇒ Object
Method returns proxy to execute all contact related operations.
-
#initialize(api_key) ⇒ Connection
constructor
A new instance of Connection.
-
#links ⇒ LinksProxy
Method return proxy to execute all links related operations.
-
#messages ⇒ Object
Method returns proxy to execute all message related operations.
-
#ping ⇒ Object
Test connection with API.
-
#send_request(method, params = {}) ⇒ Object
Send request to JSON-RPC service.
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_key ⇒ Object (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
#account ⇒ Object
Get basic info about your account.
- returns
-
GetResponse::Account
30 31 32 33 |
# File 'lib/get_response/connection.rb', line 30 def account resp = self.send_request("get_account_info") GetResponse::Account.new(resp["result"], self) end |
#campaigns ⇒ Object
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_bodies ⇒ ConfirmationBodyProxy
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_subjects ⇒ ConfirmationSubjectProxy
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 |
#contacts ⇒ Object
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 |
#links ⇒ LinksProxy
Method return proxy to execute all links related operations.
106 107 108 |
# File 'lib/get_response/connection.rb', line 106 def links @links_proxy ||= LinksProxy.new(self) end |
#messages ⇒ Object
Method returns proxy to execute all message related operations.
- returns
-
GetResponse::MessageProxy
55 56 57 |
# File 'lib/get_response/connection.rb', line 55 def @message_proxy ||= GetResponse::MessageProxy.new(self) end |
#ping ⇒ Object
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
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 |