title: Ruby on Rails -- NilClass error on Faraday GET request
body: <p>Using Rails 5.1.4, Ruby 2.4</p>
  
  <p>I am attempting to connect to an API using Faraday which I've done before without issue, but I'm getting the following error when I run RSpec:</p>
  
  <p>SearchController GET #index returns http success
       Failure/Error: response = @conn.get("/congress/v1/members/house/#{state}/current.json")</p>
  
  <pre><code> NoMethodError:
     undefined method `get' for nil:NilClass
     Did you mean?  gem
   # ./app/controllers/search_controller.rb:9:in `block in index'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday/connection.rb:88:in `initialize'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
   # ./app/controllers/search_controller.rb:5:in `index'
   # ./spec/controllers/search_controller_spec.rb:7:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>2) user can search for house members user submits a valid state name
       Failure/Error: response = @conn.get("/congress/v1/members/house/#{state}/current.json")</p>
  
  <pre><code> NoMethodError:
     undefined method `get' for nil:NilClass
     Did you mean?  gem
   # ./app/controllers/search_controller.rb:9:in `block in index'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday/connection.rb:88:in `initialize'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
   # ./app/controllers/search_controller.rb:5:in `index'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/etag.rb:25:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/conditional_get.rb:25:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/head.rb:12:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:232:in `context'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:226:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/urlmap.rb:68:in `block in call'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `each'
   # /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `call'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/mock_session.rb:30:in `request'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/test.rb:249:in `process_request'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/test.rb:59:in `get'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:69:in `process'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:41:in `process_and_follow_redirects'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:32:in `submit'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/form.rb:78:in `submit'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/node.rb:64:in `click'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/element.rb:143:in `block in click'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/base.rb:85:in `synchronize'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/element.rb:143:in `click'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/actions.rb:25:in `click_link_or_button'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/session.rb:776:in `block (2 levels) in &lt;class:Session&gt;'
   # /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/dsl.rb:50:in `block (2 levels) in &lt;module:DSL&gt;'
   # ./spec/features/user_can_search_by_state_spec.rb:11:in `block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>Here is my Search controller, where the logic resides until I can fix this error and extract it elsewhere:</p>
  
  <pre><code>class SearchController &lt; ApplicationController
    def index
      state = params[:state]
      @conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
        faraday.headers["X-API-KEY"] = "&lt;$MY-API-KEY&gt;"
        faraday.adapter Faraday.default_adapter
  
        response = @conn.get("/congress/v1/members/house/#{state}/current.json")
  
      results = JSON.parse(response.body, symbolize_names: true)[:results]
      @members = results.map do |result|
        Member.new(result)
      end
      end
    end
  end
  </code></pre>
  
  <p>And to be thorough, here is my Member model:</p>
  
  <pre><code>class Member
    attr_reader :name,
                :role,
                :party,
                :district,
                :seniority
  
    def initialize(attributes = {})
      @name = attributes[:name]
      @role = attributes[:role]
      @party = attributes[:party]
      @district = attributes[:district]
      @seniority = attributes[:seniority]
  
    end
  end
  </code></pre>