Skip to content

Commit

Permalink
Factor common location code into a method
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Apr 27, 2013
1 parent 4081bd8 commit 84fc56f
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions lib/congress/client.rb
Expand Up @@ -24,15 +24,7 @@ def legislators(options={})
# Congress.legislators_locate(94107)
# Congress.legislators_locate(37.775, -122.418)
def legislators_locate(*args)
options = args.last.is_a?(::Hash) ? args.pop : {}
if args.size == 1
options[:zip] = args.pop
elsif args.size == 2
options[:longitude] = args.pop
options[:latitude] = args.pop
else
raise ArgumentError, "Must pass a latitude/longitude or zip"
end
options = extract_location(args)
get('/legislators/locate', options.merge(api_key))
end

Expand All @@ -44,15 +36,7 @@ def legislators_locate(*args)
# Congress.districts_locate(94107)
# Congress.districts_locate(37.775, -122.418)
def districts_locate(*args)
options = args.last.is_a?(::Hash) ? args.pop : {}
if args.size == 1
options[:zip] = args.pop
elsif args.size == 2
options[:longitude] = args.pop
options[:latitude] = args.pop
else
raise ArgumentError, "Must pass a latitude/longitude or zip"
end
options = extract_location(args)
get('/districts/locate', options.merge(api_key))
end

Expand Down Expand Up @@ -126,11 +110,25 @@ def upcoming_bills(options={})
get('/upcoming_bills', options.merge(api_key))
end

private
private

def api_key
{:apikey => Congress.key}
end

def extract_location(args)
options = args.last.is_a?(::Hash) ? args.pop : {}
case args.size
when 1
options[:zip] = args.pop
when 2
options[:longitude] = args.pop
options[:latitude] = args.pop
else
raise ArgumentError, "Must pass a latitude/longitude or zip"
end
options
end

end
end

0 comments on commit 84fc56f

Please sign in to comment.