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
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
|
# File 'lib/haveapi/client_examples/curl.rb', line 14
def auth(method, desc)
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
login = auth_token_credentials(desc).merge(lifetime: 'fixed')
<<~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
when :oauth2
'# See RFC 6749 for authorization process and RFC 6750 for access token usage.'
end
end
|
82
83
84
85
86
87
88
|
# File 'lib/haveapi/client_examples/curl.rb', line 82
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
10
11
12
|
# File 'lib/haveapi/client_examples/curl.rb', line 10
def init
"$ curl --request OPTIONS '#{version_url}'"
end
|
#request(sample) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/haveapi/client_examples/curl.rb', line 51
def request(sample)
url = File.join(
base_url,
resolve_path(
action[:method],
action[:path],
sample[:path_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
73
74
75
76
77
78
79
80
|
# File 'lib/haveapi/client_examples/curl.rb', line 73
def response(sample)
JSON.pretty_generate({
status: sample[:status],
message: sample[:message],
response: { action[:output][:namespace] => sample[:response] },
errors: sample[:errors]
})
end
|