Sha256: f5a62e48c7bdcf54e0f257f0ee64fba08b49308739a6c56420dd97d39e71fe1a

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

module NYTimes
	module Congress
		class Legislator < Base
      include AttributeTransformation
      attr_reader :attributes, :id
		  
			ATTRIBUTE_MAP = { 
			  :date_for    =>  [:date_of_birth],
        :roles_for   =>  [:roles],
			  :integer_for =>  [:govtrack_id, :district],
			  :string_for  =>  [:url, :state, :gender, :name, :party, :missed_votes_pct, :votes_with_party_pct] 
			}           			  
      ATTRIBUTES = ATTRIBUTE_MAP.values.flatten
      ATTRIBUTES.each {|attribute| define_lazy_reader_for_attribute_named(attribute) }
      
      def self.find(id)
        response = invoke("members/#{id}.json")
				new(response['results'].first)
      end
      
  		def initialize(args={})
  		  prepare_arguments(args)
  		  @attributes = {}
				@transformed_arguments.each_pair {|name, value| attributes[name.to_sym] = value }
				@fully_loaded = false
			end
      
      def to_s
    	  id
    	end
      
      def positions
        @positions ||= fetch_positions
      end
      alias votes positions

			private
			  attr_reader :fully_loaded
			  
			  def prepare_arguments(hash)
			    args = hash.dup
    			@id = args.delete('member_id') || args.delete('id')
    			raise ArgumentError, "could not assign ID" unless @id.is_a?(String)
    			@transformed_arguments = transform(args, ATTRIBUTE_MAP)
    	  end
			  
			  def fully_loaded?
			    fully_loaded
			  end
			  
	  		def load_fully
	  		  full_legislator = Legislator.find(id)
	  		  attributes.merge!(full_legislator.attributes)
	  		  @fully_loaded = true
  			end
        
        def fetch_positions
          response = Base.invoke("members/#{id}/votes.json")
          response = response['results'].first['votes']
          positions_for(response)
        end
      #end of private methods
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hoverbird-ny-times-congress-1.3.0 lib/ny-times/congress/legislator.rb
hoverbird-ny-times-congress-1.3.1 lib/ny-times/congress/legislator.rb