-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcakeapi.rb
More file actions
54 lines (39 loc) · 1005 Bytes
/
cakeapi.rb
File metadata and controls
54 lines (39 loc) · 1005 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
43
44
45
46
47
48
49
50
51
52
53
54
require 'net/https'
require 'rubygems'
require 'json'
require 'CGI'
class CakeAPI
def initialize(apikey)
@key = apikey
end
def to_query(params, *parent)
query = ''
stack = ''
parent = ''
params.each do |k, v|
if v.class == Hash
parent = k
stack = v
else
query << "#{CGI.escape(k)}=#{CGI.escape(v)}&"
end
end
stack.each do |k,v|
query << "#{CGI.escape(parent)}[#{CGI.escape(k)}]=#{CGI.escape(v)}&"
end
return query.chop!
end
def call(url, params)
uri = URI.parse('https://api.wbsrvc.com')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
path = url[0,1] != '/' ? '/' + url : url
headers = {'apikey' => @key}
resp, data = http.post(path, to_query(params), headers)
if data.include? 'error'
return data
end
return JSON.parse(data)
end
end