emarsys 0.2.1 → 0.2.2

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: cbb69af7c3e5a8574c609396438fb0c8bd98669a
4
- data.tar.gz: 7624b2f1909182375b5373462240b4947a02f6a1
3
+ metadata.gz: dc2e05dfded9bc3e85a484b0a100dd0bbee2c4c3
4
+ data.tar.gz: c6a286becd3fb612d9e379f794430e5f8fa6740c
5
5
  SHA512:
6
- metadata.gz: 859b05d96793b5fec1071afbb5a6fae628ab0829c9e1739e6a7c98185301b3bfa4dca0749b40bc4b93c0e8b92ebdfce450c4221461e0e6b6b04195a4e48c45d3
7
- data.tar.gz: defafe1ddfa3e4ff987bd271e415276ce280d60206d0fbbd3b9bd4bf3831b9440de42cd75f89e50d2df0e99a87eaf64d6a4d14eb48389d6c317084b416c912ea
6
+ metadata.gz: a27a34fa7afd4d4e182dfa6e6d8948490f8d095cacca5c3fc018d1a45c699588e91ee75fe193ac395c88654f7876e23f73e9df3c0412150d2c5dce05c26556dd
7
+ data.tar.gz: c344e9354da2b659f2edb4243d9b5e8a8abce6a124b4b83886bd4ac210cfe2ae564038ef62e639d549529f38ce4988342dc5e414cd0091694d163e5f1314dd4d
data/README.md CHANGED
@@ -208,6 +208,15 @@ Please refer to the code for detailed instructions of each method.
208
208
 
209
209
  ### HEAD (not yet released)
210
210
 
