mrkt 0.9.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +59 -5
  3. data/.travis.yml +19 -10
  4. data/Gemfile.lock +68 -63
  5. data/LICENSE +21 -0
  6. data/README.md +4 -1
  7. data/lib/mrkt.rb +14 -3
  8. data/lib/mrkt/concerns/authentication.rb +27 -12
  9. data/lib/mrkt/concerns/connection.rb +13 -7
  10. data/lib/mrkt/concerns/crud_activities.rb +12 -9
  11. data/lib/mrkt/concerns/crud_asset_folders.rb +43 -0
  12. data/lib/mrkt/concerns/crud_asset_static_lists.rb +30 -0
  13. data/lib/mrkt/concerns/crud_campaigns.rb +2 -4
  14. data/lib/mrkt/concerns/crud_custom_activities.rb +3 -2
  15. data/lib/mrkt/concerns/crud_custom_objects.rb +13 -14
  16. data/lib/mrkt/concerns/crud_helpers.rb +7 -0
  17. data/lib/mrkt/concerns/crud_leads.rb +35 -20
  18. data/lib/mrkt/concerns/crud_lists.rb +13 -10
  19. data/lib/mrkt/concerns/crud_programs.rb +6 -5
  20. data/lib/mrkt/concerns/import_custom_objects.rb +1 -1
  21. data/lib/mrkt/concerns/import_leads.rb +8 -5
  22. data/lib/mrkt/errors.rb +3 -2
  23. data/lib/mrkt/faraday.rb +4 -0
  24. data/lib/mrkt/faraday/params_encoder.rb +20 -0
  25. data/lib/mrkt/faraday_middleware.rb +3 -7
  26. data/lib/mrkt/version.rb +1 -1
  27. data/mrkt.gemspec +11 -12
  28. data/spec/concerns/authentication_spec.rb +57 -5
  29. data/spec/concerns/crud_activities_spec.rb +46 -7
  30. data/spec/concerns/crud_asset_folders_spec.rb +273 -0
  31. data/spec/concerns/crud_asset_static_lists_spec.rb +183 -0
  32. data/spec/concerns/crud_custom_activities_spec.rb +3 -1
  33. data/spec/concerns/crud_custom_objects_spec.rb +1 -1
  34. data/spec/concerns/crud_leads_spec.rb +104 -2
  35. data/spec/concerns/crud_lists_spec.rb +33 -0
  36. data/spec/concerns/import_leads_spec.rb +3 -3
  37. data/spec/faraday/params_encoder_spec.rb +24 -0
  38. data/spec/spec_helper.rb +3 -1
  39. data/spec/support/webmock.rb +1 -7
  40. metadata +44 -46
@@ -1,29 +1,32 @@
1
1
  module Mrkt
2
2
  module CrudActivities
3
3
  def get_activity_types
4
- get("/rest/v1/activities/types.json")
4
+ get('/rest/v1/activities/types.json')
5
5
  end
6
6
 
7
7
  def get_paging_token(since_datetime)
8
- params = {
9
- sinceDatetime: since_datetime.iso8601
10
- }
11
- get("/rest/v1/activities/pagingtoken.json", params)
8
+ get('/rest/v1/activities/pagingtoken.json', sinceDatetime: since_datetime.iso8601)
12
9
  end
13
10
 
14
11
  def get_activities(next_page_token, activity_type_ids: [], lead_ids: [])
15
12
  params = {
16
13
  nextPageToken: next_page_token
17
14
  }
18
- params[:activityTypeIds] = activity_type_ids.join(',') unless blank?(activity_type_ids)
19
- params[:leadIds] = lead_ids.join(',') unless blank?(lead_ids)
20
- get("/rest/v1/activities.json", params)
15
+
16
+ params[:activityTypeIds] = activity_type_ids unless blank?(activity_type_ids)
17
+ params[:leadIds] = lead_ids unless blank?(lead_ids)
18
+
19
+ get('/rest/v1/activities.json', params)
20
+ end
21
+
22
+ def get_deleted_leads(next_page_token)
23
+ get('/rest/v1/activities/deletedleads.json', nextPageToken: next_page_token)
21
24
  end
22
25
 
23
26
  private
24
27
 
25
28
  def blank?(value)
26
- !value || value == "" || value.empty?
29
+ !value || value == '' || value.empty?
27
30
  end
28
31
  end
29
32
  end
