Module: KanazawaCity::Infra

Includes:
HTTParty
Defined in:
lib/kanazawa_city-infra.rb,
lib/kanazawa_city/infra/version.rb

Constant Summary collapse

API_VERSION =
'v1'
VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.facilities(options = {}) ⇒ Object



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

.facility(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kanazawa_city-infra.rb', line 90

def facility(options={})
  options[:lang] ||= "ja"

  res = JSON.parse(get("/#{API_VERSION}/facilities/#{options[:id]}.json",
                       :query => {:lang => options[:lang]}).body)

  f = res["facility"]
  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.freeze
end

.genres(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kanazawa_city-infra.rb', line 15

def genres(options={})
  options[:lang] ||= "ja"

  res = JSON.parse(get("/#{API_VERSION}/genres/list.json", :query => options).body)

  res["genres"].map do |g|
    that = self
    g[:query_id] = g["id"]
    g = Hashie::Mash.new(g)
    g.subgenres.map! do |sg|
      sg["query_id"] = "#{g["id"]}-#{sg["id"]}"
      sg
    end
    g.freeze
  end
end