expresspigeon-ruby 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3531f1f4bb4e7b172a5529c14fb7ff4135fae0f
4
- data.tar.gz: 7656345b121e756d514a4827815d917d4f9015a3
3
+ metadata.gz: 740eda4d7d4f1e572ce3d256f24f5b076b891d00
4
+ data.tar.gz: 5a7c7f792fedced6f59fda76d4f8a40cfd162a7f
5
5
  SHA512:
6
- metadata.gz: e523bae70bab3fb65377dd6336bba0542ad8ac0780b62e240187bd1153f277aefca9b55c6992fcb45f54515642311f775b14e1d4ca310bbb8a76da85599dfddd
7
- data.tar.gz: a7b7c9626ea4a95469d8b3b9ab6a07c647c1a5fa50c5cbab530fab6a4f4e249a6544283467a0b1b03c516cba41b56a49768bb3b4c8cbd814558693567fa689c8
6
+ metadata.gz: af4c0be7a36f27623981947e6b2e7c9cb9fcc72024d84fa3dab1a81b2c29c8d2596aca5bf4a9b5a652e0ee5936b323ad37d22152616ec015a346ff417dcdd5b0
7
+ data.tar.gz: f52d89c356545ef134463f3ddd89b3fb6536f065ee28c251c110d9d1f7b3d13b49f047f1f2981378f7a0223c5369dd350e8adcf69fb0bbbe4bdfcbb7a219413d
data/.gitignore CHANGED
@@ -3,7 +3,12 @@
3
3
  .idea/
4
4
 
5
5
 
