Ruby OpenStates NoMethodError - 未定义的方法`map'

时间:2016-12-17 19:09:27

标签: ruby-on-rails ruby rubygems

我遇到过曾经工作过的Ruby gem的问题,但现在还没有。它的开放状态https://rubygems.org/gems/openstates。寻找立法者的代码似乎不再适用。我们已经将这段代码生产了一段时间,但现在收到此错误

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 个答案:

答案 0 :(得分: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",
    ...