Class: Everypolitician::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/everypolitician/index.rb

Constant Summary collapse

DEFAULT_INDEX_URL =
'https://raw.githubusercontent.com/' \
'everypolitician/everypolitician-data/master/countries.json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index_url: DEFAULT_INDEX_URL) ⇒ Index

Returns a new instance of Index.



11
12
13
# File 'lib/everypolitician/index.rb', line 11

def initialize(index_url: DEFAULT_INDEX_URL)
  @index_url = index_url
end

Instance Attribute Details

#index_urlObject (readonly)

Returns the value of attribute index_url.



9
10
11
# File 'lib/everypolitician/index.rb', line 9

def index_url
  @index_url
end

Instance Method Details

#all_legislaturesObject



27
28
29
# File 'lib/everypolitician/index.rb', line 27

def all_legislatures
  @all_legislatures ||= countries.flat_map(&:legislatures)
end

#countriesObject



19
20
21
22
23
24
25
# File 'lib/everypolitician/index.rb', line 19

def countries
  @countries ||= begin
    JSON.parse(open(index_url).read, symbolize_names: true).map do |c|
      Country.new(c)
    end
  end
end

#country(slug) ⇒ Object



15
16
17
# File 'lib/everypolitician/index.rb', line 15

def country(slug)
  country_index[slug.to_s.downcase]
end