Class: Sendyr::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ct_sendyr/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list_id = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
# File 'lib/ct_sendyr/client.rb', line 5

def initialize(list_id = nil)
	@list_id     = list_id
	@api_key     = Sendyr.configuration.api_key
	@base_uri    = Sendyr.configuration.url
	@noop        = Sendyr.configuration.noop || false
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/ct_sendyr/client.rb', line 3

def api_key
  @api_key
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



3
4
5
# File 'lib/ct_sendyr/client.rb', line 3

def base_uri
  @base_uri
end

#last_resultObject (readonly)

Returns the value of attribute last_result.



3
4
5
# File 'lib/ct_sendyr/client.rb', line 3

def last_result
  @last_result
end

#list_idObject (readonly)

Returns the value of attribute list_id.



3
4
5
# File 'lib/ct_sendyr/client.rb', line 3

def list_id
  @list_id
end

Instance Method Details

#active_subscriber_count(opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ct_sendyr/client.rb', line 90

def active_subscriber_count(opts = {})
	return noop if @noop

	opts = {api_key: @api_key, list_id: @list_id}.merge(opts)
	raise_if_missing_arg([:list_id, :api_key], opts)

	path   = '/api/subscribers/active-subscriber-count.php'
	result = post_to(path, opts)

	cleaned_body = clean_body(result)
	if result.success? && !!(cleaned_body =~ /^[-+]?[0-9]+$/)
		respond_with_success(result, cleaned_body.to_i)
	else
		respond_with_failure(result)
	end
end

#subscribe(opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ct_sendyr/client.rb', line 12

def subscribe(opts = {})
	return noop if @noop

	opts = {boolean: true, list: @list_id}.merge(opts)
	raise_if_missing_arg([:email, :list], opts)

	path   = '/subscribe'
	result = post_to(path, opts)

	if result.success? && %w(true 1).include?(clean_body(result))
		respond_with_success(result)
	else
		respond_with_failure(result)
	end
end

#subscription_status(opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ct_sendyr/client.rb', line 44

def subscription_status(opts = {})
	return noop if @noop

	opts = {api_key: @api_key, list_id: @list_id}.merge(opts)
	raise_if_missing_arg([:api_key, :email, :list_id, :api_key], opts)

	path   = '/api/subscribers/subscription-status.php'
	result = post_to(path, opts)

	success_messages = { "subscribed"   => :subscribed,
											 "unsubscribed" => :unsubscribed,
											 "unconfirmed"  => :unconfirmed,
											 "bounced"      => :bounced,
											 "soft bounced" => :soft_bounced,
											 "complained"   => :complained,
											 "email does not exist in list" => :not_in_list }

    cleaned_body = clean_body(result)
	if result.success? && success_messages.keys.include?(cleaned_body)
		respond_with_success(result, success_messages[cleaned_body])
	else
		respond_with_failure(result, underscore(cleaned_body).to_sym)
	end
end

#unsubscribe(opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ct_sendyr/client.rb', line 28

def unsubscribe(opts = {})
	return noop if @noop

	opts = {boolean: true, list: @list_id}.merge(opts)
	raise_if_missing_arg([:email, :list], opts)

	path   = '/unsubscribe'
	result = post_to(path, opts)

	if result.success? && %w(true 1).include?(clean_body(result))
		respond_with_success(result)
	else
		respond_with_failure(result)
	end
end

#update_subscription(email, opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ct_sendyr/client.rb', line 69

def update_subscription(email, opts = {})
	return noop if @noop

	status = subscription_status(email: email)

	return false if status == :not_in_list

	# Trying to change the email address?
	# Need to unsubscribe and subscribe again.
	if (!opts[:email].nil? && opts[:email] != email) &&
		[:subscribed, :unconfirmed, :bounced, :soft_bounced].include?(status)
		unsubscribe(email: email)
	end

	unless [:complained, :unsubscribed].include?(status)
		subscribe({email: email}.merge(opts)) == true
	else
		false
	end
end