@@ -0,0 +1,43 @@
1
+ module Mrkt
2
+ module CrudAssetFolders
3
+ def create_folder(name, parent, description: nil)
4
+ post('/rest/asset/v1/folders.json') do |req|
5
+ params = {
6
+ name: name,
7
+ parent: JSON.generate(parent)
8
+ }
9
+
10
+ optional = {
11
+ description: description
12
+ }
13
+
14
+ req.body = merge_params(params, optional)
15
+ end
16
+ end
17
+
18
+ def get_folder_by_id(id, type: nil)
19
+ params = {}
20
+ params[:type] = type if type
21
+
22
+ get("/rest/asset/v1/folder/#{id}.json", params)
23
+ end
24
+
25
+ def get_folder_by_name(name, type: nil, root: nil, work_space: nil)
26
+ params = {
27
+ name: name
28
+ }
29
+
30
+ optional = {
31
+ root: root&.to_json,
32
+ type: type,
33
+ workSpace: work_space
34
+ }
35
+
36
+ get('/rest/asset/v1/folder/byName.json', params, optional)
37
+ end
38
+
39
+ def delete_folder(id)
40
+ post("/rest/asset/v1/folder/#{id}/delete.json")
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ module Mrkt
2
+ module CrudAssetStaticLists
3
+ def create_static_list(name, folder, description: nil)
4
+ post('/rest/asset/v1/staticLists.json') do |req|
5
+ params = {
6
+ name: name,
7
+ folder: JSON.generate(folder)
8
+ }
9
+
10
+ optional = {
11
+ description: description
12
+ }
13
+
14
+ req.body = merge_params(params, optional)
15
+ end
16
+ end
17
+
18
+ def get_static_list_by_id(id)
19
+ get("/rest/asset/v1/staticList/#{id}.json")
20
+ end
21
+
22
+ def get_static_list_by_name(name)
23
+ get('/rest/asset/v1/staticList/byName.json', name: name)
24
+ end
25
+
26
+ def delete_static_list(id)
27
+ post("/rest/asset/v1/staticList/#{id}/delete.json")
28
+ end
29
+ end
30
+ end
@@ -1,15 +1,13 @@
1
1
  module Mrkt
2
2
  module CrudCampaigns
3
3
  def request_campaign(id, lead_ids, tokens = {})
4
- post("/rest/v1/campaigns/#{id}/trigger.json") do |req|
5
- params = {
4
+ post_json("/rest/v1/campaigns/#{id}/trigger.json") do
5
+ {
6
6
  input: {
7
7
  leads: map_lead_ids(lead_ids),
8
8
  tokens: tokens
9
9
  }
10
10
  }
11
-
12
- json_payload(req, params)
13
11
  end
14
12
  end
15
13
  end
@@ -19,8 +19,9 @@ module Mrkt
19
19
  primaryAttributeValue: primary_attribute_value,
20
20
  attributes: converted_attributes
21
21
  }]
22
- post('/rest/v1/activities/external.json') do |req|
23
- json_payload(req, input: input)
22
+
23
+ post_json('/rest/v1/activities/external.json') do
24
+ { input: input }
24
25
  end
25
26
  end
26
27
 
@@ -2,51 +2,50 @@ module Mrkt
2
2
  module CrudCustomObjects
3
3
  def get_list_of_custom_objects(names = nil)
4
4
  params = {}
5
- params[:names] = names.join(',') if names
5
+ params[:names] = names if names
6
6
 
7
7
  get('/rest/v1/customobjects.json', params)
8
8
  end
9
9
 
10
10
  def describe_custom_object(name)
11
- fail Mrkt::Errors::Unknown unless name
11
+ raise Mrkt::Errors::Unknown unless name
12
12
 
13
13
  get("/rest/v1/customobjects/#{name}/describe.json")
14
14
  end
15
15
 
16
16
  def createupdate_custom_objects(name, input, action: 'createOrUpdate', dedupe_by: 'dedupeFields')
