Sha256: 39fc57e9b11d7bd199a3185a4150fd16a81c1aebd258eb674bd460b74bb839a3

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

module ChurchCommunityBuilder

  class IndividualList

    include Enumerable

    attr_reader :request_data,
                :response_data,
                :service,
                :individuals,
                :count,
                :individual_array,
                :json_data #for debugging


    def initialize(json)
      @json_data = json["ccb_api"]
      @request_data = @json_data["request"]
      @response_data = @json_data["response"]
      @service = @response_data["service"] #CCB service type accessed

      @individuals  = @response_data['individuals']

      @count = @individuals["count"].to_i #number of records

      # if @individuals['individual'] is a single item, it will be returned
      # as a Hash, rather than a single element Array, containing the Hash.
      #
      if @individuals["individual"].is_a?(Array)
        @individual_array = @individuals["individual"]
      
      elsif @individuals["individual"].is_a?(Hash)
        @individual_array = []
        @individual_array << @individuals["individual"] #array of each individual
      end

    end

    def all_names
      return [] unless @individual_array
      @individual_array.collect { |individual| [individual['first_name'], individual['last_name']].join(' ') }
    end

    def ids
      (@individual_array.collect { |individual| individual['id'] }).uniq
    end

    def [](index)
      Individual.new( @individual_array[index] ) if @individual_array and @individual_array[index]
    end


    # This method is needed for Enumerable.
    def each &block
      @individual_array.each{ |individual| yield( Individual.new(individual) )}
    end


    def empty?
      self.count == 0 ? true : false
    end

  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
church-community-builder-0.2.2 lib/api/individual_list.rb
church-community-builder-0.2.1 lib/api/individual_list.rb
church-community-builder-0.2.0 lib/api/individual_list.rb
church-community-builder-0.1.6 lib/api/individual_list.rb
church-community-builder-0.1.5 lib/api/individual_list.rb
church-community-builder-0.1.4 lib/api/individual_list.rb
church-community-builder-0.1.3 lib/api/individual_list.rb
church-community-builder-0.1.2 lib/api/individual_list.rb
church-community-builder-0.1.1 lib/api/individual_list.rb
church-community-builder-0.1.0 lib/api/individual_list.rb