Class: Pemilu::API

Inherits:
Object
  • Object
show all
Defined in:
lib/pemilu/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil) ⇒ API

Returns a new instance of API.

[View source]

6
7
8
# File 'lib/pemilu/api.rb', line 6

def initialize(key: nil)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.


4
5
6
# File 'lib/pemilu/api.rb', line 4

def key
  @key
end

Instance Method Details

#candidate(id) ⇒ Object

[View source]

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

#candidates(name: nil, party: nil, electoral_district: nil, election_year: nil, province: nil, gender: nil, religion: nil, legislative_body: nil, limit: 100, offset: nil) ⇒ Object

[View source]

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/pemilu/api.rb', line 10

def candidates(name: nil, party: nil, electoral_district: nil, election_year: nil, province: nil, gender: nil, religion: nil, legislative_body: nil, limit: 100, offset: nil)

  uri = URI("http://api.pemiluapi.org/candidate/api/caleg")
  params = {
    apiKey: @key,
    nama: name,
    partai: party,
    dapil: electoral_district,
    tahun: election_year,
    provinsi: province,
    jenis_kelamin: gender,
    agama: religion,
    lembaga: legislative_body,
    limit: limit,
    offset: offset
  }
  params.delete_if{ |k,v| v.nil? }
  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)
  result = []
  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  @total_candidate = data["data"]["results"]["total"]
  candidates = data["data"]["results"]["caleg"]
  candidates.each do |caleg|
    result << Pemilu::Candidate.new(
      id: caleg["id"],
      name: caleg["nama"],
      gender: caleg["jenis_kelamin"],
      religion: caleg["agama"],
      birthplace: caleg["tempat_lahir"],
      date_of_birth: caleg["tanggal_lahir"],
      marital_status: caleg["status_perkawinan"],
      name_of_couples: caleg["nama_pasangan"],
      number_of_children: caleg["jumlah_anak"],
      village: caleg["kelurahan_tinggal"],
      sub_district: caleg["kecamatan_tinggal"],
      district: caleg["kab_kota_tinggal"],
      province: {
        "id" => caleg["provinsi"]["id"],
        "name" => caleg["provinsi"]["nama"]
        },
      electoral_district: {
        "id" => caleg["dapil"]["id"],
        "nama" => caleg["dapil"]["nama"]
        },
      election_year: caleg["tahun"],
      legislative_body: caleg["lembaga"],
      party: caleg["partai"],
      ordinal: caleg["urutan"],
      picture: caleg["foto_url"])
  end
  return result
end

#electoral_district(id) ⇒ Object

[View source]

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/pemilu/api.rb', line 227

def electoral_district(id)
  uri = URI("http://api.pemiluapi.org/candidate/api/dapil/#{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 Electoral District with id: #{id}" if respond.is_a?(Net::HTTPInternalServerError)

  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  ed = data["data"]["results"]["dapil"][0]
  return Pemilu::ElectoralDistrict.new(
      id: ed["id"],
      name: ed["nama"],
      full_name: ed["nama_lengkap"],
      legislative_body: ed["nama_lembaga"],
      available_chairs: ed["jumlah_kursi"],
      population: ed["jumlah_penduduk"],
      province: {
        "id" => ed["provinsi"]["id"],
        "name" => ed["provinsi"]["nama"]
      })
end

#electoral_districts(province: nil, legislative_body: nil) ⇒ Object

[View source]

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/pemilu/api.rb', line 195

def electoral_districts(province: nil, legislative_body: nil)

  uri = URI("http://api.pemiluapi.org/candidate/api/dapil")
  params = {
    apiKey: @key,
    lembaga: legislative_body,
    provinsi: province
  }
  params.delete_if{ |k,v| v.nil? }
  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)
  result = []
  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  @total_election_districts = data["data"]["results"]["count"]
  eds = data["data"]["results"]["dapil"]
  eds.each do |ed|
    result << Pemilu::ElectoralDistrict.new(
      id: ed["id"],
      name: ed["nama"],
      full_name: ed["nama_lengkap"],
      legislative_body: ed["nama_lembaga"],
      available_chairs: ed["jumlah_kursi"],
      population: ed["jumlah_penduduk"],
      province: {
        "id" => ed["provinsi"]["id"],
        "name" => ed["provinsi"]["nama"]
      })
  end
  return result
end

#partiesObject

[View source]

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pemilu/api.rb', line 108

def parties
  uri = URI("http://api.pemiluapi.org/candidate/api/partai")
  params = { apiKey: @key }
  uri.query = URI.encode_www_form(params)
  respond = Net::HTTP.get_response(uri)
  result = []
  return "Invalid request error. Please check your API key" if respond.is_a?(Net::HTTPUnauthorized)

  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  @total_parties = data["data"]["results"]["count"]
  parties = data["data"]["results"]["partai"]
  parties.each do |party|
    result << Pemilu::Party.new(
      id: party["id"].to_i,
      nick_name: party["nama"],
      full_name: party["nama_lengkap"],
      url: party["url_situs"],
      facebook: party["url_facebook"],
      twitter: party["url_twitter"]
    )
  end
  return result
end

#party(id) ⇒ Object

[View source]

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pemilu/api.rb', line 132

def party(id)
  uri = URI("http://api.pemiluapi.org/candidate/api/partai/#{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 Party with id: #{id}" if respond.is_a?(Net::HTTPBadRequest)
  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  party = data["data"]["results"]["partai"][0]
  return Pemilu::Party.new(
      id: party["id"].to_i,
      nick_name: party["nama"],
      full_name: party["nama_lengkap"],
      url: party["url_situs"],
      facebook: party["url_facebook"],
      twitter: party["url_twitter"]
    )
end

#province(id) ⇒ Object

[View source]

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/pemilu/api.rb', line 174

def province(id)
  uri = URI("http://api.pemiluapi.org/candidate/api/provinsi/#{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 Province with id: #{id}" if respond.is_a?(Net::HTTPBadRequest)

  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  province = data["data"]["results"]["provinsi"][0]
  return Pemilu::Province.new(
      id: province["id"].to_i,
      name: province["nama"],
      full_name: province["nama_lengkap"],
      international_name: province["nama_inggris"],
      available_chairs: province["jumlah_kursi"],
      electoral_district: province["dapil"],
      population: province["jumlah_penduduk"])
end

#provincesObject

[View source]

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pemilu/api.rb', line 151

def provinces
  uri = URI("http://api.pemiluapi.org/candidate/api/provinsi")
  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)
  result = []

  data = JSON.parse(respond.body) if respond.is_a?(Net::HTTPSuccess)
  @total_provinsi = data["data"]["results"]["count"]
  provinces = data["data"]["results"]["provinsi"]
  provinces.each do |province|
    result << Pemilu::Province.new(
      id: province["id"].to_i,
      name: province["nama"],
      full_name: province["nama_lengkap"],
      international_name: province["nama_inggris"],
      available_chairs: province["jumlah_kursi"],
      population: province["jumlah_penduduk"])
  end
  return result
end