Class: Chimp

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

Constant Summary collapse

URL =
"https://%s.api.mailchimp.com/1.3/?method="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey, datacenter) ⇒ Chimp

Returns a new instance of Chimp.



20
21
22
23
# File 'lib/chimp.rb', line 20

def initialize(apikey, datacenter)
  @url  = URL % datacenter
  @data = { apikey: apikey }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/chimp.rb', line 8

def data
  @data
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/chimp.rb', line 7

def url
  @url
end

Class Method Details

.connect(apikey) ⇒ Object



10
11
12
13
14
# File 'lib/chimp.rb', line 10

def self.connect(apikey)
  datacenter = apikey[/-(.+)$/, 1]

  @current = new(apikey, datacenter)
end

.currentObject



16
17
18
# File 'lib/chimp.rb', line 16

def self.current
  @current || raise(RuntimeError, "No chimp connection defined.")
end

Instance Method Details

#list_members(list_id, status = nil, opts = {}) ⇒ Object



37
38
39
# File 'lib/chimp.rb', line 37

def list_members(list_id, status=nil, opts = {})
  JSON(post("listMembers", id: list_id, status: status))
end

#listsObject



25
26
27
# File 'lib/chimp.rb', line 25

def lists
  JSON(post("lists"))
end

#subscribe(email, list_id, opts = {}) ⇒ Object



29
30
31
# File 'lib/chimp.rb', line 29

def subscribe(email, list_id, opts = {})
  post("listSubscribe", opts.merge(email_address: email, id: list_id))
end

#unsubscribe(email, list_id) ⇒ Object



33
34
35
# File 'lib/chimp.rb', line 33

def unsubscribe(email, list_id)
  post("listUnsubscribe", email_address: email, id: list_id)
end