<commit_before>require 'rest-core'
require 'crack/xml'

RestCore::MadMimi = RestCore::Builder.client(:data, :username, :api_key,
                                             :promotion_name) do
  s = self.class # this is only for ruby 1.8!
  use s::Timeout       , 300
  use s::DefaultSite   , 'https://api.madmimi.com/'
  use s::DefaultQuery  , {}
  use s::Cache         , {}, 60 do
    use s::ErrorHandler, lambda { |env|
                           raise RestCore::MadMimi::Error.call(env) }
    use s::ErrorDetectorHttp
  end
  use s::Defaults      , :data => {}
end

class RestCore::MadMimi::Error < RestCore::Error
  include RestCore
  attr_reader :error, :code, :url
  def initialize error, code, url=''
    @error, @code, @url = error, code, url
    super("[#{code}] #{error.inspect} from #{url}")
  end

  def self.call env
    error, code, url = env[RESPONSE_BODY], env[RESPONSE_STATUS],
                       Middleware.request_uri(env)
    new(error, code, url)
  end
end

require 'rest-core/client/mad_mimi/audience_list'

module RestCore::MadMimi::Client
  include RestCore

  POSSIBLE_STATUSES = %w[ignorant sending failed sent received clicked_through
                         bounced retried retry_failed forwarded opted_out
                         abused]

  # https://madmimi.com/developer/mailer/transactional
  #
  # Usage:
  #
  #   client.mailer('<EMAIL>',
  #                 :subject => 'Subject',
  #                 :raw_html => 'the mail body [[tracking_beacon]]')
  #
  # The transaction code is convert to integer
  def mailer(recipient, options = {})
    options = {:recipients => recipient}.merge(options)
    response = post('mailer', options)
    # response was a string that included RestClient::AbstractResponse,
    # and it overrided #to_i method (which returns status code)
    String.new(response).to_i
  end

  # https://madmimi.com/developer/mailer/send-to-a-list
  #
  # Usage:
  #
  #   client.mailer_to_list('list_name',
  #                         :subject => 'Subject',
  #                         :raw_html => 'the mail body [[tracking_beacon]]')
  #
  # The transaction code is convert to integer
  def mailer_to_list(list, options = {})
    list = list.join(',') if list.is_a?(Array)
    options = {:list_name => list}.merge(options)
    response = post('mailer/to_list', options)
    # response was a string that included RestClient::AbstractResponse,
    # and it overrided #to_i method (which returns status code)
    String.new(response).to_i
  end

  # https://madmimi.com/developer/mailer/status
  #
  # Usage:
  #
  #   id = client.mailer(...)
  #   client.status(id)
  def status(id)
    get("mailers/status/#{id.to_i}")
  end

  # https://madmimi.com/developer/lists
  # Audience lists apis

  def audience_lists
    response = get('audience_lists/lists.xml')
    Crack::XML.parse(response.body)['lists']['list'].map do |list|
      RestCore::MadMimi::AudienceList.new(self, list)
    end
  end

  def create_audience_list(name)
    post('audience_lists', :name => name).tap{}
    RestCore::MadMimi::AudienceList.new(self, :name => name)
  end

  def rename_audience_list(name, new_name)
    post("audience_lists/#{CGI.escape(name)}/rename", :name => new_name).tap{}
    RestCore::MadMimi::AudienceList.new(self, :name => new_name)
  end

  def destroy_audience_list(name)
    post("audience_lists/#{CGI.escape(name)}", :_method => 'delete').tap{}
  end

  # https://madmimi.com/developer/lists
  # Audience lists apis (members)

  def add_member_to_audience_list(list, email, options = {})
    options = {:email => email}.merge(options)
    post("audience_lists/#{CGI.escape(list)}/add", options).tap{}
  end

  def remove_member_from_audience_list(list, email, options = {})
    options = {:email => email}.merge(options)
    post("audience_lists/#{CGI.escape(list)}/remove", options).tap{}
  end

  def query
    {'username' => username,
     'api_key' => api_key,
     'promotion_name' => promotion_name}
  end
end

RestCore::MadMimi.send(:include, RestCore::MadMimi::Client)<commit_msg>Update Madmimi core structure (based on Facebook)<commit_after>require 'rest-core'
require 'crack/xml'

