cheetahmails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cheetahmails.rb +59 -43
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f0ba4bd29038677e67d57cb0577484dfbb8b209
4
- data.tar.gz: 652bdfc20d6ae2af735c94dcd2cc108de10f8e56
3
+ metadata.gz: aee984ac6a9285db5c84668cfe43c3984168a747
4
+ data.tar.gz: 17caa4d68620ab641c8052048ad26129c1c4a3a2
5
5
  SHA512:
6
- metadata.gz: e402bf99b4b365005ea9b0a04006f4b928ecae91421baea0c2aeb6e3f54c74b3b7ab902be0a3fd995f85075a339afced82d7398aa7c2cba307cf076d0036045e
7
- data.tar.gz: 55c5cf6e4afc6568b494cb9697638a0b9367bdb033ce7213d32dcd8591e83d201de863be3d44b2362b6c04fb025ee406b3454dbb7fcd6e0c20ef09767e8d922d
6
+ metadata.gz: 650fff6a0faf9565dfd90f2a789ab6bec812b97bac59c073b3e25d2674706bc8ccd5784e9917b43c5cf117e111b2d3ef298ca75d9d415c273a5fb7182c3a466b
7
+ data.tar.gz: f52b315512e4567510e989ac18aeb0df8c5742336ad3b2948ed018f4f5467b6e09fecedaeba2cbbc4f3342ae1c091def25dedff2ac0ff04c76b2d511ff25d6e8
@@ -1,16 +1,16 @@
1
- require 'httparty'
1
+ require 'faraday'
2
2
  require 'json'
3
3
  require 'redis'
4
4
 
5
5
  module Cheetahmails
6
- include HTTParty
7
- base_uri 'https://login.eccmp.com'
6
+ @base_uri = 'https://login.eccmp.com'
8
7
 
9
8
  class << self
10
9
  attr_accessor :configuration
11
10
  end
12
11
 
13
- def self.getToken(clear_cache = false)
12
+ def self.get_token(clear_cache = false)
13
+ tries ||= 2
14
14
 
15
15
  redis = Redis.new(Cheetahmails.configuration.redis)
16
16
 
@@ -18,21 +18,26 @@ module Cheetahmails
18
18
 
19
19
  if not token = redis.get("cheetahmails_access_token")
20
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
- }
21
+ faraday = Faraday.new(:url => @base_uri) do |faraday|
22
+ faraday.request :url_encoded # form-encode POST params
23
+ #faraday.response :logger # log requests to STDOUT
24
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
25
+ end
26
+
27
+ params = {
28
+ "username" => Cheetahmails.configuration.username,
29
+ "password" => Cheetahmails.configuration.password,
30
+ "grant_type" => "password"
28
31
  }
29
32
 
30
- response = self.post('/services2/authorization/oAuth2/Token', @options)
33
+ response = faraday.post '/services2/authorization/oAuth2/Token', params
34
+
35
+ raise RetryException, response.status.to_s + " " + response.body if response.status != 200
31
36
 
32
37
  begin
33
38
  jsonresponse = JSON.parse(response.body)
34
39
  rescue JSON::ParserError => error
35
- return nil
40
+ raise response.status.to_s + " " + response.body
36
41
  end
37
42
 
38
43
  if token = jsonresponse["access_token"]
@@ -44,35 +49,44 @@ module Cheetahmails
44
49
 
45
50
  token
46
51
 
52
+ rescue RetryException => e
53
+ if (tries -= 1) > 0
54
+ retry
55
+ else
56
+ raise e
57
+ end
47
58
  end
48
59
 
49
- def self.customerExists(email)
60
+ def self.find_list_member(email)
50
61
  tries ||= 2
51
62
 
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
- }
63
+ faraday = Faraday.new(:url => @base_uri) do |faraday|
64
+ faraday.request :url_encoded # form-encode POST params
65
+ #faraday.response :logger # log requests to STDOUT
66
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
67
+ end
68
+
69
+ faraday.headers["Authorization"] = "Bearer " + self.get_token(tries < 2)
70
+ faraday.headers["Content-Type"] = "application/json"
71
+ faraday.headers["Accept"] = "application/json"
72
+
73
+ params = {
74
+ "viewName" => Cheetahmails.configuration.view_name,
75
+ "prop" => '', #first_name,last_name
76
+ "columnName" => "email_address",
77
+ "operation" => "=",
78
+ "param" => email
65
79
  }
66
80
 
67
- response = self.get('/services2/api/SearchRecords', @options)
81
+ response = faraday.get '/services2/api/SearchRecords', params
68
82
 
69
83
  begin
70
84
  jsonresponse = JSON.parse(response.body)
71
85
  rescue JSON::ParserError => error
72
- return nil
86
+ raise response.status.to_s + " " + response.body
73
87
  end
74
88
 
75
- raise RetryException, jsonresponse["message"] if response.code == 401
89
+ raise RetryException, jsonresponse["message"] if response.status == 401
76
90
 
77
91
  begin
78
92
  id = jsonresponse[0]["id"]
@@ -86,7 +100,7 @@ module Cheetahmails
86
100
  end
87
101
  end
88
102
 
89
- def self.addCustomer(api_post_id, key_data)
103
+ def self.add_list_member(api_post_id, key_data)
90
104
  tries ||= 2
91
105
 
92
106
  data = []
@@ -97,26 +111,28 @@ module Cheetahmails
97
111
 
98
112
  data = {"apiPostId" => api_post_id, "data" => data}
99
113
 
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
- }
114
+ faraday = Faraday.new(:url => @base_uri) do |faraday|
115
+ #faraday.response :logger # log requests to STDOUT
116
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
117
+ end
108
118
 
109
- response = self.post('/services2/api/Recipients', @options)
119
+ response = faraday.post do |req|
120
+ req.url '/services2/api/Recipients'
121
+ req.headers["Authorization"] = "Bearer " + self.get_token(tries < 2)
122
+ req.headers["Content-Type"] = "application/json"
123
+ req.headers["Accept"] = "application/json"
124
+ req.body = data.to_json
125
+ end
110
126
 
111
127
  begin
112
128
  jsonresponse = JSON.parse(response.body)
113
129
  rescue JSON::ParserError => error
114
- return nil
130
+ raise response.status.to_s + " " + response.body
115
131
  end
116
132
 
117
- raise RetryException, jsonresponse["message"] if response.code == 401
133
+ raise RetryException, jsonresponse["message"] if response.status == 401
118
134
 
119
- return response.code == 200 && jsonresponse["success"]
135
+ return response.status == 200 && jsonresponse["success"]
120
136
 
121
137
  rescue RetryException => e
122
138
  if (tries -= 1) > 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheetahmails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Holder
@@ -11,19 +11,19 @@ cert_chain: []
11
11
  date: 2015-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: httparty
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 0.13.3
19
+ version: 0.9.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 0.13.3
26
+ version: 0.9.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: redis
29
29
  requirement: !ruby/object:Gem::Requirement