embulk-output-mailchimp 0.3.4 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 743479cc26e1bed837531a34c605df1f27ae1ee8
4
- data.tar.gz: 5d6af1f50147f6a79447efd2ba118862088b959d
3
+ metadata.gz: a306f852f0eea3bd2d95ef46dcff661f63c5f3bf
4
+ data.tar.gz: e6be5cde31d3ade89061b961cbc7bc0fd80ac257
5
5
  SHA512:
6
- metadata.gz: 7c79489c197c389155db2a6ef243b78152bc9f51069d81d88c3dd13502d55b254c1de6e6519e93f597619422897888914bad66778cdda4e3b50ac8ebb8ab1183
7
- data.tar.gz: ccc953a7406261e40cacc0881b4bafe81564381b718781588b870f3502a738d59df19a6b59f31f14253598d62807177c09ccca29c55f85ecd78e9624d3a406bd
6
+ metadata.gz: d0daa9b8299e3858e6c5347f1ec72fafb580eb88b61ffd905dc8180e5179b5aec27925804f7f3ade25a02b5fc23ffdf5900d4b771f55a393254be5e13e2d73de
7
+ data.tar.gz: d3b0c68f30be686c2fbd092d6e14ec455562ee9d7da198e319c7bfcb68a9c8457a0a672533ba7d0195ed90951ef0f4f6df0721074d073e5b206a068f72214806
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.6
2
+ - Removed log query and add API to check address type [#22](https://github.com/treasure-data/embulk-output-mailchimp/pull/22)
3
+ - Enabled log query for logging on console temporary [#21](https://github.com/treasure-data/embulk-output-mailchimp/pull/21)
4
+
1
5
  ## 0.3.4 - 2017-06-01
2
6
  - Enable JSON type for `address` MERGE field [#20](https://github.com/treasure-data/embulk-output-mailchimp/pull/20)
3
7
 
@@ -8,19 +12,11 @@
8
12
  - Rename `interest_categories` to `grouping_columns` to fix backward compatibility [#18](https://github.com/treasure-data/embulk-output-mailchimp/pull/18)
9
13
 
10
14
  ## 0.3.1 - 2017-05-26
11
- - Enable merge fields with case-insensitive [#17](https://github.com/treasure-data/embulk-output-mailchimp/pull/17)
12
-
13
- ## 0.3.0.4 - 2017-05-24
14
- - Fixed compatible with API v2 and changed data format [#16](https://github.com/treasure-data/embulk-output-mailchimp/pull/16)
15
-
16
- ## 0.3.0.3 - 2017-05-18
17
- - Fixed bug can not extract data center when use `api_key` as `auth_mode` [#15](https://github.com/treasure-data/embulk-output-mailchimp/pull/15)
18
-
19
- ## 0.3.0.2 - 2017-05-18
20
- - Enabled double_optin in configuration and use default status if schema has no column `status` [#15](https://github.com/treasure-data/embulk-output-mailchimp/pull/15)
21
-
22
- ## 0.3.0.1 - 2017-05-11
23
15
  - Enabled API v3 and supported OAuth2 beside API Key[#13](https://github.com/treasure-data/embulk-output-mailchimp/pull/13)
16
+ - Enabled double_optin in configuration and use default status if schema has no column `status` [#15](https://github.com/treasure-data/embulk-output-mailchimp/pull/15)
17
+ - Fixed bug can not extract data center when use `api_key` as `auth_mode` [#15](https://github.com/treasure-data/embulk-output-mailchimp/pull/15)
18
+ - Fixed compatible with API v2 and changed data format [#16](https://github.com/treasure-data/embulk-output-mailchimp/pull/16)
19
+ - Enable merge fields with case-insensitive [#17](https://github.com/treasure-data/embulk-output-mailchimp/pull/17)
24
20
 
25
21
  ## 0.2.3 - 2016-03-24
26
22
 
data/build.gradle CHANGED
@@ -18,7 +18,7 @@ configurations {
18
18
  provided
19
19
  }
20
20
 
21
- version = "0.3.4"
21
+ version = "0.3.6"
22
22
 
23
23
  sourceCompatibility = 1.7
24
24
  targetCompatibility = 1.7
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
6
6
  import com.fasterxml.jackson.databind.JsonNode;
7
7
  import com.fasterxml.jackson.databind.ObjectMapper;
8
8
  import com.fasterxml.jackson.databind.node.JsonNodeFactory;
9
+ import com.fasterxml.jackson.databind.node.NullNode;
9
10
  import com.fasterxml.jackson.databind.node.ObjectNode;
10
11
  import com.google.common.base.Function;
11
12
  import com.google.common.base.Throwables;
@@ -16,6 +17,7 @@ import org.embulk.base.restclient.record.ServiceRecord;
16
17
  import org.embulk.config.TaskReport;
17
18
  import org.embulk.output.mailchimp.model.ErrorResponse;
18
19
  import org.embulk.output.mailchimp.model.InterestResponse;
20
+ import org.embulk.output.mailchimp.model.MergeField;
19
21
  import org.embulk.output.mailchimp.model.ReportResponse;
20
22
  import org.embulk.spi.Column;
21
23
  import org.embulk.spi.DataException;
@@ -32,9 +34,9 @@ import java.util.Map;
32
34
 
33
35
  import static org.embulk.output.mailchimp.helper.MailChimpHelper.containsCaseInsensitive;
34
36
  import static org.embulk.output.mailchimp.helper.MailChimpHelper.fromCommaSeparatedString;
37
+ import static org.embulk.output.mailchimp.helper.MailChimpHelper.toJsonNode;
35
38
  import static org.embulk.output.mailchimp.model.MemberStatus.PENDING;
36
39
  import static org.embulk.output.mailchimp.model.MemberStatus.SUBSCRIBED;
37
- import static org.embulk.spi.type.Types.JSON;
38
40
 
39
41
  /**
40
42
  * Created by thangnc on 4/14/17.
@@ -55,6 +57,7 @@ public abstract class MailChimpAbstractRecordBuffer
55
57
  private long totalCount;
56
58
  private List<JsonNode> records;
57
59
  private Map<String, Map<String, InterestResponse>> categories;
60
+ private Map<String, MergeField> availableMergeFields;
58
61
 
59
62
  /**
60
63
  * Instantiates a new Mail chimp abstract record buffer.
@@ -146,10 +149,19 @@ public abstract class MailChimpAbstractRecordBuffer
146
149
  * @return the object node
147
150
  */
148
151
  ObjectNode processSubcribers(final List<JsonNode> data, final MailChimpOutputPluginDelegate.PluginTask task)
152
+ throws JsonProcessingException
149
153
  {
150
154
  LOG.info("Start to process subscriber data");
151
- extractDataCenterBasedOnAuthMethod();
152
- extractInterestCategories();
155
+ // Extract data center from meta data URL
156
+ String dc = extractDataCenter(task);
157
+ mailchimpEndpoint = MessageFormat.format(mailchimpEndpoint, dc);
158
+
159
+ // Should loop the names and get the id of interest categories.
160
+ // The reason why we put categories validation here because we can not share data between instance.
161
+ categories = extractInterestCategoriesByGroupNames(task);
162
+
163
+ // Extract merge fields detail
164
+ availableMergeFields = extractMergeFieldsFromList(task);
153
165
 
154
166
  // Required merge fields
155
167
  Map<String, String> map = new HashMap<>();
@@ -229,29 +241,15 @@ public abstract class MailChimpAbstractRecordBuffer
229
241
  abstract String extractDataCenter(final MailChimpOutputPluginDelegate.PluginTask task)
230
242
  throws JsonProcessingException;
231
243
 
232
- private void extractDataCenterBasedOnAuthMethod()
233
- {
234
- try {
235
- // Extract data center from meta data URL
236
- String dc = extractDataCenter(task);
237
- mailchimpEndpoint = MessageFormat.format(mailchimpEndpoint, dc);
238
- }
239
- catch (JsonProcessingException jpe) {
240
- throw new DataException(jpe);
241
- }
242
- }
243
-
244
- private void extractInterestCategories()
245
- {
246
- try {
247
- // Should loop the names and get the id of interest categories.
248
- // The reason why we put categories validation here because we can not share data between instance.
249
- categories = extractInterestCategoriesByGroupNames(task);
250
- }
251
- catch (JsonProcessingException jpe) {
252
- throw new DataException(jpe);
253
- }
254
- }
244
+ /**
245
+ * Extract all merge fields from MailChimp list.
246
+ *
247
+ * @param task the task
248
+ * @return the map
249
+ * @throws JsonProcessingException the json processing exception
250
+ */
251
+ abstract Map<String, MergeField> extractMergeFieldsFromList(final MailChimpOutputPluginDelegate.PluginTask task)
252
+ throws JsonProcessingException;
255
253
 
256
254
  private Function<JsonNode, JsonNode> contactMapper(final Map<String, String> allowColumns)
257
255
  {
@@ -273,11 +271,20 @@ public abstract class MailChimpAbstractRecordBuffer
273
271
  if (task.getMergeFields().isPresent() && !task.getMergeFields().get().isEmpty()) {
274
272
  for (final Column column : schema.getColumns()) {
275
273
  if (!"".equals(containsCaseInsensitive(column.getName(), task.getMergeFields().get()))) {
276
- if (column.getType().equals(JSON)) {
277
- mergeFields.set(column.getName().toUpperCase(), input.findValue(column.getName()));
274
+ String value = input.findValue(column.getName()).asText();
275
+
276
+ // Try to convert to Json from string with the merge field's type is address
277
+ if (availableMergeFields.get(column.getName()).getType()
278
+ .equals(MergeField.MergeFieldType.ADDRESS.getType())) {
279
+ JsonNode addressNode = toJsonNode(value);
280
+ if (addressNode instanceof NullNode) {
281
+ mergeFields.put(column.getName().toUpperCase(), value);
282
+ }
283
+ else {
284
+ mergeFields.set(column.getName().toUpperCase(), addressNode);
285
+ }
278
286
  }
279
287
  else {
280
- String value = input.findValue(column.getName()).asText();
281
288
  mergeFields.put(column.getName().toUpperCase(), value);
282
289
  }
283
290
  }
@@ -15,6 +15,8 @@ import org.embulk.output.mailchimp.model.ErrorResponse;
15
15
  import org.embulk.output.mailchimp.model.InterestCategoriesResponse;
16
16
  import org.embulk.output.mailchimp.model.InterestResponse;
17
17
  import org.embulk.output.mailchimp.model.InterestsResponse;
18
+ import org.embulk.output.mailchimp.model.MergeField;
19
+ import org.embulk.output.mailchimp.model.MergeFields;
18
20
  import org.embulk.output.mailchimp.model.MetaDataResponse;
19
21
  import org.embulk.output.mailchimp.model.ReportResponse;
20
22
  import org.embulk.spi.Exec;
@@ -156,6 +158,17 @@ public class MailChimpRecordBuffer extends MailChimpAbstractRecordBuffer
156
158
  }
157
159
  }
158
160
 
161
+ @Override
162
+ Map<String, MergeField> extractMergeFieldsFromList(MailChimpOutputPluginDelegate.PluginTask task) throws JsonProcessingException
163
+ {
164
+ String endpoint = MessageFormat.format(mailchimpEndpoint + "/lists/{0}/merge-fields",
165
+ task.getListId());
166
+ JsonNode response = client.sendRequest(endpoint, HttpMethod.GET, task);
167
+ MergeFields mergeFields = getMapper().treeToValue(response,
168
+ MergeFields.class);
169
+ return convertMergeFieldToMap(mergeFields.getMergeFields());
170
+ }
171
+
159
172
  private Map<String, InterestResponse> convertInterestCategoryToMap(final List<InterestResponse> interestResponseList)
160
173
  {
161
174
  Function<InterestResponse, String> function = new Function<InterestResponse, String>()
@@ -171,4 +184,21 @@ public class MailChimpRecordBuffer extends MailChimpAbstractRecordBuffer
171
184
  .toList(),
172
185
  function);
173
186
  }
187
+
188
+ private Map<String, MergeField> convertMergeFieldToMap(final List<MergeField> mergeFieldList)
189
+ {
190
+ Function<MergeField, String> function = new Function<MergeField, String>()
191
+ {
192
+ @Nullable
193
+ @Override
194
+ public String apply(@Nullable MergeField input)
195
+ {
196
+ return input.getTag().toLowerCase();
197
+ }
198
+ };
199
+
200
+ return Maps.uniqueIndex(FluentIterable.from(mergeFieldList)
201
+ .toList(),
202
+ function);
203
+ }
174
204
  }
@@ -1,6 +1,10 @@
1
1
  package org.embulk.output.mailchimp.helper;
2
2
 
3
+ import com.fasterxml.jackson.core.JsonParser;
4
+ import com.fasterxml.jackson.databind.DeserializationFeature;
3
5
  import com.fasterxml.jackson.databind.JsonNode;
6
+ import com.fasterxml.jackson.databind.ObjectMapper;
7
+ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
4
8
  import com.google.common.base.Function;
5
9
  import com.google.common.base.Splitter;
6
10
  import com.google.common.collect.Lists;
@@ -9,6 +13,7 @@ import com.google.common.collect.Multimaps;
9
13
 
10
14
  import javax.annotation.Nullable;
11
15
 
16
+ import java.io.IOException;
12
17
  import java.util.List;
13
18
 
14
19
  /**
@@ -76,9 +81,28 @@ public final class MailChimpHelper
76
81
  * @param string the string
77
82
  * @return the list
78
83
  */
79
- public static List<String> fromCommaSeparatedString(String string)
84
+ public static List<String> fromCommaSeparatedString(final String string)
80
85
  {
81
86
  Iterable<String> split = Splitter.on(",").omitEmptyStrings().trimResults().split(string);
82
87
  return Lists.newArrayList(split);
83
88
  }
89
+
90
+ /**
91
+ * TODO: td-worker automatically converts Presto json type to Embulk string type. This is wordaround to convert String to JsonNode
92
+ *
93
+ * @param string the string
94
+ * @return the json node
95
+ */
96
+ public static JsonNode toJsonNode(final String string)
97
+ {
98
+ final ObjectMapper mapper = new ObjectMapper()
99
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
100
+ .configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, false);
101
+ try {
102
+ return mapper.readTree(string);
103
+ }
104
+ catch (IOException e) {
105
+ return JsonNodeFactory.instance.nullNode();
106
+ }
107
+ }
84
108
  }
@@ -0,0 +1,102 @@
1
+ package org.embulk.output.mailchimp.model;
2
+
3
+ import com.fasterxml.jackson.annotation.JsonCreator;
4
+ import com.fasterxml.jackson.annotation.JsonProperty;
5
+ import org.embulk.config.ConfigException;
6
+
7
+ /**
8
+ * Created by thangnc on 5/8/17.
9
+ */
10
+ public class MergeField
11
+ {
12
+ @JsonProperty("merge_id")
13
+ private int mergeId;
14
+
15
+ private String name;
16
+ private String tag;
17
+ private String type;
18
+
19
+ public int getMergeId()
20
+ {
21
+ return mergeId;
22
+ }
23
+
24
+ public void setMergeId(int mergeId)
25
+ {
26
+ this.mergeId = mergeId;
27
+ }
28
+
29
+ public String getName()
30
+ {
31
+ return name;
32
+ }
33
+
34
+ public void setName(String name)
35
+ {
36
+ this.name = name;
37
+ }
38
+
39
+ public String getTag()
40
+ {
41
+ return tag;
42
+ }
43
+
44
+ public void setTag(String tag)
45
+ {
46
+ this.tag = tag;
47
+ }
48
+
49
+ public String getType()
50
+ {
51
+ return type;
52
+ }
53
+
54
+ public void setType(String type)
55
+ {
56
+ this.type = type;
57
+ }
58
+
59
+ public enum MergeFieldType
60
+ {
61
+ TEXT("text"), NUMBER("number"), ADDRESS("address"), PHONE("phone"), DATE("date"), URL("url"),
62
+ IMAGEURL("imageurl"), RADIO("radio"), DROPDOWN("dropdown"), BIRTHDAY("birthday"), ZIP("zip");
63
+
64
+ private String type;
65
+
66
+ MergeFieldType(String type)
67
+ {
68
+ this.type = type;
69
+ }
70
+
71
+ /**
72
+ * Gets type.
73
+ *
74
+ * @return the type
75
+ */
76
+ public String getType()
77
+ {
78
+ return type;
79
+ }
80
+
81
+ /**
82
+ * Find by type auth method.
83
+ *
84
+ * @param type the type
85
+ * @return the auth method
86
+ */
87
+ @JsonCreator
88
+ public static MergeFieldType findByType(final String type)
89
+ {
90
+ for (MergeFieldType method : values()) {
91
+ if (method.getType().equals(type.toLowerCase())) {
92
+ return method;
93
+ }
94
+ }
95
+
96
+ throw new ConfigException(
97
+ String.format("Unknown merge field type '%s'. Supported targets are [text, number, address, phone, " +
98
+ "date, url, imageurl, radio, dropdown, birthday, zip]",
99
+ type));
100
+ }
101
+ }
102
+ }
@@ -0,0 +1,24 @@
1
+ package org.embulk.output.mailchimp.model;
2
+
3
+ import com.fasterxml.jackson.annotation.JsonProperty;
4
+
5
+ import java.util.List;
6
+
7
+ /**
8
+ * Created by thangnc on 5/8/17.
9
+ */
10
+ public class MergeFields
11
+ {
12
+ @JsonProperty("merge_fields")
13
+ private List<MergeField> mergeFields;
14
+
15
+ public List<MergeField> getMergeFields()
16
+ {
17
+ return mergeFields;
18
+ }
19
+
20
+ public void setMergeFields(List<MergeField> mergeFields)
21
+ {
22
+ this.mergeFields = mergeFields;
23
+ }
24
+ }
@@ -2,6 +2,8 @@ package org.embulk.output.mailchimp;
2
2
 
3
3
  import com.fasterxml.jackson.databind.JsonNode;
4
4
  import com.fasterxml.jackson.databind.node.JsonNodeFactory;
5
+ import com.fasterxml.jackson.databind.node.NullNode;
6
+ import com.fasterxml.jackson.databind.node.ObjectNode;
5
7
  import com.google.common.collect.Multimap;
6
8
  import org.embulk.output.mailchimp.helper.MailChimpHelper;
7
9
  import org.junit.Test;
@@ -13,6 +15,7 @@ import java.util.List;
13
15
  import static org.embulk.output.mailchimp.helper.MailChimpHelper.containsCaseInsensitive;
14
16
  import static org.embulk.output.mailchimp.helper.MailChimpHelper.extractMemberStatus;
15
17
  import static org.embulk.output.mailchimp.helper.MailChimpHelper.maskEmail;
18
+ import static org.embulk.output.mailchimp.helper.MailChimpHelper.toJsonNode;
16
19
  import static org.junit.Assert.assertArrayEquals;
17
20
  import static org.junit.Assert.assertEquals;
18
21
 
@@ -63,4 +66,20 @@ public class TestMailChimpHelper
63
66
  assertEquals("Length should match", expect.length, separatedString.size());
64
67
  assertArrayEquals("Should match", expect, separatedString.toArray());
65
68
  }
69
+
70
+ @Test
71
+ public void test_toJsonNode_validJsonString()
72
+ {
73
+ String given = "{\"addr1\":\"1234\",\"city\":\"mountain view\",\"country\":\"US\",\"state\":\"CA\",\"zip\":\"95869\"}";
74
+ String expect = "US";
75
+
76
+ assertEquals("Should be Json", ObjectNode.class, toJsonNode(given).getClass());
77
+ assertEquals("Should have attribute `country`", expect, toJsonNode(given).get("country").asText());
78
+ }
79
+
80
+ @Test
81
+ public void test_toJsonNode_invalidJSonString()
82
+ {
83
+ assertEquals("Should be NullNode", NullNode.class, toJsonNode("abc").getClass());
84
+ }
66
85
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-output-mailchimp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thang Nguyen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-01 00:00:00.000000000 Z
11
+ date: 2017-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -74,6 +74,8 @@ files:
74
74
  - src/main/java/org/embulk/output/mailchimp/model/InterestResponse.java
75
75
  - src/main/java/org/embulk/output/mailchimp/model/InterestsResponse.java
76
76
  - src/main/java/org/embulk/output/mailchimp/model/MemberStatus.java
77
+ - src/main/java/org/embulk/output/mailchimp/model/MergeField.java
78
+ - src/main/java/org/embulk/output/mailchimp/model/MergeFields.java
77
79
  - src/main/java/org/embulk/output/mailchimp/model/MetaDataResponse.java
78
80
  - src/main/java/org/embulk/output/mailchimp/model/ReportResponse.java
79
81
  - src/main/java/org/embulk/output/mailchimp/validation/ColumnDataValidator.java
@@ -86,7 +88,7 @@ files:
86
88
  - test/override_assert_raise.rb
87
89
  - test/run-test.rb
88
90
  - classpath/embulk-base-restclient-0.5.0.jar
89
- - classpath/embulk-output-mailchimp-0.3.4.jar
91
+ - classpath/embulk-output-mailchimp-0.3.6.jar
90
92
  - classpath/embulk-util-retryhelper-jetty92-0.5.0.jar
91
93
  - classpath/jetty-client-9.2.14.v20151106.jar
92
94
  - classpath/jetty-http-9.2.14.v20151106.jar