module RestCore
  MadMimi = Builder.client(:data, :username, :api_key, :promotion_name) do
    use Timeout       , 300
    use DefaultSite   , 'https://api.madmimi.com/'
    use DefaultQuery  , {}
    use Cache         , {}, 60 do
      use ErrorHandler, lambda { |env| MadMimi::Error.call(env) }
      use ErrorDetectorHttp
    end
    use Defaults      , :data => {}
  end
end

class RestCore::MadMimi::Error < RestCore::Error
  include RestCore
  attr_reader :error, :code, :url
  def initialize error, code, url=''
    @error, @code, @url = error, code, url
    super("[#{code}] #{error.inspect} from #{url}")
  end

  def self.call env
    error, code, url = env[RESPONSE_BODY], env[RESPONSE_STATUS],
                       Middleware.request_uri(env)
    new(error, code, url)
  end
end

require 'rest-core/client/mad_mimi/audience_list'

module RestCore::MadMimi::Client
  include RestCore

  POSSIBLE_STATUSES = %w[ignorant sending failed sent received clicked_through
                         bounced retried retry_failed forwarded opted_out
                         abused]

  # https://madmimi.com/developer/mailer/transactional
  #
  # Usage:
  #
  #   client.mailer('<EMAIL>',
  #                 :subject => 'Subject',
  #                 :raw_html => 'the mail body [[tracking_beacon]]')
  #
  # The transaction code is convert to integer
  def mailer(recipient, options = {})
    options = {:recipients => recipient}.merge(options)
    response = post('mailer', options)
    # response was a string that included RestClient::AbstractResponse,
    # and it overrided #to_i method (which returns status code)
    String.new(response).to_i
  end

  # https://madmimi.com/developer/mailer/send-to-a-list
  #
  # Usage:
  #
  #   client.mailer_to_list('list_name',
  #                         :subject => 'Subject',
  #                         :raw_html => 'the mail body [[tracking_beacon]]')
  #
  # The transaction code is convert to integer
  def mailer_to_list(list, options = {})
    list = list.join(',') if list.is_a?(Array)
    options = {:list_name => list}.merge(options)
    response = post('mailer/to_list', options)
    # response was a string that included RestClient::AbstractResponse,
    # and it overrided #to_i method (which returns status code)
    String.new(response).to_i
  end

  # https://madmimi.com/developer/mailer/status
  #
  # Usage:
  #
  #   id = client.mailer(...)
  #   client.status(id)
  def status(id)
    get("mailers/status/#{id.to_i}")
  end

  # https://madmimi.com/developer/lists
  # Audience lists apis

  def audience_lists
    response = get('audience_lists/lists.xml')
    Crack::XML.parse(response.body)['lists']['list'].map do |list|
      RestCore::MadMimi::AudienceList.new(self, list)
    end
  end

  def create_audience_list(name)
    post('audience_lists', :name => name).tap{}
    RestCore::MadMimi::AudienceList.new(self, :name => name)
  end

  def rename_audience_list(name, new_name)
    post("audience_lists/#{CGI.escape(name)}/rename", :name => new_name).tap{}
    RestCore::MadMimi::AudienceList.new(self, :name => new_name)
  end

  def destroy_audience_list(name)
    post("audience_lists/#{CGI.escape(name)}", :_method => 'delete').tap{}
  end

  # https://madmimi.com/developer/lists
  # Audience lists apis (members)

  def add_member_to_audience_list(list, email, options = {})
    options = {:email => email}.merge(options)
    post("audience_lists/#{CGI.escape(list)}/add", options).tap{}
  end

  def remove_member_from_audience_list(list, email, options = {})
    options = {:email => email}.merge(options)
    post("audience_lists/#{CGI.escape(list)}/remove", options).tap{}
  end

  def query
    {'username' => username,
     'api_key' => api_key,
     'promotion_name' => promotion_name}
  end
end

class RestCore::MadMimi
  include RestCore::MadMimi::Client
end
{'fraction_non_alphanumeric': 0.13762957412264268, 'fraction_numerical': 0.0014986886474334957, 'mean_word_length': 3.1299638989169676, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '950144', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2621, 'n_words': 560}