6
- rspec.sh
6
+ scripts/*
7
+
7
8
  set_env
8
9
 
9
10
  *.txt
11
+
12
+ test/*
13
+
14
+ expresspigeon-ruby-*.gem
@@ -0,0 +1 @@
1
+ ruby-2.1.1
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- expresspigeon-ruby (0.0.5)
5
- rest-client (~> 1.8.0)
4
+ expresspigeon-ruby (0.1.1)
5
+ rest-client (~> 2.0.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -14,10 +14,10 @@ GEM
14
14
  domain_name (~> 0.5)
15
15
  mime-types (2.99.3)
16
16
  netrc (0.11.0)
17
- rest-client (1.8.0)
17
+ rest-client (2.0.2)
18
18
  http-cookie (>= 1.0.2, < 2.0)
19
- mime-types (>= 1.16, < 3.0)
20
- netrc (~> 0.7)
19
+ mime-types (>= 1.16, < 4.0)
20
+ netrc (~> 0.8)
21
21
  rspec (2.14.1)
22
22
  rspec-core (~> 2.14.0)
23
23
  rspec-expectations (~> 2.14.0)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Expresspigeon::Ruby
2
2
 
3
- This is a Ruby library for convenince access to [ExpressPigeon API](https://expresspigeon.com/api).
3
+ This is a Ruby library for convenient access to [ExpressPigeon API](https://expresspigeon.com/api).
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,11 +16,10 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install expresspigeon-ruby
18
18
 
19
- ## Usage
19
+ ## Sending a simple message
20
20
 
21
21
  Sending a transactional message is easy:
22
22
 
23
-
24
23
  ```ruby
25
24
  MESSAGES = ExpressPigeon::API.messages.auth_key 'XXX'
26
25
  message_response = MESSAGES.send_message 115, # template ID
@@ -40,6 +39,50 @@ sleep 5
40
39
  puts MESSAGES.report message_response.id
41
40
  ```
42
41
 
42
+ ## Sending a message with attachments
43
+
44
+ ```ruby
45
+ MESSAGES = ExpressPigeon::API.messages.auth_key(ENV['AUTH_KEY'])
46
+
47
+ attachments = %W{attachments/attachment1.txt attachments/smile.pdf attachments/example.ics}
48
+
49
+ puts MESSAGES.send_message(
50
+ 123, # template_id
51
+ 'john@doe.com', #to
52
+ 'jane@doe.com', #reply_to
53
+ "Jane Doe", #from
54
+ "Want to get out for a dinner?", #subject
55
+ {first_name: 'John', main_course: 'stake'}, #merge_fields
56
+ false, #view_online
57
+ true, #click_tracking
58
+ true, #suppress_address
59
+ attachments #file paths to upload as attachments
60
+ )
61
+
62
+ ```
63
+
64
+ ## Sending a message with all required and optional arguments
65
+
66
+ ```ruby
67
+
68
+ MESSAGES = ExpressPigeon::API.messages.auth_key(ENV['AUTH_KEY'])
69
+
70
+ attachments = %W{attachments/attachment1.txt attachments/calendar.ics}
71
+
72
+ puts MESSAGES.send_msg 123, 'john@doe.com', 'jane@doe.com',
73
+ 'Jane Doe', 'A simple test subject',
74
+ merge_fields: { first_name: "John" },
75
+ view_online: false,
76
+ click_tracking: true,
77
+ suppress_address: false,
78
+ attachments: attachments,
79
+ headers: { Xtest: "test" },
80
+ reply_name: "Jane S. Doe",
81
+ from_address: "jane+123@doe.com"
82
+
83
+ ```
84
+
85
+ The first five arguments are mandatory, while the rest are optional.
43
86
 
44
87
  ## Contributing
45
88
 
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_runtime_dependency 'rest-client', '~> 1.8.0'
21
-
20
+ gem.add_runtime_dependency 'rest-client', '~> 2.0.2'
22
21
  gem.add_development_dependency "rspec", "~> 2.6"
22
+ gem.licenses = ['MIT']
23
23
  end
@@ -8,7 +8,9 @@ require 'rest_client'
8
8
  module ExpressPigeon
9
9
 
10
10
  AUTH_KEY = ENV['EXPRESSPIGEON_AUTH_KEY']
11
- ROOT = 'https://api.expresspigeon.com/'
11
+
12
+ # EXPRESSPIGEON_ROOT env var is used to point to testenv
13
+ ROOT = ENV.has_key?('EXPRESSPIGEON_ROOT') ? ENV['EXPRESSPIGEON_ROOT'] : 'https://api.expresspigeon.com/'
12
14
  USE_SSL = true
13
15
 
14
16
  module API
@@ -76,11 +78,16 @@ module ExpressPigeon
76
78
  ) do |http|
77
79
  http.request req
78
80
  end
79
- parsed = JSON.parse(resp.body)
80
- if parsed.kind_of? Hash
81
- MetaResponse.new parsed
81
+
82
+ if resp.content_type == 'application/json'
83
+ parsed = JSON.parse(resp.body)
84
+ if parsed.kind_of? Hash
85
+ MetaResponse.new parsed
86
+ else
87
+ parsed
88
+ end
82
89
  else
83
- parsed
90
+ resp.body
84
91
  end
85
92
  end
86
93
  end
@@ -121,7 +128,7 @@ module ExpressPigeon
121
128
  Messages.new
122
129
  end
123
130
 
124
- def self.auto_responders
131
+ def self.autoresponders
125
132
  AutoResponders.new
126
133
  end
127
134
 
@@ -12,10 +12,8 @@ module ExpressPigeon
12
12
  #
13
13
  # Returns an array of autoresponders.
14
14
  #
15
- # Docs: https://expresspigeon.com/api#auto_responders_get_all
16
- #
17
15
  def all
18
- get endpoint
16
+ get @endpoint
19
17
  end
20
18
 
21
19
  # Start for a contact
@@ -25,10 +23,9 @@ module ExpressPigeon
25
23
  # :param auto_responder_id: autoresponder id to be started for a contact
26
24
  # :param email: contact email
27
25
  #
28
- # Docs: https://expresspigeon.com/api#auto_responders_start
29
26
  #
30
27
  def start(auto_responder_id, email)
31
- post "#{endpoint}/#{auto_responder_id}/start", email: email
28
+ post "#{@endpoint}/#{auto_responder_id}/start", email: email
32
29
  end
33
30
 
34
31
  # Stop for a contact
@@ -41,7 +38,45 @@ module ExpressPigeon
41
38
  # Docs: https://expresspigeon.com/api#auto_responders_stop
42
39
  #
43
40
  def stop(auto_responder_id, email)
44
- post "#{endpoint}/#{auto_responder_id}/stop", email: email
41
+ post "#{@endpoint}/#{auto_responder_id}/stop", email: email
42
+ end
43
+
44
+ # Reports for a single responder
45
+ #
46
+ # :param auto_responder_id: autoresponder id to be stopped for a contact
47
+ # :param email: contact email
48
+ #
49
+ def report(auto_responder_id)
50
+ get "#{@endpoint}/#{auto_responder_id}"
51
+ end
52
+
53
+
54
+ # Reports bounces for autoresponder part
55
+ #
56
+ # :param auto_responder_id: autoresponder id to be stopped for a contact
57
+ # :param auto_responder_part_id: id of the autoresponder part in questions
58
+ #
59
+ def bounced(auto_responder_id, autoresponder_part_id)
60
+ get "#{@endpoint}/#{auto_responder_id}/#{autoresponder_part_id}/bounced"
61
+ end
62
+
63
+ # Reports unsubscribed for autoresponder part
64
+ #
65
+ # :param auto_responder_id: autoresponder id to be stopped for a contact
66
+ # :param auto_responder_part_id: id of the autoresponder part in questions
67
+ #
68
+ def unsubscribed(auto_responder_id, autoresponder_part_id)
69
+ get "#{@endpoint}/#{auto_responder_id}/#{autoresponder_part_id}/unsubscribed"
70
+ end
71
+
72
+
73
+ # Get spam reports for autoresponder part
74
+ #
75
+ # :param auto_responder_id: autoresponder id to be stopped for a contact
76
+ # :param auto_responder_part_id: id of the autoresponder part in questions
77
+ #
78
+ def spam(auto_responder_id, autoresponder_part_id)
79
+ get "#{@endpoint}/#{auto_responder_id}/#{autoresponder_part_id}/spam"
45
80
  end
46
81
  end
47
82
  end
@@ -14,6 +14,14 @@ module ExpressPigeon
14
14
  get "#{@endpoint}/#{campaign_id}"
15
15
  end
16
16
 
17
+ def opened(campaign_id)
18
+ get "#{@endpoint}/#{campaign_id}/opened"
19
+ end
20
+
21
+ def clicked(campaign_id)
22
+ get "#{@endpoint}/#{campaign_id}/clicked"
23
+ end
24
+
17
25
  def bounced(campaign_id)
18
26
  get "#{@endpoint}/#{campaign_id}/bounced"
19
27
  end
@@ -11,6 +11,7 @@ module ExpressPigeon
11
11
  end
12
12
 
13
13
 
14
+
14
15
  # JSON document represents a contact to be created or updated.
15
16
  # The email field is required.
16
17
  # When updating a contact, list_id is optional,
@@ -33,6 +33,13 @@ module ExpressPigeon
33
33
  end
34
34
 
35
35
 
36
+ #
37
+ #:returns: status of upload
38
+ #
39
+ def upload_status(list_id)
40
+ get "#{@endpoint}/upload_status/#{list_id}"
41
+ end
42
+
36
43
  ##
37
44
  # Removes a list with a given id. A list must be enabled and has no dependent subscriptions and/or scheduled campaigns.
38
45
  #
@@ -7,19 +7,57 @@ module ExpressPigeon
7
7
  @endpoint = 'messages'
8
8
  end
9
9
 
10
- def send_message(template_id, to, reply_to, from_name, subject, merge_fields = nil, view_online = false, click_tracking = true)
11
- post @endpoint, params = {template_id: template_id, :to => to, reply_to: reply_to, :from => from_name, :subject => subject,
12
- :merge_fields => merge_fields, :view_online => view_online, :click_tracking => click_tracking}
10
+ # Sends a single transactional message.
11
+ #
12
+ # For more information on arguments, see: [https://expresspigeon.com/kb/transactional-send].
13
+ #
14
+ def send_message(template_id, to, reply_to, from, subject, merge_fields = nil, view_online = false,
15
+ click_tracking = true, suppress_address = false, attachments = nil, headers = nil)
16
+ if attachments
17
+ upload(template_id, to, reply_to, from, subject, merge_fields, view_online, click_tracking,
18
+ suppress_address, attachments, headers, nil, nil)
19
+ else
20
+ post @endpoint, params = {template_id: template_id, to: to, reply_to: reply_to, from: from,
21
+ subject: subject, merge_fields: merge_fields, view_online: view_online,
22
+ click_tracking: click_tracking, suppress_address: suppress_address, headers: headers}
23
+ end
13
24
  end
14
25
 
26
+
27
+ # Sends a single transactional message.
28
+ #
29
+ # For more information on arguments, see: [https://expresspigeon.com/kb/transactional-send].
30
+
31
+ # This method allows to specify different 'reply_to', 'reply_name', 'from' and 'from_address' values.
32
+ #
33
+ def send_msg(template_id, to, reply_to, from, subject, optional = {})
34
+
35
+ if optional[:attachments]
36
+ upload(template_id, to, reply_to, from, subject, optional[:merge_fields], optional[:view_online], optional[:click_tracking],
37
+ optional[:suppress_address], optional[:attachments], optional[:headers], optional[:reply_name], optional[:from_address])
38
+ else
39
+ params = optional ? optional.clone : {}
40
+ params[:template_id] = template_id
41
+ params[:to] = to
42
+ params[:reply_to] = reply_to
43
+ params[:from] = from
44
+ params[:subject] = subject
45
+
46
+ post @endpoint, params
47
+ end
48
+ end
49
+
50
+ # Retrieve report for a single message.
51
+ #
52
+ # * +message_id+ - ID of a message sent previously
15
53
  def report(message_id)
16
54
  get "#{@endpoint}/#{message_id}"
17
55
  end
18
56
 
57
+ # Report for a group of messages in a given time period.
19
58
  #
20
- #
21
- # start_date is instance of Time
22
- # end_date is instance of Time
59
+ # * +start_date+ is instance of Time
60
+ # * +end_date+ is instance of Time
23
61
  def reports(from_id, start_date = nil, end_date = nil)
24
62
  params = []
25
63
 
@@ -46,7 +84,75 @@ module ExpressPigeon
46
84
  end
47
85
 
48
86
  get query
87
+ end
88
+
89
+
90
+ # Sends a transactional message with attachments using ExpressPigeon Rest API.
91
+ # This method is not used directly, instead use +send_message()+ or +send_msg()+
92
+ #
93
+ # * +template_id+ - ID of a template to use for sending
94
+ # * +to+ - destination email address
95
+ # * +reply_to+ - return email address
96
+ # * +from+ - name of sender
97
+ # * +subject+ - subject of email
98
+ # * +merge fields+ - hash with dynamic values to merge into a template
99
+ # * +view_online+ - generate "view online" link in the template
100
+ # * +click_tracking+ - enable/disable click tracking (and URL rewriting)
101
+ # * +suppress_address+ - enable/disable display of physical address at the bottom of newsletter.
102
+ # * +attachments+ - array of file paths to attach to email.
103
+ # * +headers+ - hash of headers to add to SMTP message
104
+ # * +reply_name+ - reply name to use in the +reply-to+ header.
105
+ # * +from_name+ - name to use in the +from+ header
106
+ #
107
+ def upload(template_id, to, reply_to, from, subject, merge_fields, view_online, click_tracking, suppress_address, attachments, headers, reply_name, from_address)
108
+ path = "#{@root ? @root : ROOT}/#{@endpoint}"
109
+ begin
110
+ payload = prepare_payload(template_id, to, reply_to, from, subject, merge_fields, view_online, click_tracking, suppress_address, attachments, headers, reply_name, from_address)
111
+ request = RestClient::Request.new(
112
+ :method => :post,
113
+ :headers => {:'X-auth-key' => get_auth_key},
114
+ :url => path,
115
+ :payload => payload)
116
+ resp = request.execute
117
+ res = resp.body
118
+ rescue RestClient::ExceptionWithResponse => err
119
+ res = err.response
120
+ end
121
+
122
+ parsed = JSON.parse(res)
123
+ if parsed.kind_of? Hash
124
+ MetaResponse.new parsed
125
+ else
126
+ parsed
127
+ end
128
+ end
129
+
130
+
131
+ def prepare_payload(template_id, to, reply_to, from, subject, merge_fields, view_online, click_tracking, suppress_address, attachments, headers,
132
+ reply_name, from_address)
133
+ payload = { multipart: true }
134
+ payload[:template_id] = template_id
135
+ payload[:to] = to
136
+ payload[:reply_to] = reply_to
137
+ payload[:subject] = subject
138
+ payload[:from] = from
139
+ payload[:merge_fields] = merge_fields.to_json
140
+ payload[:headers] = headers.to_json
141
+ payload[:view_online] = view_online
142
+ payload[:click_tracking] = click_tracking
143
+ payload[:suppress_address] = suppress_address
144
+ payload[:reply_name] = reply_name
145
+ payload[:from_address] = from_address
49
146
 
147
+ attachments.each { |attachment|
148
+ if File.file?(attachment)
149
+ file = File.basename(attachment)
150
+ payload[file] = File.new attachment
151
+ else
152
+ raise "File #{attachment} does not exist"
153
+ end
154
+ }
155
+ payload
50
156
  end
51
157
  end
52
158
  end
@@ -15,7 +15,7 @@ module ExpressPigeon
15
15
  @delegate[m.to_s]
16
16
  end
17
17
 
18
- def respond_to?(m)
18
+ def respond_to?(m, include_all = false)
19
19
  @delegate.key?(m.to_s)
20
20
  end
21
21
 
@@ -1,5 +1,5 @@
1
1
  module Expresspigeon
2
2
  module API
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -0,0 +1,46 @@
1
+ require './lib/expresspigeon-ruby'
2
+ require 'pigeon_helper'
3
+
4
+ describe 'autoresponders integration test' do
5
+
6
+ include PigeonSpecHelper
7
+
8
+ it 'should return all autoresponders' do
9
+ res = ExpressPigeon::API.autoresponders.all
10
+ res.class.should == Array
11
+ res.size.should > 0
12
+ puts res
13
+ end
14
+
15
+ it 'should start autoresponder by id' do
16
+ res = ExpressPigeon::API.autoresponders.start 672, 'igor@polevoy.org'
17
+ puts res
18
+ end
19
+
20
+ it 'should stop autoresponder by id' do
21
+ res = ExpressPigeon::API.autoresponders.stop 672, 'igor@polevoy.org'
22
+ puts res
23
+ end
24
+
25
+ it 'should get single report' do
26
+ res = ExpressPigeon::API.autoresponders.report 672
27
+ puts res
28
+ end
29
+
30
+ it 'should get bounces for autoresponder part' do
31
+ res = ExpressPigeon::API.autoresponders.bounced 672, 746
32
+ puts res
33
+ end
34
+
35
+ it 'should get spam for autoresponder part' do
36
+ res = ExpressPigeon::API.autoresponders.spam 672, 746
37
+ puts res
38
+ end
39
+
40
+ it 'should get unsubscribed for autoresponder part' do
41
+ res = ExpressPigeon::API.autoresponders.unsubscribed 672, 746
42
+ puts res
43
+ end
44
+
45
+ end
46
+