unresponsys 0.0.9 → 0.1.0

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: 140ba580abc3fb8fe898395180262a279584d191
4
- data.tar.gz: 887817c8b8bb07fb9fd1216433feae008d7b3aa8
3
+ metadata.gz: 041bc50501ae1f8deb8f12dd258b0a290a062254
4
+ data.tar.gz: ce3847787e4907d4051f4af0478a0a886a35a45d
5
5
  SHA512:
6
- metadata.gz: 2a46806169f4f210795abbcadc7b03bef2e786c66ef801ff2473a0e5a1cce513fd7718c489b5185f16e3b4f5e7c55bf719b9f9e57084b07f890d1c7cbadab53b
7
- data.tar.gz: 2955543b360be917faeddd41de76368d728686aa3dcb32f81e9d7d97091248cf2f2897ad7d3703d05513d26daf20e04e247a1fc1a1428c5544f2542c8d86e0d9
6
+ metadata.gz: e671a298e294a4a7dc20561f118aa4e9d19b0d6a564308821ddbfc0f765b68073879cc1dc357bc237fda8da999b4715aede22abc20d0c4f15ab68b32f13fe4d7
7
+ data.tar.gz: e010fe01bc5ca6a505ee958f55e7e960416550d3419f368c2a88f17de3e8d32408d63772e4168631fbe628f5a15b834b55dc5d0f28558fbc10d87ea78529d766
@@ -2,38 +2,65 @@ require 'httparty'
2
2
 
3
3
  class Unresponsys
4
4
  class Client
5
- include HTTParty
6
-
7
5
  def initialize(options = {})
8
- raise Unresponsys::ArgumentError, 'Username is required' unless options[:username]
6
+ raise Unresponsys::ArgumentError unless options[:username] && options[:password]
9
7
  @username = options[:username]
10
-
11
- raise Unresponsys::ArgumentError, 'Password is required' unless options[:password]
12
8
  @password = options[:password]
9
+ authenticate
10
+ end
11
+
12
+ def get(path, options = {}, &block)
13
+ path = "#{@base_uri}#{path}"
14
+ options = @options.merge(options)
15
+ response = HTTParty.get(path, options, &block)
16
+ handle_error(response)
17
+ end
18
+
19
+ def post(path, options = {}, &block)
20
+ path = "#{@base_uri}#{path}"
21
+ options = @options.merge(options)
22
+ response = HTTParty.post(path, options, &block)
23
+ handle_error(response)
24
+ end
13
25
 
14
- raise Unresponsys::Error, 'Could not authenticate' unless authenticate
26
+ def delete(path, options = {}, &block)
27
+ path = "#{@base_uri}#{path}"
28
+ options = @options.merge(options)
29
+ response = HTTParty.delete(path, options, &block)
30
+ handle_error(response)
31
+ end
15
32
 
16
- self.class.debug_output($stdout) if options[:debug]
33
+ def folders
34
+ @folders ||= Folders.new(self)
17
35
  end
18
36
 
19
- def self.get(path, options = {}, &block)
20
- r = perform_request Net::HTTP::Get, path, options, &block
21
- handle_error(r)
37
+ def lists
38
+ @lists ||= Lists.new(self)
22
39
  end
23
40
 
24
- def self.post(path, options = {}, &block)
25
- r = perform_request Net::HTTP::Post, path, options, &block
26
- handle_error(r)
41
+ class Folders
42
+ def initialize(client)
43
+ @client = client
44
+ end
45
+
46
+ def find(folder_name)
47
+ Folder.new(@client, folder_name)
48
+ end
27
49
  end
28
50
 
29
- def self.delete(path, options = {}, &block)
30
- r = perform_request Net::HTTP::Delete, path, options, &block
31
- handle_error(r)
51
+ class Lists
52
+ def initialize(client)
53
+ @client = client
54
+ end
55
+
56
+ def find(list_name)
57
+ List.new(@client, list_name)
58
+ end
32
59
  end
33
60
 
34
61
  private
35
62
 
36
- def self.handle_error(response)
63
+ def handle_error(response)
37
64
  if response.is_a?(Hash) && response.keys.include?('errorCode')
38
65
  raise Unresponsys::TokenExpired if response['title'].include?('token expired')
39
66
  raise Unresponsys::NotFoundError, response['detail'] if response['title'].include?('not found')
@@ -43,13 +70,14 @@ class Unresponsys
43
70
  end
44
71
 
45
72
  def authenticate
46
- self.class.headers('Content-Type' => 'application/x-www-form-urlencoded')
47
- body = { user_name: @username, password: @password, auth_type: 'password' }
48
- r = self.class.post('https://login2.responsys.net/rest/api/v1/auth/token', body: body)
49
- return false unless r.success?
50
- self.class.headers('Authorization' => r['authToken'], 'Content-Type' => 'application/json')
51
- self.class.base_uri("#{r['endPoint']}/rest/api/v1")
52
- true
73
+ headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
74
+ body = { user_name: @username, password: @password, auth_type: 'password' }
75
+ response = HTTParty.post('https://login2.responsys.net/rest/api/v1/auth/token', headers: headers, body: body)
76
+
77
+ raise Unresponsys::AuthenticationError unless response.success?
78
+
79
+ @options = { headers: { 'Authorization' => response['authToken'], 'Content-Type' => 'application/json' } }
80
+ @base_uri = "#{response['endPoint']}/rest/api/v1"
53
81
  end
