-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby.rb
More file actions
42 lines (33 loc) · 903 Bytes
/
ruby.rb
File metadata and controls
42 lines (33 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
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
# Postali API — Ruby examples using only the stdlib (net/http).
# Run with: ruby examples/ruby.rb
require "net/http"
require "json"
require "uri"
BASE = "https://postali.app/api/v1"
def get(path)
JSON.parse(Net::HTTP.get(URI("#{BASE}#{path}")))
end
def post(path, body)
uri = URI("#{BASE}#{path}")
res = Net::HTTP.post(uri, body.to_json, "Content-Type" => "application/json")
JSON.parse(res.body)
end
def lookup(country, cp)
get("/#{country}/cp/#{cp}")
end
def validate(country, cp)
get("/#{country}/validate/#{cp}")
end
def search(country, q, limit: 10)
qs = URI.encode_www_form(q: q, limit: limit)
get("/#{country}/search?#{qs}")
end
def bulk(country, cps)
post("/#{country}/bulk", cps: cps)
end
if __FILE__ == $PROGRAM_NAME
pp lookup("mx", "06700")
pp validate("co", "050001")
pp search("es", "barcelona", limit: 5)
pp bulk("mx", ["06700", "44100", "00000"])
end