Sha256: 94186e6656c52aba46633c9d784ac25700e7d290c8ccc7595e4ca530ce100020

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module NYTimes
	module Congress
		class Legislator < Base
      include AttributeTransformation
			ATTRIBUTE_MAP = { :date_for    =>  [:date_of_birth],
                        :roles_for   =>  [:roles],
                			  :integer_for =>  [:govtrack_id, :district],
                			  :string_for  =>  [:url, :state, :gender, :name, :party] }
                			  
      ATTRIBUTES = ATTRIBUTE_MAP.values.flatten
      ATTRIBUTES.each {|attribute| define_lazy_reader_for_attribute_named attribute }
      
      def self.find(id)
        response = invoke("members/#{id}.json")['results']        
				new(response.first)
      end
		  attr_reader :attributes, :id
		  
			def initialize(args={})
        @attributes = {}
  			@id = args.delete('id') || args.delete('member_id')
  			transformed = transform(args, ATTRIBUTE_MAP)
				transformed.each_pair do |attribute_name, attribute_value|
				  attributes[attribute_name.to_sym] = attribute_value
				end
				@fully_loaded = false
			end
      
      def positions
        response = Base.invoke("members/#{id}/votes.json")
        response = response['results'].first['votes']
        positions_for(response)
      end
      
      def votes
        response = Base.invoke("members/#{id}/votes.json")
        response = response['results'].first['votes']
        votes_for(response)
      end
  		
			private
			  attr_reader :fully_loaded
			  
			  def fully_loaded?
			    fully_loaded
			  end
			  
	  		def load_fully
	  		  full_legislator = self.class.find(id)
	  		  attributes.merge!(full_legislator.attributes)
	  		  @fully_loaded = true
  			end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hoverbird-ny-times-congress-1.0.0 lib/ny-times/congress/legislator.rb
hoverbird-ny-times-congress-1.1.0 lib/ny-times/congress/legislator.rb