54
82
  end
55
83
  end
@@ -1,5 +1,6 @@
1
1
  class Unresponsys
2
2
  class ArgumentError < StandardError; end
3
+ class AuthenticationError < StandardError; end
3
4
  class Error < StandardError; end
4
5
  class NotFoundError < StandardError; end
5
6
  class TokenExpired < StandardError; end
@@ -1,5 +1,8 @@
1
1
  class Unresponsys
2
2
  class Event
3
+ extend Forwardable
4
+ delegate [:client] => :member
5
+ attr_reader :member
3
6
 
4
7
  def initialize(options = {})
5
8
  @event_name = options[:event]
@@ -11,10 +14,7 @@ class Unresponsys
11
14
  body = {
12
15
  customEvent: {},
13
16
  recipientData: [{
14
- recipient: {
15
- listName: { objectName: @member.list },
16
- recipientId: @member.riid,
17
- }
17
+ recipient: { listName: { objectName: @member.list.name }, recipientId: @member.riid }
18
18
  }]
19
19
  }
20
20
 
@@ -26,10 +26,9 @@ class Unresponsys
26
26
  end
27
27
  end
28
28
 
29
- r = Unresponsys::Client.post("/events/#{@event_name}", body: body.to_json)
29
+ r = client.post("/events/#{@event_name}", body: body.to_json)
30
30
  return false if r.first['errorMessage'].present?
31
31
  true
32
32
  end
33
-
34
33
  end
35
34
  end
@@ -1,13 +1,10 @@
1
1
  class Unresponsys
2
2
  class Folder
3
- attr_reader :name
3
+ attr_reader :client, :name
4
4
 
5
- def self.find(name)
6
- self.new(name)
7
- end
8
-
9
- def initialize(name)
10
- @name = name
5
+ def initialize(client, name)
6
+ @client = client
7
+ @name = name
11
8
  end
12
9
 
13
10
  def tables
@@ -1,13 +1,10 @@
1
1
  class Unresponsys
2
2
  class List
3
- attr_reader :name
3
+ attr_reader :client, :name
4
4
 
5
- def self.find(name)
6
- self.new(name)
7
- end
8
-
9
- def initialize(name)
10
- @name = name
5
+ def initialize(client, name)
6
+ @client = client
7
+ @name = name
11
8
  end
12
9
 
13
10
  def members
@@ -21,7 +18,7 @@ class Unresponsys
21
18
 
22
19
  def find(email)
23
20
  options = { query: { qa: 'e', id: email.to_responsys, fs: 'all' } }
24
- r = Unresponsys::Client.get("/lists/#{@list.name}/members", options)
21
+ r = @list.client.get("/lists/#{@list.name}/members", options)
25
22
 
26
23
  fields = {}
27
24
  r['recordData']['fieldNames'].each_with_index do |field, index|
@@ -1,5 +1,8 @@
1
1
  class Unresponsys
2
2
  class Member
3
+ extend Forwardable
4
+ delegate [:client] => :list
5
+ attr_reader :list
3
6
 
4
7
  def initialize(list, fields)
5
8
  @fields = default_fields.merge(fields)
@@ -29,10 +32,6 @@ class Unresponsys
29
32
  email_address
30
33
  end
31
34
 
32
- def list
33
- @list.name
34
- end
35
-
36
35
  def save
37
36
  record_data = { fieldNames: [], records: [[]], mapTemplateName: nil }
38
37
  @fields.each_pair do |key, val|
@@ -47,8 +46,9 @@ class Unresponsys
47
46
  end
48
47
 
49
48
  options = { body: { recordData: record_data, mergeRule: merge_rule }.to_json }
50
- r = Unresponsys::Client.post("/lists/#{@list.name}/members", options)
49
+ r = client.post("/lists/#{@list.name}/members", options)
51
50
  return false if r['recordData']['records'][0][0].include?('MERGEFAILED')
51
+
52
52
  @changed = ['EMAIL_ADDRESS_']
53
53
  self.instance_variable_set(:@riid, r['recordData']['records'][0][0])
54
54
  true
@@ -137,6 +137,5 @@ class Unresponsys
137
137
  rejectRecordIfChannelEmpty: nil,
138
138
  }
139
139
  end
140
-
141
140
  end
142
141
  end
@@ -1,5 +1,7 @@
1
1
  class Unresponsys
2
2
  class Table
3
+ extend Forwardable
4
+ delegate [:client] => :folder
3
5
  attr_reader :folder, :name
4
6
 
5
7
  def initialize(folder, table_name)
@@ -18,7 +20,7 @@ class Unresponsys
18
20
 
19
21
  def find(primary_key)
20
22
  options = { query: { qa: 'ID_', id: primary_key.to_responsys, fs: 'all' } }
21
- r = Unresponsys::Client.get("/folders/#{@table.folder.name}/suppData/#{@table.name}/members", options)
23
+ r = @table.client.get("/folders/#{@table.folder.name}/suppData/#{@table.name}/members", options)
22
24
 
23
25
  fields = {}
24
26
  r['recordData']['fieldNames'].each_with_index do |field, index|
@@ -1,3 +1,3 @@
1
1
  class Unresponsys
2
- VERSION = '0.0.9'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unresponsys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Kimball
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty