getresponse 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,94 @@
1
+ == GetResponse
2
+
3
+ Wrapper for GetResponse API
4
+
5
+ == Usage
6
+
7
+ Test connection to GetResponse API.
8
+
9
+ gr_connection = GetResponse::Connection.new("my_secret_api")
10
+ gr_connection.ping
11
+
12
+ Get info about account
13
+
14
+ gr_connection.get_account_info
15
+
16
+ Get yout active campaings. More info about operators http://dev.getresponse.com/api-doc/#operators
17
+
18
+ # get campaings with name "My campaing name"
19
+ gr_connection.get_campaings(:name.is_eq => "My campaing name")
20
+
21
+ # get campaings with other name than "My campaing name"
22
+ gr_connection.get_campaings(:name.is_not_eq => "My campaing name")
23
+
24
+ # get campaings with name like "signup" and with from_email with domain "mybiz.xx"
25
+ gr_connection.get_campaigns(:name.contain => "%signup%", :from_email.contain => "%mybiz.xx"
26
+
27
+ Get single campaign
28
+
29
+ gr_connection.get_campaign(123456)
30
+ => <#GetResponse::Campaign ... >
31
+
32
+ gr_connection.get_campaign(bad_id)
33
+ => nil
34
+
35
+ You can operate on your contacts quite the same way as on ActiveRecord objects. Before any operation
36
+ on contacts you must connect to API.
37
+
38
+ Get all contacts:
39
+
40
+ # connect to API
41
+ GetResponse::Connection.instance("my_secret_api_key")
42
+ GetResponse::Contact.find_all
43
+
44
+ Create new contact:
45
+
46
+ # with connection
47
+ GetResponse::Contact.create("name" => "John Doe", "email" => "john.d@somewhere.com",
48
+ "campaign" => "campaignId", "customs" => { "source" => "mainpage" })
49
+
50
+ Update your contact:
51
+
52
+ # with connection
53
+ # contact - existing contact
54
+ contact.update("name" => "John Thenewname")
55
+
56
+ Delete contact:
57
+
58
+ # with connection
59
+ # contact - existing contact
60
+ contact.destroy
61
+
62
+ Move contact between campaigns:
63
+
64
+ # with connection
65
+ # contact - existing contact
66
+ # zXy - existing campaign
67
+ contact.move("zXy")
68
+
69
+
70
+ Get geoip location
71
+
72
+ # with connection
73
+ # contact - existing contact
74
+ contact.geoip
75
+
76
+ Set contact cycle
77
+
78
+ # with connection
79
+ # contact - existing contact
80
+ contact.set_cycle(5)
81
+
82
+ Get messages in account:
83
+
84
+ # get messages assigned to campaign with "N3i" ID
85
+ gr_connection.get_messages(:campaigns => ["N3i"])
86
+
87
+ # get messages with newsletter type
88
+ gr_connection.get_messages(:type => "newsletter")
89
+
90
+ # get messages from from campaigns with name like "my_biz"
91
+ gr_connection.get_messages(:get_campaigns => { :name.contain => "%my_biz%" })
92
+
93
+ # get one messages identifier by <tt>id</tt>
94
+ gr_connection.get_message("some_id")
@@ -0,0 +1,51 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup
4
+ Bundler.require(:default)
5
+
6
+ module GetResponse
7
+
8
+ # Operators that may be used in queries to GetResponse API service
9
+ OPERATORS = {
10
+ "is_eq" => "EQUALS",
11
+ "is_not_eq" => "NOT_EQUALS",
12
+ "contain" => "CONTAINS",
13
+ "not_contain" => "NOT_CONTAINS",
14
+ "match" => "MATCHES"
15
+ }
16
+
17
+ end
18
+
19
+
20
+ class SymbolOperator
21
+ attr_reader :field, :operator
22
+
23
+ def initialize(field, operator)
24
+ @field, @operator = field, operator
25
+ end unless method_defined?(:initialize)
26
+
27
+
28
+ def evaluate(value)
29
+ { field.to_s => { gr_operator => value } }
30
+ end
31
+
32
+
33
+ def gr_operator
34
+ GetResponse::OPERATORS[@operator]
35
+ end
36
+ end
37
+
38
+
39
+ class Symbol
40
+ GetResponse::OPERATORS.keys.each do |operator|
41
+ define_method(operator) do
42
+ SymbolOperator.new(self, operator)
43
+ end unless method_defined?(operator)
44
+ end
45
+ end
46
+
47
+
48
+
49
+ Dir.glob(File.join('**/*.rb')).each do |fname|
50
+ GetResponse.autoload File.basename(fname, '.rb').gsub(/(?:^|_)(.)/) { $1.upcase }, fname
51
+ end
@@ -0,0 +1,15 @@
1
+ module GetResponse
2
+
3
+ # GetResponse Account
4
+ class Account
5
+ attr_reader :login, :name, :email, :created_on
6
+
7
+
8
+ def initialize(params)
9
+ @login = params["login"]
10
+ @name = params["from_name"]
11
+ @email = params["from_email"]
12
+ @created_on = params["created_on"]
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module GetResponse
2
+
3
+ # GetResponse email campaign
4
+ class Campaign
5
+ attr_reader :id, :name, :from_name, :from_email, :reply_to_email, :created_on
6
+
7
+
8
+ def initialize(params)
9
+ @id = params["id"]
10
+ @name = params["name"]
11
+ @from_name = params["from_name"]
12
+ @from_email = params["from_email"]
13
+ @reply_to_email = params["reply_to_email"]
14
+ @created_on = params["created_on"]
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,152 @@
1
+ require 'net/http'
2
+
3
+ module GetResponse
4
+
5
+ # Simple class that simulates connection to service
6
+ class Connection
7
+ API_URI = "http://api2.getresponse.com"
8
+
9
+ attr_reader :api_key
10
+
11
+
12
+ private_class_method :new
13
+
14
+
15
+ def initialize(api_key)
16
+ @api_key = api_key
17
+ end
18
+
19
+
20
+ def self.instance(api_key = "")
21
+ @@instance ||= new(api_key)
22
+ end
23
+
24
+
25
+ # Test connection with API.
26
+ #
27
+ # returns:: Boolean
28
+ def ping
29
+ result = self.send_request("ping")
30
+ return result["errors"].nil?
31
+ end
32
+
33
+
34
+ # Get basic info about your account
35
+ #
36
+ # returns:: GetResponse::Account
37
+ def get_account_info
38
+ resp = self.send_request("get_account_info")
39
+ GetResponse::Account.new(resp["result"])
40
+ end
41
+
42
+
43
+ # Get list of active campaigns in account. There are allowed operators for building conditions.
44
+ # More info about operators http://dev.getresponse.com/api-doc/#operators
45
+ #
46
+ # gr_connection.get_campaings
47
+ #
48
+ # Get list of all active campaigns with name "my_campaign" and from_email is from domain "mybiz.xx"
49
+ #
50
+ # gr_connection.get_campaings(:name.is_eq => "my_campaign", :from_email.contain => "%mybiz.xx")
51
+ #
52
+ # get_campaings(:name.eq => "my name")
53
+ def get_campaigns(conditions = {})
54
+ req_cond = build_conditions(conditions)
55
+
56
+ response = send_request("get_campaigns", req_cond)["result"]
57
+ response.inject([]) do |campaings, resp|
58
+ campaings << Campaign.new(resp[1].merge("id" => resp[0]))
59
+ end
60
+ end
61
+
62
+
63
+ # Get single campaign using <tt>campaign_id</tt>.
64
+ #
65
+ # campaign_id:: Integer || String
66
+ #
67
+ # returns:: GetResponse::Campaign || nil
68
+ def get_campaign(campaign_id)
69
+ result = self.get_campaigns(:id.is_eq => campaign_id)
70
+ result.first
71
+ end
72
+
73
+
74
+ # TODO: untested!
75
+ # Get messages in account.
76
+ # Conditions:
77
+ # * campaigns / get_campaigns (optional) – Search only in given campaigns. Uses OR logic.
78
+ # If those params are not given search, is performed in all campaigns in the account.
79
+ # Check IDs in conditions for detailed explanation.
80
+ # * type (optional) – Use newsletter or follow-up to narrow down search results to specific
81
+ # message types.
82
+ # * subject (optional) – Use text operators to narrow down search results to specific message subjects.
83
+ #
84
+ # conditions:: Hash
85
+ #
86
+ # returns:: [Message]
87
+ def get_messages(conditions = {})
88
+ req_cond = build_conditions(conditions)
89
+
90
+ response = send_request("get_messages", req_cond)["result"]
91
+ response.inject([]) do |messages, resp|
92
+ messages << Message.new(resp[1].merge("id" => resp[0]))
93
+ end
94
+ end
95
+
96
+
97
+ # Get single message using <tt>message_id</tt>.
98
+ #
99
+ # message_id:: Integer || String
100
+ #
101
+ # returns:: Message
102
+ def get_message(message_id)
103
+ response = self.send_request("get_message", { "message" => message_id })["result"]
104
+ return nil if response.empty?
105
+ Message.new(response[message_id].merge("id" => message_id))
106
+ end
107
+
108
+
109
+ # Send request to JSON-RPC service.
110
+ #
111
+ # method:: String
112
+ #
113
+ # params:: Hash
114
+ def send_request(method, params = {})
115
+ request_params = {
116
+ :method => method,
117
+ :params => [@api_key, params]
118
+ }.to_json
119
+
120
+ uri = URI.parse(API_URI)
121
+ resp = Net::HTTP.start(uri.host, uri.port) do |conn|
122
+ conn.post("/", request_params)
123
+ end
124
+ response = JSON.parse(resp.body)
125
+ if response["error"]
126
+ raise GetResponse::GetResponseError.new(response["error"])
127
+ end
128
+ response
129
+ end
130
+
131
+
132
+ protected
133
+
134
+
135
+ def build_conditions(conditions)
136
+ conditions.inject({}) do |hash, cond|
137
+ if cond[0].respond_to?(:evaluate)
138
+ hash.merge!(cond[0].evaluate(cond[1]))
139
+ else
140
+ if cond[1].instance_of?(Hash)
141
+ hash.merge!(cond[0] => build_conditions(cond[1]))
142
+ else
143
+ hash.merge!(cond[0] => cond[1])
144
+ end
145
+ end
146
+ hash
147
+ end
148
+ end
149
+
150
+ end
151
+
152
+ end
@@ -0,0 +1,145 @@
1
+ module GetResponse
2
+
3
+ # GetResponse contact
4
+ class Contact
5
+ attr_accessor :campaign, :name, :email, :cycle_day, :ip, :customs
6
+ attr_reader :id
7
+
8
+
9
+ def initialize(params)
10
+ @campaign = params["campaign"]
11
+ @name = params["name"]
12
+ @email = params["email"]
13
+ @cycle_day = params["cycle_day"]
14
+ @ip = params["ip"]
15
+ @customs = parse_customs(params["customs"])
16
+ @id = params["id"]
17
+ end
18
+
19
+
20
+ # Add contact to the list. Method raises an exception <tt>GetResponseError</tt> if contact can't
21
+ # be added (bad email syntax for example).
22
+ # Example:
23
+ # GetResponse::Contact.create("email" => "john@doe.org", "name => "John D.", "campaign" => "N67")
24
+ #
25
+ # params:: Hash - attributes for new contact
26
+ # returns:: Boolean - true if contact queued for create
27
+ def self.create(params)
28
+ contact = Contact.new(params)
29
+ contact.save
30
+ end
31
+
32
+
33
+ # Find all contacts associated with GetResponse account
34
+ #
35
+ # returns:: [Contact]
36
+ def self.find_all
37
+ response = GetResponse::Connection.instance.send_request("get_contacts", {})
38
+
39
+ response["result"].inject([]) do |contacts, resp|
40
+ contacts << Contact.new(resp[1].merge("id" => resp[0]))
41
+ end
42
+ end
43
+
44
+
45
+ # Save contact object. When object can't be saved than <tt>GetResponseError</tt> is raised,
46
+ # otherwise returns <tt>true</tt>.
47
+ #
48
+ # returns:: Boolean
49
+ def save
50
+ connection = GetResponse::Connection.instance
51
+
52
+ result = connection.send_request(:add_contact, self.attributes)
53
+ result["error"].nil?
54
+ end
55
+
56
+
57
+ # Returns setted attributes as Hash.
58
+ #
59
+ # returns:: Hash
60
+ def attributes
61
+ attrs = {
62
+ "campaign" => @campaign,
63
+ "name" => @name,
64
+ "email" => @email
65
+ }
66
+
67
+ attrs["ip"] = @ip if @ip
68
+ attrs["cycle_day"] = @cycle_day if @cycle_day
69
+ attrs["customs"] = @customs if @customs
70
+
71
+ attrs
72
+ end
73
+
74
+
75
+ # Delete contact. If deleting contact was successfull method returns <tt>true</tt>. If contact
76
+ # object hasn't <tt>id</tt> attribute <tt>GetResponseError</tt> exception is raised. Exception
77
+ # is raised when try to delete already deleted contact.
78
+ #
79
+ # returns:: Boolean
80
+ def destroy
81
+ raise GetResponse::GetResponseError.new("Can't delete contact without id") unless @id
82
+
83
+ resp = GetResponse::Connection.instance.send_request("delete_contact", { "contact" => @id })
84
+ resp["result"]["deleted"].to_i == 1
85
+ end
86
+
87
+
88
+ # Update contact with passed attributes set. When object can't be saved than <tt>GetResponseError</tt>
89
+ # is raised, otherwise returns <tt>true</tt>.
90
+ #
91
+ # net_attrs:: Hash
92
+ def update(new_attrs)
93
+ new_attrs.each_pair { |key, value| self.send(key + "=", value) }
94
+
95
+ self.save
96
+ end
97
+
98
+
99
+ # Move contact from one campaign (current) to another. If move contact fails (for example: no
100
+ # campaign with passed id) <tt>GetResponseError</tt> exception is raised, otherwise method
101
+ # returns <tt>true</tt>.
102
+ #
103
+ # new_campaign_id:: String - identifier of new camapign
104
+ # returns:: Boolean
105
+ def move(new_campaign_id)
106
+ param = { "contact" => @id, "campaign" => new_campaign_id }
107
+ result = GetResponse::Connection.instance.send_request("move_contact", param)
108
+ result["result"]["updated"].to_i == 1
109
+ end
110
+
111
+
112
+ # Get contact geo location based on IP address from which the subscription was processed.
113
+ #
114
+ # returns:: Hash
115
+ def geoip
116
+ param = { "contact" => @id }
117
+ GetResponse::Connection.instance.send_request("get_contact_geoip", param)["result"]
118
+ end
119
+
120
+
121
+ # Place a contact on a desired day of the follow-up cycle or deactivate a contact. Method
122
+ # raises a <tt>GetResponseError</tt> exception when fails otherwise returns true.
123
+ #
124
+ # value:: String || Fixnum
125
+ # returns:: true
126
+ def set_cycle(value)
127
+ param = { "contact" => @id, "cycle_day" => value }
128
+ GetResponse::Connection.instance.send_request("set_contact_cycle", param)["result"]["updated"].to_i == 1
129
+ end
130
+
131
+
132
+ protected
133
+
134
+
135
+ def parse_customs(customs)
136
+ return unless customs
137
+ customs.inject([]) do |customs_hash, custom|
138
+ customs_hash << { "name" => custom[0], "content" => custom[1] }
139
+ end
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+
@@ -0,0 +1,5 @@
1
+ # Errors for GetResponse. For example: bad requests, ...
2
+ module GetResponse
3
+ class GetResponseError < StandardError
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ module GetResponse
2
+
3
+ # Class representa a message in GetResponse
4
+ class Message
5
+ attr_reader :id, :type, :subject, :day_of_cycle, :flags, :created_on
6
+
7
+ def initialize(params)
8
+ @id = params["id"]
9
+ @type = params["type"]
10
+ @subject = params["subject"]
11
+ @day_of_cycle = params["day_of_cycle"]
12
+ @flags = params["flags"] || []
13
+ @created_on = params["created_on"]
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: getresponse
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Sebastian Nowak
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-19 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 1
31
+ - 4
32
+ version: "1.4"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json_pure
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 1
46
+ - 4
47
+ version: "1.4"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rr
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 15
59
+ segments:
60
+ - 1
61
+ - 0
62
+ version: "1.0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: With this gem you can manage your subscribers, campaigns, messages
66
+ email: sebastian.nowak@implix.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files: []
72
+
73
+ files:
74
+ - lib/get_response/account.rb
75
+ - lib/get_response/campaign.rb
76
+ - lib/get_response/connection.rb
77
+ - lib/get_response/contact.rb
78
+ - lib/get_response/get_response_error.rb
79
+ - lib/get_response/message.rb
80
+ - lib/get_response.rb
81
+ - README.rdoc
82
+ has_rdoc: true
83
+ homepage: http://dev.getresponse.com
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 17
106
+ segments:
107
+ - 1
108
+ - 3
109
+ - 5
110
+ version: 1.3.5
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.7
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Ruby wrapper for GetResponse API
118
+ test_files: []
119
+