<commit_before>require 'active_support/core_ext/hash/keys'
require 'clever_elements/model'

module CleverElements
  class Subscriber < Model
    class << self
      def all list_id = CleverElements::List.default_id
        response = proxy.get_subscriber :listID => list_id
        
        ids = if Array === response[:item]
          response[:item].map do |subscriber_response|
            instantiate subscriber_response, list_id
          end
        elsif Hash === response[:item]
          [instantiate(response[:item], list_id)]
        else
          []
        end
      rescue Savon::SOAP::Fault
        []
      end
      
      def all_unsubscribed list_id = CleverElements::List.default_id
        response = proxy.get_subscriber_unsubscribes :listID => list_id
        
        ids = if Array === response[:item]
          response[:item].map do |subscriber_response|
            instantiate subscriber_response, list_id
          end
        elsif Hash === response[:item]
          [instantiate(response[:item], list_id)]
        else
          []
        end
      rescue Savon::SOAP::Fault
        []
      end
      
      protected
      def instantiate attributes, list_id = CleverElements::List.default_id
        attributes = {
          :id => attributes[:subscriber_id],
          :email => attributes[:subscriber_email],
          :list_id => list_id
        }
        
        new attributes
      end
    end
    
    attr_accessor :id, :email, :list_id
    
    def initialize attributes = {}
      @id, @email, @list_id = attributes.symbolize_keys.values_at(:id, :email, :list_id)
    end
    
    def list
      @list ||= list_id && CleverElements::List.find(list_id)
    end
    
    def create
      create_with :add_subscriber
    end
    
    def create_doi
      create_with :add_subscriber_doi
    end
    
    def destroy
      response = proxy.delete_subscriber :subscriberIDListShort => { :item => { :subscriberID => id }}
      
      if response == '200'
        true
      else
        false
      end
    rescue Savon::SOAP::Fault
      false
    end
    
    def unsubscribe
      return false unless list_id
      
      unsubscribe_from list_id
    end
    
    def unsubscribe_from list_or_id
      raise 'FIXME'
      list_id = if CleverElements::List === list_or_id
        list_or_id.id
      else
        list_or_id
      end
      
      response = proxy.unsubscribe_subscriber_from_list :subscriberIDList => { :item => { :subscriberID => id, :listID => list_id}}
      
      if response == '200'
        @list = @list_id = nil if @list_id == list_id
        true
      else
        false
      end
    rescue Savon::SOAP::Fault
      false
    end
    
    def unsubscribe_from_all
      response = proxy.unsubscribe_subscriber_from_all :subscriberIDList => { :item => { :subscriberID => id }}
      
      if response == '200'
        true
      else
        false
      end
    rescue Savon::SOAP::Fault<commit_msg>default list id for subscriber<commit_after>require 'active_support/core_ext/hash/keys'
require 'clever_elements/model'

module CleverElements
  class Subscriber < Model
    class << self
      def all list_id = CleverElements::List.default_id
        response = proxy.get_subscriber :listID => list_id
        
        ids = if Array === response[:item]
          response[:item].map do |subscriber_response|
            instantiate subscriber_response, list_id
          end
        elsif Hash === response[:item]
          [instantiate(response[:item], list_id)]
        else
          []
        end
      rescue Savon::SOAP::Fault
        []
      end
      
      def all_unsubscribed list_id = CleverElements::List.default_id
        response = proxy.get_subscriber_unsubscribes :listID => list_id
        
        ids = if Array === response[:item]
          response[:item].map do |subscriber_response|
            instantiate subscriber_response, list_id
          end
        elsif Hash === response[:item]
          [instantiate(response[:item], list_id)]
        else
          []
        end
      rescue Savon::SOAP::Fault
        []
      end
      
      protected
      def instantiate attributes, list_id = CleverElements::List.default_id
        attributes = {
          :id => attributes[:subscriber_id],
          :email => attributes[:subscriber_email],
          :list_id => list_id
        }
        
        new attributes
      end
    end
    
    attr_accessor :id, :email, :list_id
    
    def initialize attributes = {}
      @id, @email, @list_id = attributes.symbolize_keys.values_at(:id, :email, :list_id)
      @list_id ||= CleverElements::List.default_id
    end
    
    def list
      @list ||= list_id && CleverElements::List.find(list_id)
    end
    
    def create
      create_with :add_subscriber
    end
    
    def create_doi
      create_with :add_subscriber_doi
    end
    
    def destroy
      response = proxy.delete_subscriber :subscriberIDListShort => { :item => { :subscriberID => id }}
      
      if response == '200'
        true
      else
        false
      end
    rescue Savon::SOAP::Fault
      false
    end
    
    def unsubscribe
      return false unless list_id
      
      unsubscribe_from list_id
    end
    
    def unsubscribe_from list_or_id
      raise 'FIXME, this should work, but it doesnt, im guessing api broken' 
      list_id = if CleverElements::List === list_or_id
        list_or_id.id
      else
        list_or_id
      end
      
      response = proxy.unsubscribe_subscriber_from_list :subscriberIDList => { :item => { :subscriberID => id, :listID => list_id}}
      
      if response == '200'
        @list = @list_id = nil if @list_id == list_id
        true
      else
        false
      end
    rescue Savon::SOAP::Fault
      false
    end
    
    def unsubscribe_from_all
      response = proxy.unsubscribe_subscriber_from_all :subscriberIDList => { :item => { :subscriberID => id }}
      
      if response == '200'
        true
      else
        false
      end
    rescue Savon::SOAP::Fault
{'fraction_non_alphanumeric': 0.08530961791831357, 'fraction_numerical': 0.0029644268774703555, 'mean_word_length': 1.9769607843137256, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1764542', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1784, 'n_words': 413}