Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor user_id to cached subscriber_id, clean up comments

  • Loading branch information...
commit 434701793ab06f97c02570fed0ec0b4501d0ccba 1 parent 0b55c36
@heidi heidi authored
View
3  README.md
@@ -1,6 +1,6 @@
# Yesmail
-A RubyGem to interact with the Yesmail api (http://www.yesmail.com/)
+A RubyGem to interact with the Yesmail API (http://www.yesmail.com/)
## Installation
@@ -34,6 +34,7 @@ yesmail is written and maintained by ApartmentList.com:
Contributors include:
+ * [Heidi Galbraith] (https://github.com/heidiho)
* [Wes Hunt](https://github.com/wesdotcool)
* [Jon Snitow](https://github.com/otherjonvb)
* [@jakeonrails](http://twitter.com/jakeonrails)
View
8 lib/yesmail/configuration.rb
@@ -1,10 +1,10 @@
-# This holds the information that Yesmail needs before it allows any api calls.
-# These values will be placed in json when sending Yesmail queries
+# This holds the information that Yesmail needs before it allows any API calls.
+# These values will be placed in JSON when sending Yesmail queries.
#
# The Yesmail module initializes its configuration on startup. You can change
# it at the command line:
-# Yesmail::configuratoin.username = 'blah'
-# Yesmail::configuratoin.division = 'Retention'
+# Yesmail::configuration.username = 'blah'
+# Yesmail::configuration.division = 'Retention'
module Yesmail
class Configuration
View
8 lib/yesmail/master.rb
@@ -1,7 +1,7 @@
-# This class is used to represent a single transaction with Yesmail
+# This class is used to represent a single transaction with Yesmail.
module Yesmail
class Master
- # @attribute api_id [String] the master_id that identifies
+ # @attribute api_id [String] The master_id that identifies
# your account with Yesmail
attr_accessor :api_id
@@ -9,8 +9,8 @@ def path
'/masters'
end
- # @return [Hash] represents the masterId and transactionID
- # part of the outgoing JSON request to Yesmail
+ # @return [Hash] A hash representing the masterId part of the outgoing JSON
+ # request to Yesmail
def subscriber_message_data
{
masterId: api_id,
View
8 lib/yesmail/poster.rb
@@ -1,5 +1,5 @@
-# This class handles the bare communications with Yesmail
-# It uses HTTParty for the underlying http requests
+# This class handles the bare communications with Yesmail.
+# It uses HTTParty for the underlying HTTP requests.
require 'httparty'
module Yesmail
@@ -24,8 +24,8 @@ def options(post_data, is_post = false)
hash
end
- # This will handle an http post, and return the body of the response
- # @param post_data [String] data that will be placed in the post body
+ # This will handle an HTTP post, and return the body of the response.
+ # @param post_data [String] The data that will be placed in the post body
# @param path [String] The URL to post the data
def post(post_data, path)
response = self.class.post(path, options(post_data, true))
View
6 lib/yesmail/side_table.rb
@@ -1,5 +1,6 @@
# This class handles the side table part of the outgoing JSON message to
-# Yesmail
+# Yesmail. A side table is a table in your database related to the Yesmail
+# users table.
#
# According to the online API, the side table is structured as such:
#
@@ -27,9 +28,8 @@ def rows
@rows ||= { rows: [] }
end
- # This probably the only method you need to call.
+ # This is probably the only method you need to call.
def data_to_rows(data, subscriber = nil)
- #increment the sort id
@sort_id += 1
data.merge!({ email: subscriber.email })
columns = { columns: [] }
View
53 lib/yesmail/subscriber.rb
@@ -1,5 +1,5 @@
-# This class represents 1 person that will receive an email from Yesmail
-# It currently supports the Send and Subscribe Composite API, and the
+# This class represents 1 person that will receive an email from Yesmail.
+# It currently supports the Send and Subscribe composite API, and the
# Subscriber API.
#
# Key concepts:
@@ -26,7 +26,7 @@ class Subscriber
# @attribute name [String] The name of the user
# @attribute attribute_data [Hash] used for any extra data in the user
# attributes
- attr_accessor :email, :name, :user_id, :attribute_data
+ attr_accessor :email, :name, :attribute_data
def path
'/subscribers'
@@ -44,7 +44,6 @@ def make_hash
data.each do |key, value|
data_hash[:attributes][:attributes] << { name: key, value: value }
end
-
data_hash
end
@@ -61,7 +60,7 @@ def data
end
# These name methods aren't really safe. They might just blow up if the
- # name isn't formatted correctly. (However, they work correctly if +name+
+ # name isn't formatted correctly. (However, they work correctly if +name+
# is a first name with no last name.)
def first_name
name.split(' ').first
@@ -71,6 +70,19 @@ def last_name
name.split(' ')[1..-1].join(' ')
end
+ # Invoke api_get() and parse the response to determine the user ID, if any.
+ # @return [Fixnum] The subscriber ID associated with the email address,
+ # or nil if Yesmail does not know this email address
+ # Cache subscriber_id so we don't have to make multiple API calls
+ def subscriber_id
+ @subscriber_id ||= begin
+ user_id_pattern = %r{https://services\.yesmail\.com/enterprise/subscribers/(\d+)}
+ id_string = api_get[user_id_pattern]
+ id_string.try(:to_i)
+ end
+ @subscriber_id
+ end
+
def handler
@poster ||= Poster.new(Yesmail.configuration.username, Yesmail.configuration.password)
end
@@ -80,27 +92,15 @@ def api_create
end
def api_update
- user_id = self.user_id || get_user_id_from_email
data_hash = make_hash
- #allowResubscribe must be true when resubscribing a subscriber
+ # allowResubscribe must be true when resubscribing a subscriber
data_hash[:allowResubscribe] = true
- handler.update(data_hash, path, user_id)
- end
-
- # Invoke api_get() and parse the response to determine the user ID, if any.
- # @return [Fixnum] the subscriber ID associated with the email address,
- # or 0 if Yesmail does not know this email address.
- def get_user_id_from_email
- if user_id.nil?
- response = api_get
- match_data = response.match(%r[https://services\.yesmail\.com/enterprise/subscribers/(\d+)])
- match_data.present? ? match_data[1].to_i : 0
- end
+ handler.update(data_hash, path, subscriber_id)
end
# Return the raw XML response from attempting to retrieve this subscriber
# via their email address.
- # @return [String] an XML response such as the following, on success:
+ # @return [String] An XML response such as the following, on success:
# <?xml version="1.0"?>
# <yesws:uri xmlns:yesws="https://services.yesmail.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://services.yesmail.com docs/xsd/uri.xsd">https://services.yesmail.com/enterprise/subscribers/2266554</yesws:uri>
#
@@ -119,17 +119,16 @@ def api_get
# @return [String] the XML response from the server
def api_remove
data_hash = make_hash
- user_id = get_user_id_from_email
delete_hash = { division: data_hash[:division][:value] }
- handler.remove(delete_hash, path, user_id)
+ handler.remove(delete_hash, path, subscriber_id)
end
- # This will create all of the json from the data placed in this subscriber
- # and send it in the form of JSON to Yesmail's subscribe and send
- # composite API
+ # This will create all of the JSON from the data placed in this subscriber
+ # and send it in the form of JSON to Yesmail's Subscribe and Send
+ # composite API.
#
- # @param master [Yesmail::Master] the email campaign
- # @param side_table [Yesmail::SideTable] data to be stored in side table
+ # @param master [Yesmail::Master] The email campaign
+ # @param side_table [Yesmail::SideTable] The data to be stored in side table
# or nil if no side table data should be sent
# @return [String] the XML response from the server
def api_create_and_send(master, side_table = nil)
View
2  spec/lib/yesmail/subscriber_spec.rb
@@ -58,7 +58,7 @@ module Yesmail
describe '#api_update' do
let(:subscriber) { Subscriber.new }
it 'sets allowResubscribe to true' do
- stub(subscriber).get_user_id_from_email { 1 }
+ stub(subscriber).subscriber_id
mock(subscriber.handler).update(
post_data = hash_including(allowResubscribe: true),
Please sign in to comment.
Something went wrong with that request. Please try again.