Class: Cindy::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(sendy_url, api_key = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/cindy/client.rb', line 8

def initialize(sendy_url, api_key = nil)
  @url = sendy_url
  @key = api_key || ENV['SENDY_API_KEY']
end

Instance Method Details

#active_subscriber_count(list_id) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/cindy/client.rb', line 55

def active_subscriber_count(list_id)
  response = connection.post "api/subscribers/active-subscriber-count.php" do |req|
    req.body = {list_id: list_id, api_key: @key}
  end

  response.body.to_i
end

#create_campaign(opts = {}) ⇒ Object



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

def create_campaign(opts={})
  post_opts     = {}
  req_opts      = i(from_name from_email reply_to subject html_text)
  optional_opts = i(plain_text list_ids brand_id send_campaign)

  req_opts.each do |opt|
    post_opts[opt] = opts.delete(opt) || raise(ArgumentError, "opt :#{opt} required")
  end
  post_opts.merge!(Hash[optional_opts.zip(opts.values_at(*optional_opts))])
  post_opts[:api_key] = @key

  response = connection.post "api/campaigns/create.php" do |req|
    req.body = post_opts
  end

  response.body
end

#subscribe(list_id, email, name = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/cindy/client.rb', line 31

def subscribe(list_id, email, name = nil)
  response = connection.post "subscribe" do |req|
    params = {list: list_id, email: email, boolean: true}
    params[:name] = name if name
    req.body = params
  end

  !!(response.body =~ /^1$/)
end

#subscription_status(list_id, email) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/cindy/client.rb', line 47

def subscription_status(list_id, email)
  response = connection.post "api/subscribers/subscription-status.php" do |req|
    req.body = {list_id: list_id, email: email, api_key: @key}
  end

  response.body
end

#unsubscribe(list_id, email) ⇒ Object



41
42
43
44
45
# File 'lib/cindy/client.rb', line 41

def unsubscribe(list_id, email)
  response = connection.post "unsubscribe", {list: list_id, email: email, boolean: true}

  !!(response.body =~ /^1$/)
end