cheetahmails 0.0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/lib/cheetahmails.rb +153 -0
  4. metadata +101 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f0ba4bd29038677e67d57cb0577484dfbb8b209
4
+ data.tar.gz: 652bdfc20d6ae2af735c94dcd2cc108de10f8e56
5
+ SHA512:
6
+ metadata.gz: e402bf99b4b365005ea9b0a04006f4b928ecae91421baea0c2aeb6e3f54c74b3b7ab902be0a3fd995f85075a339afced82d7398aa7c2cba307cf076d0036045e
7
+ data.tar.gz: 55c5cf6e4afc6568b494cb9697638a0b9367bdb033ce7213d32dcd8591e83d201de863be3d44b2362b6c04fb025ee406b3454dbb7fcd6e0c20ef09767e8d922d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in surveymonkey.gemspec
4
+ gemspec
@@ -0,0 +1,153 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'redis'
4
+
5
+ module Cheetahmails
6
+ include HTTParty
7
+ base_uri 'https://login.eccmp.com'
8
+
9
+ class << self
10
+ attr_accessor :configuration
11
+ end
12
+
13
+ def self.getToken(clear_cache = false)
14
+
15
+ redis = Redis.new(Cheetahmails.configuration.redis)
16
+
17
+ redis.del "cheetahmails_access_token" if clear_cache
18
+
19
+ if not token = redis.get("cheetahmails_access_token")
20
+
21
+ @options = {
22
+ headers: {"Content-Type" => "application/x-www-form-urlencoded"},
23
+ body: {
24
+ "username" => Cheetahmails.configuration.username,
25
+ "password" => Cheetahmails.configuration.password,
26
+ "grant_type" => "password"
27
+ }
28
+ }
29
+
30
+ response = self.post('/services2/authorization/oAuth2/Token', @options)
31
+
32
+ begin
33
+ jsonresponse = JSON.parse(response.body)
34
+ rescue JSON::ParserError => error
35
+ return nil
36
+ end
37
+
38
+ if token = jsonresponse["access_token"]
39
+ redis.set("cheetahmails_access_token", token)
40
+ redis.expire("cheetahmails_access_token", jsonresponse["expires_in"])
41
+ end
42
+
43
+ end
44
+
45
+ token
46
+
47
+ end
48
+
49
+ def self.customerExists(email)
50
+ tries ||= 2
51
+
52
+ @options = {
53
+ headers: {
54
+ "Authorization" => "Bearer " + self.getToken(tries < 2),
55
+ "Content-Type" => "application/json",
56
+ "Accept" => "application/json"
57
+ },
58
+ query: {
59
+ "viewName" => Cheetahmails.configuration.view_name,
60
+ "prop" => '', #first_name,last_name
61
+ "columnName" => "email_address",
62
+ "operation" => "=",
63
+ "param" => email
64
+ }
65
+ }
66
+
67
+ response = self.get('/services2/api/SearchRecords', @options)
68
+
69
+ begin
70
+ jsonresponse = JSON.parse(response.body)
71
+ rescue JSON::ParserError => error
72
+ return nil
73
+ end
74
+
75
+ raise RetryException, jsonresponse["message"] if response.code == 401
76
+
77
+ begin
78
+ id = jsonresponse[0]["id"]
79
+ rescue => error
80
+ return false
81
+ end
82
+
83
+ rescue RetryException => e
84
+ if (tries -= 1) > 0
85
+ retry
86
+ end
87
+ end
88
+
89
+ def self.addCustomer(api_post_id, key_data)
90
+ tries ||= 2
91
+
92
+ data = []
93
+
94
+ key_data.each do |key, value|
95
+ data << { "name" => key, "value" => value}
96
+ end
97
+
98
+ data = {"apiPostId" => api_post_id, "data" => data}
99
+
100
+ @options = {
101
+ headers: {
102
+ "Authorization" => "Bearer " + self.getToken(tries < 2),
103
+ "Content-Type" => "application/json",
104
+ "Accept" => "application/json"
105
+ },
106
+ body: data.to_json
107
+ }
108
+
109
+ response = self.post('/services2/api/Recipients', @options)
110
+
111
+ begin
112
+ jsonresponse = JSON.parse(response.body)
113
+ rescue JSON::ParserError => error
114
+ return nil
115
+ end
116
+
117
+ raise RetryException, jsonresponse["message"] if response.code == 401
118
+
119
+ return response.code == 200 && jsonresponse["success"]
120
+
121
+ rescue RetryException => e
122
+ if (tries -= 1) > 0
123
+ retry
124
+ end
125
+ end
126
+
127
+ def self.configuration
128
+ @configuration ||= Configuration.new
129
+ end
130
+
131
+ def self.configure
132
+ self.configuration ||= Configuration.new
133
+ yield(configuration)
134
+ end
135
+
136
+ class Configuration
137
+ attr_accessor :username
138
+ attr_accessor :password
139
+ attr_accessor :redis
140
+ attr_accessor :view_name
141
+
142
+ def initialize
143
+ @username = ''
144
+ @password = ''
145
+ @view_name = 'Email Recipient'
146
+ @redis = { :host => "127.0.0.1", :port => 6379, :db => 0 }
147
+ end
148
+ end
149
+
150
+ class RetryException < RuntimeError
151
+ end
152
+
153
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cheetahmails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tom Holder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dotenv
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Some basic cheetah mail API end points implemented
70
+ email: tom@simpleweb.co.uk
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/cheetahmails.rb
76
+ - Gemfile
77
+ homepage: http://rubygems.org/gems/cheetahmail
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.0
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Cheetah mail is a clunky campaign manager by Experian
101
+ test_files: []