17
- post("/rest/v1/customobjects/#{name}.json") do |req|
18
- params = {
17
+ post_json("/rest/v1/customobjects/#{name}.json") do
18
+ {
19
19
  input: input,
20
20
  action: action,
21
21
  dedupeBy: dedupe_by
22
22
  }
23
- json_payload(req, params)
24
23
  end
25
24
  end
26
25
 
27
26
  def delete_custom_objects(name, input, delete_by: 'dedupeFields')
28
- post("/rest/v1/customobjects/#{name}/delete.json") do |req|
29
- params = {
27
+ post_json("/rest/v1/customobjects/#{name}/delete.json") do
28
+ {
30
29
  input: input,
31
30
  deleteBy: delete_by
32
31
  }
33
-
34
- json_payload(req, params)
35
32
  end
36
33
  end
37
34
 
38
35
  def get_custom_objects(name, input, filter_type: 'dedupeFields', fields: nil, next_page_token: nil, batch_size: nil)
39
- post("/rest/v1/customobjects/#{name}.json?_method=GET") do |req|
36
+ post_json("/rest/v1/customobjects/#{name}.json?_method=GET") do
40
37
  params = {
41
38
  input: input,
42
39
  filterType: filter_type
43
40
  }
44
41
 
45
- params[:fields] = fields if fields
46
- params[:nextPageToken] = next_page_token if next_page_token
47
- params[:batchSize] = batch_size if batch_size
42
+ optional = {
43
+ fields: fields,
44
+ nextPageToken: next_page_token,
45
+ batchSize: batch_size
46
+ }
48
47
 
49
- json_payload(req, params)
48
+ merge_params(params, optional)
50
49
  end
51
50
  end
52
51
  end
@@ -4,6 +4,13 @@ module Mrkt
4
4
  lead_ids.map { |id| { id: id } }
5
5
  end
6
6
 
7
+ def post_json(url)
8
+ post(url) do |req|
9
+ payload = block_given? ? yield(req) : {}
10
+ json_payload(req, payload)
11
+ end
12
+ end
13
+
7
14
  def json_payload(req, payload)
8
15
  req.headers[:content_type] = 'application/json'
9
16
  req.body = JSON.generate(payload)
@@ -1,28 +1,42 @@
1
1
  module Mrkt
2
2
  module CrudLeads
3
+ def get_lead_by_id(id, fields: nil)
4
+ optional = {
5
+ fields: fields
6
+ }
7
+
8
+ get("/rest/v1/lead/#{id}.json", {}, optional)
9
+ end
10
+
3
11
  def get_leads(filter_type, filter_values, fields: nil, batch_size: nil, next_page_token: nil)
4
12
  params = {
5
13
  filterType: filter_type,
6
- filterValues: filter_values.join(',')
14
+ filterValues: filter_values
7
15
  }
8
- params[:fields] = fields if fields
9
- params[:batchSize] = batch_size if batch_size
10
- params[:nextPageToken] = next_page_token if next_page_token
11
16
 
12
- get('/rest/v1/leads.json', params)
17
+ optional = {
18
+ fields: fields,
19
+ batchSize: batch_size,
20
+ nextPageToken: next_page_token
21
+ }
22
+
23
+ get('/rest/v1/leads.json', params, optional)
13
24
  end
14
25
 
15
26
  def createupdate_leads(leads, action: 'createOrUpdate', lookup_field: nil, partition_name: nil, async_processing: nil)
16
- post('/rest/v1/leads.json') do |req|
27
+ post_json('/rest/v1/leads.json') do
17
28
  params = {
18
29
  action: action,
19
30
  input: leads
20
31
  }
21
- params[:lookupField] = lookup_field if lookup_field
22
- params[:partitionName] = partition_name if partition_name
23
- params[:asyncProcessing] = async_processing if async_processing
24
32
 
25
- json_payload(req, params)
33
+ optional = {
34
+ lookupField: lookup_field,
35
+ partitionName: partition_name,
36
+ asyncProcessing: async_processing
37
+ }
38
+
39
+ merge_params(params, optional)
26
40
  end
27
41
  end
28
42
 
@@ -33,23 +47,24 @@ module Mrkt
33
47
  end
34
48
 
35
49
  def associate_lead(id, cookie)
36
- params = Faraday::Utils::ParamsHash.new
37
- params[:cookie] = cookie
50
+ params = Mrkt::Faraday::ParamsEncoder.encode(cookie: cookie)
38
51
 
39
- post("/rest/v1/leads/#{id}/associate.json?#{params.to_query}") do |req|
40
- json_payload(req, {})
41
- end
52
+ post_json("/rest/v1/leads/#{id}/associate.json?#{params}")
42
53
  end
43
54
 
44
55
  def merge_leads(winning_lead_id, losing_lead_ids, merge_in_crm: false)
45
- params = Faraday::Utils::ParamsHash.new
56
+ params = {}
57
+
46
58
  params[:mergeInCRM] = merge_in_crm
47
- params[:leadIds] = losing_lead_ids.join(',') if losing_lead_ids
59
+ params[:leadIds] = losing_lead_ids if losing_lead_ids
48
60
 
49
- post("/rest/v1/leads/#{winning_lead_id}/merge.json?#{params.to_query}") do |req|
50
- json_payload(req,{})
51
- end
61
+ query_params = Mrkt::Faraday::ParamsEncoder.encode(params)
62
+
63
+ post_json("/rest/v1/leads/#{winning_lead_id}/merge.json?#{query_params}")
52
64
  end
53
65
 
66
+ def describe_lead
67
+ get('/rest/v1/leads/describe.json')
68
+ end
54
69
  end
55
70
  end
@@ -1,21 +1,24 @@
1
1
  module Mrkt
2
2
  module CrudLists
3
3
  def get_leads_by_list(list_id, fields: nil, batch_size: nil, next_page_token: nil)
4
- params = {}
5
- params[:fields] = fields if fields
6
- params[:batchSize] = batch_size if batch_size
7
- params[:nextPageToken] = next_page_token if next_page_token
4
+ optional = {
5
+ fields: fields,
6
+ batchSize: batch_size,
7
+ nextPageToken: next_page_token
8
+ }
8
9
 
9
- get("/rest/v1/list/#{list_id}/leads.json", params)
10
+ get("/rest/v1/list/#{list_id}/leads.json", {}, optional)
10
11
  end
11
12
 
12
13
  def add_leads_to_list(list_id, lead_ids)
13
- post("/rest/v1/lists/#{list_id}/leads.json") do |req|
14
- params = {
15
- input: map_lead_ids(lead_ids)
16
- }
14
+ post_json("/rest/v1/lists/#{list_id}/leads.json") do
15
+ { input: map_lead_ids(lead_ids) }
16
+ end
17
+ end
17
18
 
18
- json_payload(req, params)
19
+ def remove_leads_from_list(list_id, lead_ids)
20
+ delete("/rest/v1/lists/#{list_id}/leads.json") do |req|
21
+ json_payload(req, input: map_lead_ids(lead_ids))
19
22
  end
20
23
  end
21
24
  end
@@ -1,12 +1,13 @@
1
1
  module Mrkt
2
2
  module CrudPrograms
3
3
  def browse_programs(offset: nil, max_return: nil, status: nil)
4
- params = {}
5
- params[:offset] = offset if offset
6
- params[:maxReturn] = max_return if max_return
7
- params[:status] = status if status
4
+ optional = {
5
+ offset: offset,
6
+ maxReturn: max_return,
7
+ status: status
8
+ }
8
9
 
9
- get('/rest/asset/v1/programs.json', params)
10
+ get('/rest/asset/v1/programs.json', {}, optional)
10
11
  end
11
12
 
12
13
  def get_program_by_id(id)
@@ -3,7 +3,7 @@ module Mrkt
3
3
  def import_custom_object(file, custom_object, format = 'csv')
4
4
  params = {
5
5
  format: format,
6
- file: Faraday::UploadIO.new(file, 'text/csv')
6
+ file: ::Faraday::UploadIO.new(file, 'text/csv')
7
7
  }
8
8
 
9
9
  post("/bulk/v1/customobjects/#{custom_object}/import.json", params)
@@ -3,13 +3,16 @@ module Mrkt
3
3
  def import_lead(file, format = 'csv', lookup_field: nil, list_id: nil, partition_name: nil)
4
4
  params = {
5
5
  format: format,
6
- file: Faraday::UploadIO.new(file, 'text/csv')
6
+ file: ::Faraday::UploadIO.new(file, 'text/csv')
7
7
  }
8
- params[:lookupField] = lookup_field if lookup_field
9
- params[:listId] = list_id if list_id
10
- params[:partitionName] = partition_name if partition_name
11
8
 
12
- post('/bulk/v1/leads.json', params)
9
+ optional = {
10
+ lookupField: lookup_field,
11
+ listId: list_id,
12
+ partitionName: partition_name
13
+ }
14
+
15
+ post('/bulk/v1/leads.json', params, optional)
13
16
  end
14
17
 
15
18
  def import_lead_status(id)
@@ -25,6 +25,7 @@ class Mrkt::Errors
25
25
  610 => 'RequestedResourceNotFound',
26
26
  611 => 'System',
27
27
  612 => 'InvalidContentType',
28
+ 702 => 'RecordNotFound',
28
29
  703 => 'DisabledFeature',
29
30
  1001 => 'TypeMismatch',
30
31
  1002 => 'MissingParamater',
@@ -40,9 +41,9 @@ class Mrkt::Errors
40
41
  1012 => 'InvalidCookieValue',
41
42
  1013 => 'ObjectNotFound',
42
43
  1014 => 'FailedToCreateObject'
43
- }
44
+ }.freeze
44
45
 
45
- RESPONSE_CODE_TO_ERROR.values.each do |class_name|
46
+ RESPONSE_CODE_TO_ERROR.each_value do |class_name|
46
47
  const_set(class_name, create_class)
47
48
  end
48
49
 
@@ -0,0 +1,4 @@
1
+ require 'faraday'
2
+
3
+ require 'mrkt/faraday/params_encoder'
4
+ require 'mrkt/faraday_middleware'
@@ -0,0 +1,20 @@
1
+ module Mrkt
2
+ module Faraday
3
+ class ParamsEncoder
4
+ class << self
5
+ def encode(hash)
6
+ new_hash = hash.transform_values { |v| encode_value(v) }
7
+ ::Faraday::NestedParamsEncoder.encode(new_hash)
8
+ end
9
+
10
+ def decode(string)
11
+ ::Faraday::NestedParamsEncoder.decode(string)
12
+ end
13
+
14
+ def encode_value(value)
15
+ value.respond_to?(:join) ? value.join(',') : value
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end