2016-12-17 104 views
1

我遇到過用於工作的Ruby寶石,但現在沒有問題。這是開放狀態https://rubygems.org/gems/openstates。尋找立法者的代碼似乎不再適用。我們已經有這段代碼在生產一段時間,但現在得到這個錯誤Ruby OpenStates NoMethodError - 未定義的方法`map'

NoMethodError - undefined method `map' for #<String:0x007ff0b51941c0> 

openstates (0.0.1) lib/openstates/model.rb:37:in `where' 

這是我們如何調用方法。

OpenStates::Legislator.where(parameters) 

parameters = {:state=>"Ca"} 
+1

如果這是工作,現在由於更新你已經完成了你的寶石,最新版本有一個錯誤,指定這個寶石的版本,這是不是在你的gemfile中的越野車,並應該工作 – amrdruid

+0

它仍然是我一直使用的寶石的相同版本。版本0.0.1 – user2974739

+0

我建議你用'gem'和'openstates'或'OpenStates'替換你的標籤'Rubygems'。 –

回答

1

你的代碼沒有改變,gem沒有改變,但API可能有。

這裏是一個非常基本的模塊,幫助你從OpenStates::Legislator.where(parameters)數組:

require 'open-uri' 
require 'active_support/core_ext/object' 

module OpenStates 
    class Legislator 
    @cache={} 
    class << self 
     def where(parameters) 
     url = "https://openstates.org/api/v1/legislators/?#{parameters.to_query}" 
     @cache[url] ||= get_json(url) 
     end 

     private 

     def get_json(url) 
     puts "# Downloading #{url}" 
     sleep 2 
     JSON.load(open(url)) 
     end 
    end 
    end 
end 

parameters = {:state=>"Ca"} 
p OpenStates::Legislator.where(parameters).first(2) 

它輸出:不,這可能發生

# Downloading https://openstates.org/api/v1/legislators/?state=Ca 
[ 
    { 
        "last_name" => "Gatto", 
       "updated_at" => "2016-09-24 07:16:00", 
     "nimsp_candidate_id" => nil, 
        "full_name" => "Mike Gatto", 
         "id" => "CAL000123", 
       "first_name" => "Mike", 
       "middle_name" => "", 
        "district" => "43", 
        "chamber" => "lower", 
         "state" => "ca", 
       "votesmart_id" => "120220", 
         "party" => "Democratic", 
      "+capitol_office" => { 
      "phone" => "(916) 319-2043", 
      "street" => "P.O. Box 942849, Room 4140", 
       "zip" => "94249-0043", 
       "city" => "Sacramento" 
     }, 
        "all_ids" => [ 
      "CAL000123", 
      "CAL000246", 
      "CAL000246", 
      "CAL000367" 
     ], 
        "leg_id" => "CAL000123", 
        "active" => true, 
     "transparencydata_id" => "76584a5322274b9b892642b7b6ae3db5", 
        "photo_url" => "http://assembly.ca.gov/sites/assembly.ca.gov/files/memberphotos/AD43.jpg", 
      "+district_offices" => [ 
      { 
       "phone" => "(818) 240-6330", 
       "street" => "300 East Magnolia, Suite 504", 
        "zip" => "91502", 
        "city" => "Burbank" 
      } 
     ], 
         "url" => "http://asmdc.org/members/a43", 
        "country" => "us", 
       "created_at" => "2012-01-31 19:25:09", 
         "level" => "state", 
        "+district" => "43", 
        "offices" => [ 
      { 
        "fax" => nil, 
        "name" => "Capitol Office", 
        "phone" => "916-319-2043", 
       "address" => "P.O. Box 942849, Room 5136\nSacramento, CA 94249-0043", 
        "type" => "capitol", 
        "email" => nil 
      }, 
      { 
        "fax" => nil, 
        "name" => "District Office", 
        "phone" => "818-558-3043", 
       "address" => "300 East Magnolia Blvd, Suite 504\nBurbank, CA 91502", 
        "type" => "district", 
        "email" => nil 
      } 
     ], 
        "+party" => "Democratic", 
        "suffixes" => "" 
    }, 
    { 
        "last_name" => "Gordon", 
       "updated_at" => "2016-09-24 07:16:01", 
    ... 
相關問題