211
+ ### v0.2.2
212
+
213
+ * Allow batch updates to create missing contacts ([#22](https://github.com/Absolventa/emarsys-rb/pull/22))
214
+ * Add support for the data export API ([#23]((https://github.com/Absolventa/emarsys-rb/pull/23))
215
+
216
+ ### v0.2.1
217
+
218
+ * Added basic support for rate-limiting response from Emarsys API ([#21](https://github.com/Absolventa/emarsys-rb/pull/21))
219
+
211
220
  ### v0.2.0
212
221
  * Added country mapping ([#17](https://github.com/Absolventa/emarsys-rb/pull/17))
213
222
  * Proper encoding of GET parameters ([#11](https://github.com/Absolventa/emarsys-rb/pull/11))
@@ -38,13 +38,15 @@ module Emarsys
38
38
  # @param key_id [Integer, String] internal id of key field
39
39
  # @param key_value [Integer, String] value of interal id field
40
40
  # @param params [Hash] Contact information to update
41
+ # @param create_if_not_exists [Boolean] Whether to create contact if it does not exist
41
42
  # @return [Hash] internal id of the contact
42
43
  # @example
43
44
  # Emarsys::Contact.update('app_id', 23, {:firstname => "Jon", :lastname => "Doe"})
44
- # Emarsys::Contact.update('3', 'john.doe@example.com', {'1' => "Jon", '2' => "Doe"})
45
- def update(key_id, key_value, params = {})
45
+ # Emarsys::Contact.update('3', 'john.doe@example.com', {'1' => "Jon", '2' => "Doe"}, true)
46
+ def update(key_id, key_value, params = {}, create_if_not_exists = false)
47
+ path = "contact#{create_if_not_exists ? '/?create_if_not_exists=1' : ''}"
46
48
  transformed_key_id = transform_key_id(key_id)
47
- put "contact", params.merge!({'key_id' => transformed_key_id, transformed_key_id => key_value})
49
+ put path, params.merge!({'key_id' => transformed_key_id, transformed_key_id => key_value})
48
50
  end
49
51
 
50
52
  # Batch creation of contacts.
@@ -66,15 +68,19 @@ module Emarsys
66
68
  #
67
69
  # @param key_id [Integer, String] internal id of key field
68
70
  # @param params [Hash] Contact information of each new contact
71
+ # @param create_if_not_exists [Boolean] Whether to create non-existing contacts
69
72
  # @example
70
73
  # Emarsys::Contact.update_batch(
71
- # 'email', {:app_id => 1, :firstname => "Jon", :lastname => "Doe"},
72
- # {:app_id => 2, :firstname => "Jane", :lastname => "Doe"}
74
+ # 'email',
75
+ # [{:email => "john@example.com", :firstname => "Jon", :lastname => "Doe"},
76
+ # {:email => "jane@example.com", :firstname => "Jane", :lastname => "Doe"}],
77
+ # true
73
78
  # )
74
79
  #
75
80
  # TODO params should be parameterized with field mappings
76
- def update_batch(key_id, params = [])
77
- put "contact", {'key_id' => transform_key_id(key_id), 'contacts' => params}
81
+ def update_batch(key_id, params = [], create_if_not_exists = false)
82
+ path = "contact#{create_if_not_exists ? '/?create_if_not_exists=1' : ''}"
83
+ put path, {'key_id' => transform_key_id(key_id), 'contacts' => params}
78
84
  end
79
85
 
80
86
  # Get list of emails send to a contact
@@ -125,9 +125,33 @@ module Emarsys
125
125
  raise "Not implemented yet"
126
126
  end
127
127
 
128
- # TODO POST /getresponses
129
- def export_responses(params = {})
130
- raise "Not implemented yet"
128
+ # Exports the selected fields of all contacts who responded to emails
129
+ # within the specified time range.
130
+ #
131
+ # @param distribution_method [String] ftp or local
132
+ # @param time_range [array] Array with two elements (start date, end date)
133
+ # @param contact_fields [array] Array of contact field IDs
134
+ # @param sources [array] Array which defines sources
135
+ # @param analysis_fields [array] Array that defines the contact behaviours to analyse
136
+ # @option params [hash]
137
+ # @return [Hash] Result data
138
+ # @example
139
+ # Emarsys::Email.export_responses(
140
+ # 'local',
141
+ # ['2012-02-09', '2014-08-13'],
142
+ # [1, 3],
143
+ # ['trackable_links'],
144
+ # [5, 8, 13]
145
+ # )
146
+ def export_responses(distribution_method, time_range, contact_fields, sources, analysis_fields, params = {})
147
+ params.merge!(
148
+ :distribution_method => distribution_method,
149
+ :time_range => time_range,
150
+ :contact_fields => Emarsys::ParamsConverter.new(contact_fields).convert_to_ids,
151
+ :sources => sources,
152
+ :analysis_fields => analysis_fields
153
+ )
154
+ post "email/getresponses", params
131
155
  end
132
156
  end
133
157
  end
@@ -16,6 +16,21 @@ module Emarsys
16
16
  def resource(id)
17
17
  get "export/#{id}", {}
18
18
  end
19
+
20
+ # Download export data
21
+ #
22
+ # @param id [Integer, String] The internal emarsys id
23
+ # @option offset [Integer] Defines the ID to start listing from
24
+ # @option limit [Integer] Defines how many IDs are listed
25
+ # @return [String] text/csv
26
+ # @example
27
+ # Emarsys::Export.data(2)
28
+ def data(id, offset = nil, limit = nil)
29
+ params = {}
30
+ params.merge!(:offset => offset) if offset
31
+ params.merge!(:limit => limit) if limit
32
+ get "export/#{id}/data", params
33
+ end
19
34
  end
20
35
 
21
36
  end
@@ -9,22 +9,37 @@ module Emarsys
9
9
  end
10
10
 
11
11
  def convert_to_ids
12
+ params.is_a?(Hash) ? convert_hash_to_ids : convert_array_to_ids
13
+ end
14
+
15
+ def convert_to_identifiers
12
16
  new_hash = {}
13
17
  params.each do |key, value|
14
- matching_attributes = Emarsys::FieldMapping::ATTRIBUTES.find{|elem| elem[:identifier] == key.to_s}
15
- new_hash.merge!({ (matching_attributes.nil? ? key : matching_attributes[:id]) => value })
18
+ matching_attributes = Emarsys::FieldMapping::ATTRIBUTES.find{|elem| elem[:id] == key.to_i && key.to_i != 0}
19
+ new_hash.merge!({ (matching_attributes.nil? ? key : matching_attributes[:identifier]) => value })
16
20
  end
17
21
  new_hash
18
22
  end
19
23
 
20
- def convert_to_identifiers
24
+ private
25
+
26
+ def convert_hash_to_ids
21
27
  new_hash = {}
22
28
  params.each do |key, value|
23
- matching_attributes = Emarsys::FieldMapping::ATTRIBUTES.find{|elem| elem[:id] == key.to_i && key.to_i != 0}
24
- new_hash.merge!({ (matching_attributes.nil? ? key : matching_attributes[:identifier]) => value })
29
+ matching_attributes = Emarsys::FieldMapping::ATTRIBUTES.find{|elem| elem[:identifier] == key.to_s}
30
+ new_hash.merge!({ (matching_attributes.nil? ? key : matching_attributes[:id]) => value })
25
31
  end
26
32
  new_hash
27
33
  end
28
34
 
35
+ def convert_array_to_ids
36
+ new_array = []
37
+ params.each do |key|
38
+ matching_attributes = Emarsys::FieldMapping::ATTRIBUTES.find{|elem| elem[:identifier] == key.to_s}
39
+ new_array << (matching_attributes.nil? ? key : matching_attributes[:id])
40
+ end
41
+ new_array
42
+ end
43
+
29
44
  end
30
45
  end
@@ -4,10 +4,15 @@ module Emarsys
4
4
  attr_accessor :code, :text, :data, :status
5
5
 
6
6
  def initialize(response)
7
- json = JSON.parse(response)
8
- self.code = json['replyCode']
9
- self.text = json['replyText']
10
- self.data = json['data']
7
+ if response.headers[:content_type] == 'text/csv'
8
+ self.code = 0
9
+ self.data = response.body
10
+ else
11
+ json = JSON.parse(response)
12
+ self.code = json['replyCode']
13
+ self.text = json['replyText']
14
+ self.data = json['data']
15
+ end
11
16
  self.status = response.code if response.respond_to?(:code)
12
17
  end
13
18
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Emarsys
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
@@ -25,6 +25,12 @@ describe Emarsys::Contact do
25
25
  Emarsys::Contact.update(3, 'jane.doe@example.com', stub_params)
26
26
  expect(stub).to have_been_requested.once
27
27
  end
28
+ it "may create non-existing contact" do
29
+ stub_params = {1 => 'Jane', 2 => "Doe"}
30
+ stub = stub_request(:put, "https://api.emarsys.net/api/v2/contact/?create_if_not_exists=1").with(:body => stub_params.merge!({'key_id' => 3, 3 => 'jane.doe@example.com'}).to_json).to_return(standard_return_body)
31
+ Emarsys::Contact.update(3, 'jane.doe@example.com', stub_params, true)
32
+ expect(stub).to have_been_requested.once
33
+ end
28
34
  end
29
35
 
30
36
  describe ".create_batch" do
@@ -43,6 +49,12 @@ describe Emarsys::Contact do
43
49
  Emarsys::Contact.update_batch(3, stub_params)
44
50
  expect(stub).to have_been_requested.once
45
51
  end
52
+ it "may create non-existing contacts" do
53
+ stub_params = [{1 => 'Jane', 2 => "Doe"}, {1 => 'Paul', 2 => 'Tester'}]
54
+ stub = stub_request(:put, "https://api.emarsys.net/api/v2/contact/?create_if_not_exists=1").with(:body => {'key_id' => 3, 'contacts' => stub_params}.to_json).to_return(standard_return_body)
55
+ Emarsys::Contact.update_batch(3, stub_params, true)
56
+ expect(stub).to have_been_requested.once
57
+ end
46
58
  end
47
59
 
48
60
  describe ".contact_history" do
@@ -8,4 +8,12 @@ describe Emarsys::Export do
8
8
  ).to have_been_requested.once
9
9
  end
10
10
  end
11
+
12
+ describe ".data" do
13
+ it "requests export data" do
14
+ expect(
15
+ stub_get('export/123/data') { Emarsys::Export.data(123) }
16
+ ).to have_been_requested.once
17
+ end
18
+ end
11
19
  end
@@ -2,18 +2,54 @@ require 'spec_helper'
2
2
 
3
3
  describe Emarsys::Response do
4
4
 
5
+ class FakeResponse
6
+ attr_accessor :body
7
+
8
+ def initialize(body)
9
+ self.body = body
10
+ end
11
+
12
+ def to_str
13
+ body
14
+ end
15
+
16
+ module JSON
17
+ # Value taken from an actual response from Emarsys
18
+ def headers
19
+ {:content_type => 'text/html; charset=utf-8'}
20
+ end
21
+ end
22
+
23
+ module CSV
24
+ def headers
25
+ {:content_type => 'text/csv'}
26
+ end
27
+ end
28
+ end
29
+
5
30
  describe '#initialize' do
6
- it 'sets code, text and data attributes on initialize' do
7
- response_string = "{\"replyCode\":0,\"replyText\":\"Something\",\"data\":1}"
8
- response = Emarsys::Response.new(response_string)
9
- expect(response.code).to eq(0)
10
- expect(response.text).to eq("Something")
11
- expect(response.data).to eq(1)
31
+ context "json" do
32
+ let(:response_string) { "{\"replyCode\":0,\"replyText\":\"Something\",\"data\":1}" }
33
+ let(:response) { Emarsys::Response.new(FakeResponse.new(response_string).extend(FakeResponse::JSON)) }
34
+ it 'sets code, text and data attributes on initialize' do
35
+ expect(response.code).to eq(0)
36
+ expect(response.text).to eq("Something")
37
+ expect(response.data).to eq(1)
38
+ end
39
+ end
40
+ context "csv" do
41
+ let(:response_string) { "user_id,Vorname,E-Mail,Version Name,Url,Zeit\r\n" }
42
+ let(:response) { Emarsys::Response.new(FakeResponse.new(response_string).extend(FakeResponse::CSV)) }
43
+ it 'sets code and data attributes on initialize' do
44
+ expect(response.code).to eq(0)
45
+ expect(response.data).to eq("user_id,Vorname,E-Mail,Version Name,Url,Zeit\r\n")
46
+ end
12
47
  end
13
48
  end
14
49
 
15
50
  describe '#result' do
16
- let(:response) { Emarsys::Response.new("{\"replyCode\":0,\"replyText\":\"Something\",\"data\":1}") }
51
+ let(:response_string) { "{\"replyCode\":0,\"replyText\":\"Something\",\"data\":1}" }
52
+ let(:response) { Emarsys::Response.new(FakeResponse.new(response_string).extend(FakeResponse::JSON)) }
17
53
 
18
54
  it "returns data if code is 0" do
19
55
  allow(response).to receive(:code).and_return(0)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emarsys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schoppmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-02 00:00:00.000000000 Z
11
+ date: 2017-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client