Permalink
Browse files

create, rename, and destroy audience list

  • Loading branch information...
1 parent 79576f5 commit e59087ba6aa5b387b2e9a1873701898dacb9d1d6 @ayamomiji committed Nov 29, 2011
@@ -1,4 +1,5 @@
require 'rest-core'
+require 'crack/xml'
RestCore::MadMimi = RestCore::Builder.client(:data, :username, :api_key,
:promotion_name) do
@@ -13,6 +14,8 @@
class RestCore::MadMimi::Error < RestCore::Error
end
+require 'rest-core/client/mad_mimi/audience_list'
+
module RestCore::MadMimi::Client
include RestCore
@@ -81,6 +84,46 @@ def status(id)
end
end
+ # https://madmimi.com/developer/lists
+ # Audience lists apis
+
+ def audience_lists
+ response = get('/audience_lists/lists.xml')
+ Crack::XML.parse(response)['lists']['list'].map do |list|
+ RestCore::MadMimi::AudienceList.new(self, list)
+ end
+ end
+
+ def create_audience_list(name)
+ response = post('/audience_lists', :name => name)
+ if response.code == 200
+ cache.clear
+ audience_lists.find { |list| list.name == name }
+ else
+ raise RestCore::MadMimi::Error, response
+ end
+ end
+
+ def rename_audience_list(name, new_name)
+ response = post("/audience_lists/#{CGI.escape(name)}/rename",
+ :name => new_name)
+ if response.code == 200
+ cache.clear
+ audience_lists.find { |list| list.name == new_name }
+ else
+ raise RestCore::MadMimi::Error, response
+ end
+ end
+
+ def destroy_audience_list(name)
+ response = post("/audience_lists/#{CGI.escape(name)}", :_method => 'delete')
+ if response.code == 200
+ cache.clear
+ else
+ raise RestCore::MadMimi::Error, response
+ end
+ end
+
def query
{'username' => username,
'api_key' => api_key,
@@ -0,0 +1,27 @@
+class RestCore::MadMimi::AudienceList
+ attr_reader :client
+ attr_reader :id, :name, :display_name, :subscriber_count
+
+ def initialize(client, values = {})
+ @client = client
+ values.each do |key, value|
+ instance_variable_set("@#{key}", value)
+ end
+ end
+
+ def name=(new_name)
+ if client.rename_audience_list(name, new_name)
+ @name = new_name
+ end
+ end
+
+ def destroy
+ client.destroy_audience_list(self.name)
+ end
+
+ def to_s
+ %w"#<RestCore::MadMimi::AudienceList
+ @id=%s @name=%s @display_name=%s @subscriber_count=%s>".
+ join(' ') % [id, name, display_name, subscriber_count].map(&:inspect)
+ end
+end
@@ -0,0 +1,91 @@
+require 'rest-more/test'
+
+AL = RestCore::MadMimi::AudienceList
+
+describe RC::MadMimi, 'list api' do
+ before do
+ @client = RestCore::MadMimi.new
+ end
+
+ after do
+ WebMock.reset!
+ RR.verify
+ end
+
+ should 'get audience lists' do
+ stub_request(:get, 'https://api.madmimi.com/audience_lists/lists.xml').
+ to_return(:body => 'stubbed body')
+ mock(Crack::XML).parse('stubbed body') {
+ {
+ 'lists' => {
+ 'list' => [
+ {'id' => '1', 'name' => 'first'},
+ {'id' => '2', 'name' => 'last'},
+ ]
+ }
+ }
+ }
+ lists = @client.audience_lists
+ lists.size.should.eq(2)
+ lists.first.tap do |first|
+ first.client.should.eq(@client)
+ first.should.be.a.kind_of(AL)
+ first.id.should.eq('1')
+ first.name.should.eq('first')
+ end
+ lists.last.tap do |last|
+ last.client.should.eq(@client)
+ last.should.be.a.kind_of(AL)
+ last.id.should.eq('2')
+ last.name.should.eq('last')
+ end
+ end
+
+ should 'create audience list' do
+ stub_request(:post, 'https://api.madmimi.com/audience_lists').
+ with(:params => {:name => 'rc-test'})
+ mock(@client).audience_lists {
+ [AL.new(@client, :name => 'first'),
+ AL.new(@client, :name => 'rc-test'),
+ AL.new(@client, :name => 'last')]
+ }
+ @client.create_audience_list('rc-test').name.should.eq('rc-test')
+
+ stub_request(:post, 'https://api.madmimi.com/audience_lists').
+ to_return(:status => 422)
+ lambda {
+ @client.create_audience_list('error')
+ }.should.raise(RestCore::MadMimi::Error)
+ end
+
+ should 'rename audience list' do
+ stub_request(:post,
+ 'https://api.madmimi.com/audience_lists/rc-test/rename').
+ with(:params => {:name => 'renamed'})
+ mock(@client).audience_lists {
+ [AL.new(@client, :name => 'first'),
+ AL.new(@client, :name => 'renamed'),
+ AL.new(@client, :name => 'last')]
+ }
+ list = @client.rename_audience_list('rc-test', 'renamed')
+ list.name.should.eq('renamed')
+
+ stub_request(:post, 'https://api.madmimi.com/audience_lists/error/rename').
+ with(:params => {:name => 'renamed'}).
+ to_return(:status => 422)
+ lambda {
+ @client.rename_audience_list('error', 'renamed')
+ }.should.raise(RestCore::MadMimi::Error)
+ end
+
+ should 'destroy audience list' do
+ stub_request(:post, 'https://api.madmimi.com/audience_lists/rc-test')
+ @client.destroy_audience_list('rc-test')
+
+ stub_request(:post, 'https://api.madmimi.com/audience_lists/error').
+ to_return(:status => 422)
+ lambda {
+ @client.destroy_audience_list('error')
+ }.should.raise(RestCore::MadMimi::Error)
+ end
+end

0 comments on commit e59087b

Please sign in to comment.