Created
September 10, 2015 14:24
-
-
Save novohispano/8aa59ef168784cbed7f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Commitee < OpenStruct | |
| attr_reader :service | |
| def self.service | |
| @service ||= SunlightService.new | |
| end | |
| def self.find_by(params) | |
| service.committees(params).map { |commitee| Commitee.new(commitee) } | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './test/test_helper' | |
| class CommiteeTest < ActiveSupport::TestCase | |
| test '#find_by' do | |
| VCR.use_cassette('commitee#find_by') do | |
| commitees = Commitee.find_by(chamber: 'senate') | |
| commitee = commitees.first | |
| assert_equal 20, commitees.count | |
| assert_equal Commitee, commitee.class | |
| assert_equal 'Regulatory Affairs and Federal Management', commitee.name | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Legislator < OpenStruct | |
| attr_reader :service | |
| def self.service | |
| @service ||= SunlightService.new | |
| end | |
| def self.find_by(params) | |
| service.legislators(params).map { |legislator| Legislator.new(legislator) } | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './test/test_helper' | |
| class LegislatorTest < ActiveSupport::TestCase | |
| test '#find_by' do | |
| VCR.use_cassette('legislator#find_by') do | |
| legislators = Legislator.find_by(gender: 'F') | |
| legislator = legislators.first | |
| assert_equal 20, legislators.count | |
| assert_equal Legislator, legislator.class | |
| assert_equal 'Joni', legislator.first_name | |
| assert_equal 'Ernst', legislator.last_name | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SunlightService | |
| attr_reader :connection | |
| def initialize | |
| @connection = Hurley::Client.new('http://congress.api.sunlightfoundation.com') | |
| connection.query[:apikey] = ENV['sunlight_api_key'] | |
| end | |
| def legislators(params) | |
| parse(connection.get('legislators', params))[:results] | |
| end | |
| def committees(params) | |
| parse(connection.get('committees', params))[:results] | |
| end | |
| private | |
| def parse(response) | |
| JSON.parse(response.body, symbolize_names: true) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './test/test_helper' | |
| class SunlightServiceTest < ActiveSupport::TestCase | |
| attr_reader :service | |
| def setup | |
| @service = SunlightService.new | |
| end | |
| test '#legislators' do | |
| VCR.use_cassette('schoolist_service#legislators') do | |
| legislators = service.legislators(gender: 'F') | |
| legislator = legislators.first | |
| assert_equal 20, legislators.count | |
| assert_equal 'Joni', legislator[:first_name] | |
| assert_equal 'Ernst', legislator[:last_name] | |
| end | |
| end | |
| test '#committees' do | |
| VCR.use_cassette('schoolist_service#commitees') do | |
| committees = service.committees(chamber: 'senate') | |
| committee = committees.first | |
| assert_equal 20, committees.count | |
| assert_equal 'Regulatory Affairs and Federal Management', committee[:name] | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment