65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/pemilu/api.rb', line 65
def candidate(id)
uri = URI("http://api.pemiluapi.org/candidate/api/caleg/#{id}")
params = { apiKey: @key }
uri.query = URI.encode_www_form(params)
respond = Net::HTTP.get_response(uri)
return "Invalid request error. Please check your API key" if respond.is_a?(Net::HTTPUnauthorized)
return "Can't get Candidate with id: #{id}" if respond.is_a?(Net::HTTPInternalServerError)
data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
candidate = data["data"]["results"]["caleg"][0]
return Pemilu::Candidate.new(
id: candidate["id"],
name: candidate["nama"],
gender: candidate["jenis_kelamin"],
religion: candidate["agama"],
birthplace: candidate["tempat_lahir"],
date_of_birth: candidate["tanggal_lahir"],
marital_status: candidate["status_perkawinan"],
name_of_couples: candidate["nama_pasangan"],
number_of_children: candidate["jumlah_anak"].to_i,
village: candidate["kelurahan_tinggal"],
sub_district: candidate["kecamatan_tinggal"],
district: candidate["kab_kota_tinggal"],
province: {
"id" => candidate["provinsi"]["id"],
"name" => candidate["provinsi"]["nama"]
},
electoral_district: {
"id" => candidate["dapil"]["id"],
"name" => candidate["dapil"]["nama"]
},
election_year: candidate["tahun"],
legislative_body: candidate["lembaga"],
party: {
"id" => candidate["partai"]["id"].to_i,
"name" => candidate["partai"]["nama"]
},
ordinal: candidate["urutan"],
picture: candidate["foto_url"],
educations: candidate["riwayat_pendidikan"],
jobs: candidate["riwayat_pekerjaan"],
organizations: candidate["riwayat_organisasi"])
end
|