下記課題を解きたい。
**(以下3つの
Web API
を利用し、)
ある「利用者ID」を与え、「利用者名」・「登録クラブ一覧」・「登録クラブポイント(point)」が
出力されるプログラムをオブジェクト指向プログラミングで設計・実装しなさい。
(URLは無効なURLです)**
https://riko-19950120.herokuapp.com/users/1
json
1/*レスポンス(例)*/ 2 { 3 attributes: { 4 id: 1, 5 name: "Rico", 6 club_ids: [ 7 1, 8 3, 9 5 10 ] 11 } 12 }
https://riko-19950120.herokuapp.com/users/1/clubs
json
1 2/*レスポンス(例)*/ 3[ 4 { 5 attributes: { 6 id: 1, 7 user_id: 1, 8 name: "ライザップクラブ", 9 point: 15000 10 } 11 }, 12 { 13 attributes: { 14 id: 3, 15 user_id: 1, 16 name: "シックスパッドクラブ", 17 point: 13000 18 } 19 }, 20 { 21 attributes: { 22 id: 5, 23 user_id: 1, 24 name: "ゴールドジムクラブ", 25 point: 52000 26 } 27 } 28]
https://riko-19950120.herokuapp.com/clubs/2
json
1/*レスポンス(例)*/ 2{ 3 attributes: { 4 id: 2, 5 user_id: 2, 6 name: "ビリーズブートキャンプクラブ", 7 point: 2300 8 } 9}
現状の回答(いずれもJSONを返す)
現状はhttps://riko-19950120.herokuapp.com/users/1
というリンクを設定することでJSON
を返されるプログラムを複数作ることに成功しました。しかし動的にユーザIDを渡す方法がわかっていません。
ruby:hoge1.rb
1# JSON応答されました。 2require 'net/http' 3require 'uri' 4require 'json' 5 6def get_json(location,limit=10) 7 8 raise ArgumentError, 'too many HTTP redirects' if limit == 0 9 uri = URI.parse(location) 10 begin 11 response = Net::HTTP.start(uri.host,uri.port,use_ssl:uri.scheme=='https') do |http| 12 http.open_timeout = 5 13 http.read_timeout = 10 14 http.get(uri.request_uri) 15 end 16 case response 17 when Net::HTTPSuccess 18 json = response.body 19 JSON.parse(json) 20 when Net::Redirection 21 location = response['loaction'] 22 warn "Redirected to #{location}" 23 get_json(location,limit-1) 24 else 25 puts [uri.to_s,response.value].join(':') 26 end 27 rescue => e 28 puts [uri.to_s,e.class,e] 29 end 30end 31 32puts get_json('https://riko-19950120.herokuapp.com/users/1') 33
ruby:hoge2.rb
1# JSON応答されました。 2require 'net/http' 3require 'uri' 4require 'json' 5 6uri = URI.parse('https://riko-19950120.herokuapp.com/users/1') 7 8https = Net::HTTP.new(uri.host,uri.port) 9https.use_ssl = true 10 11res = https.start { 12 https.get(uri.request_uri) 13} 14 15if res.code == '200' 16 result = JSON.parse(res.body) 17else 18 puts "OMG!! #{res.code} #{res.message}" 19end 20 21puts result
ruby:hoge3.rb
1# JSONが返されました。 2require 'net/http' 3require 'uri' 4require 'json' 5 6url = URI.parse('https://riko-19950120.herokuapp.com/users/1') 7json = Net::HTTP.get(url) 8result = JSON.parse(json) 9 10puts result 11 12# {"attributes"=>{"id"=>1, "name"=>"Alice", "club_ids"=>[1, 3, 5]}} 13puts result['attributes']['id'] # 1 14puts result['attributes']['name'] # Alice 15puts result['attributes']['club_ids'] # 1 3 5 16club_ids = result['attributes']['club_ids'] 17club_ids.each_with_index do |friend,index| 18 puts "Friend #{ index + 1 } : #{ friend }" 19end 20 21# club1 : 1 22# club2 : 3 23# club3 : 5 24
ruby:hoge4.rb
1# net/http ライブラリ(1) 2 3require 'net/http' 4require 'uri' 5 6Net::HTTP.get_print URI.parse('https://riko-19950120.herokuapp.com/users/1') 7# {"attributes":{"id":1,"name":"Alice","club_ids":[1,3,5]}} 8
分からないこと。
- 動的に「利用者ID」を与える方法
- それをオブジェクト指向プログラミングにすること
前回の質問から少しずつ解決に近づいてはいますが、
もしアドバイス頂ける方、いらっしゃったらよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。