Class: GovTrack::Base

Inherits:
Object show all
Includes:
HTTParty
Defined in:
lib/govtrack/base.rb

Direct Known Subclasses

Bill, Person, Role, Vote, VoteVoter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
13
# File 'lib/govtrack/base.rb', line 7

def initialize(attributes=nil)
  attributes ||= {}
  attributes.each do |(attr, val)|
    instance_variable_set("@#{attr}", val)
    instance_eval "def #{attr}() @#{attr} end" unless self.respond_to?(attr)
  end
end

Class Method Details

.find(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/govtrack/base.rb', line 15

def self.find(args)
  args[:limit] ||= 500 if block_given? #default to queries of 500 when a block is given
  
  response = get("/#{self.demodulized_name}/?#{URI.escape(URI.encode_www_form(args))}")
  paginated_list = GovTrack::PaginatedList.new(self,response["meta"],response["objects"])
  
  if block_given?
    page = paginated_list.offset
    for page in paginated_list.offset..(paginated_list.total/paginated_list.limit)
      args[:offset] = page
      if page == (paginated_list.total/paginated_list.limit).to_i #don't supply the end of the last page
        self.find(args).first(paginated_list.total%paginated_list.limit).each { |item| yield item }
      else
        self.find(args).each { |item| yield item }
      end
    end
  else
    paginated_list
  end
end

.find_by_id(id) ⇒ Object



36
37
38
# File 'lib/govtrack/base.rb', line 36

def self.find_by_id(id)
  new(get("/#{self.demodulized_name}/#{id}"))
end

.find_by_uri(uri) ⇒ Object



40
41
42
# File 'lib/govtrack/base.rb', line 40

def self.find_by_uri(uri)
  new(get("http://www.govtrack.us#{uri}"))
end

.method_missing(method_id, *arguments) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/govtrack/base.rb', line 52

def self.method_missing(method_id, *arguments)
  #generates dynamic find_by methods, ActiveRecord style.
  if method_id.to_s =~ /^find_by_([_a-zA-Z]\w*)$/
    attribute_names = $1.split '_and_'
    conditions = {}
    attribute_names.each_with_index do |name,index|
      conditions[name.to_sym] = arguments[index]
    end
    self.find(conditions)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/govtrack/base.rb', line 44

def ==(other)
  other.equal?(self) || (other.instance_of?(self.class) && other.id == id)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/govtrack/base.rb', line 48

def eql?(other)
  self == other
end