32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/kanazawa_city-infra.rb', line 32
def facilities(options={})
options[:lang] ||= "ja"
options[:genre] &&=
if options[:genre].kind_of? Array
options[:genre].map { |g|
if g.respond_to? "query_id"
g.query_id
elsif g.kind_of?(String) || g.kind_of?(Integer)
g
else
raise ArgumentError
end
}.join(',')
elsif options[:genre].respond_to? "query_id"
options[:genre].query_id
elsif options[:genre].kind_of?(String) || options[:genre].kind_of?(Integer)
options[:genre]
else
raise ArgumentError
end
res = JSON.parse(get("/#{API_VERSION}/facilities/search.json", :query => options).body)
keyword = options[:keyword] || ""
next_page = res["next_page"] || ""
that = self
res = res["facilities"].map! do |f|
f["genres"].map! do |g|
g["query_id"] = g["id"]
g["subgenre"]["query_id"] = "#{g["id"]}-#{g["subgenre"]["id"]}" if g["subgenre"]
g
end
f = Hashie::Mash.new(f)
f.singleton_class.send(:define_method, :detail) do
that.facility(id: f["id"], lang: options[:lang])
end
f.freeze
end
res.singleton_class.send(:define_method, :"has_next?") do
! next_page.empty?
end
res.singleton_class.send(:define_method, :next) do
params = URI.parse(next_page).query.split('&')
query = params.inject({}) do |query, param|
k, v = param.split('=')
if k == "keyword"
query[k.to_sym] = URI.decode(keyword)
else
query[k.to_sym] = URI.decode(v)
end
query
end
that.facilities(query)
end
res
end
|