Class: HaveAPI::ClientExamples::Curl
- Inherits:
-
Http
show all
- Defined in:
- lib/haveapi/client_examples/curl.rb
Instance Attribute Summary
#action, #action_name, #base_url, #host, #resource, #resource_path, #version
Instance Method Summary
collapse
Methods inherited from Http
#resolve_path
auth, clients, code, example, init, #initialize, label, order, register, #version_url
Instance Method Details
#auth(method, desc) ⇒ Object
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
|
# File 'lib/haveapi/client_examples/curl.rb', line 13
def auth(method, desc)
login = {login: 'user', password: 'password', lifetime: 'fixed'}
case method
when :basic
<<END
# Password is asked on standard input
$ curl --request OPTIONS \\
--user username \\
'#{base_url}'
Password: secret
# Password given on the command line
$ curl --request OPTIONS \\
--user username:secret \\
'#{base_url}'
END
when :token
<<END
# Acquire the token
$ curl --request POST \\
--header 'Content-Type: application/json' \\
--data-binary "#{format_data(token: login)}" \\
'#{File.join(base_url, '_auth', 'token', 'tokens')}'
# Use a previously acquired token
$ curl --request OPTIONS \\
--header '#{desc[:]}: thetoken' \\
'#{base_url}'
END
end
end
|
78
79
80
81
82
83
84
|
# File 'lib/haveapi/client_examples/curl.rb', line 78
def format_data(data)
json = JSON.pretty_generate(data)
json.split("\n").map do |line|
out = ''
PP.pp(line, out).strip[1..-2]
end.join("\n")
end
|
#init ⇒ Object
9
10
11
|
# File 'lib/haveapi/client_examples/curl.rb', line 9
def init
"$ curl --request OPTIONS '#{version_url}'"
end
|
#request(sample) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/haveapi/client_examples/curl.rb', line 47
def request(sample)
url = File.join(
base_url,
resolve_path(
action[:method],
action[:url],
sample[:url_params] || [],
sample[:request]
)
)
data = format_data({
action[:input][:namespace] => sample[:request],
})
<<END
$ curl --request #{action[:method]} \\
--data-binary "#{data}" \\
'#{url}'
END
end
|
#response(sample) ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/haveapi/client_examples/curl.rb', line 69
def response(sample)
JSON.pretty_generate({
status: sample[:status],
message: sample[:message],
response: {action[:output][:namespace] => sample[:response]},
errors: sample[:errors],
})
end
|