Class: HaveAPI::ClientExamples::JsClient
Instance Attribute Summary
#action, #action_name, #base_url, #host, #resource, #resource_path, #version
Instance Method Summary
collapse
auth, clients, code, example, init, #initialize, label, order, register, #version_url
Instance Method Details
#auth(method, desc) ⇒ Object
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
|
# File 'lib/haveapi/client_examples/js_client.rb', line 17
def auth(method, desc)
case method
when :basic
"\#{init}\n\napi.authenticate(\"basic\", {\n username: \"user\",\n password: \"secret\"\n}, function (client, status) {\n console.log(\"Authenticated?\", status);\n});\n"
when :token
"\#{init}\n\n// Request a new token\napi.authenticate(\"token\", {\n username: \"user\",\n password: \"secret\"\n}, function (client, status) {\n console.log(\"Authenticated?\", status);\n\n if (status)\nconsole.log(\"Token is\", client.authProvider.token);\n});\n\n// Use an existing token\napi.authenticate(\"token\", {\n token: \"qwertyuiop...\"\n}, function (client, status) {\n console.log(\"Authenticated?\", status);\n});\n"
end
end
|
#error(sample) ⇒ Object
143
144
145
146
147
148
149
|
# File 'lib/haveapi/client_examples/js_client.rb', line 143
def error(sample)
out = ''
out << " // reply.isOk() returns false\n"
out << " // reply.message() returns the error message\n"
out << " // reply.envelope.errors contains parameter errors\n"
out
end
|
#example(sample) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/haveapi/client_examples/js_client.rb', line 56
def example(sample)
args = []
args.concat(sample[:path_params]) if sample[:path_params]
if sample[:request] && !sample[:request].empty?
args << JSON.pretty_generate(sample[:request])
end
out = "#{init}\n"
out << "api.#{resource_path.join('.')}.#{action_name}"
out << '('
out << "#{args.join(', ')}, " unless args.empty?
callback = "function (client, reply) {\n"
callback << " console.log('Response', reply);\n"
if sample[:status]
callback << response(sample)
else
callback << error(sample)
end
callback << "}"
out << callback.strip
out << ');'
out
end
|
#init ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/haveapi/client_examples/js_client.rb', line 9
def init
"import HaveAPI from 'haveapi-client'\n\nvar api = new HaveAPI.Client(\"\#{base_url}\", {version: \"\#{version}\"});\n"
end
|
#response(sample) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/haveapi/client_examples/js_client.rb', line 87
def response(sample)
out = ''
case action[:output][:layout]
when :hash
out << "# reply is an instance of HaveAPI.Client.Response\n"
out << "# reply.response() returns an object with output parameters:\n"
out << JSON.pretty_generate(sample[:response] || {}).split("\n").map do |v|
" // #{v}"
end.join("\n")
when :hash_list
out << "# reply is an instance of HaveAPI.Client.Response\n"
out << "# reply.response() returns an array of objects:\n"
out << JSON.pretty_generate(sample[:response] || []).split("\n").map do |v|
" // #{v}"
end.join("\n")
when :object
out << " // reply is an instance of HaveAPI.Client.ResourceInstance\n"
(sample[:response] || {}).each do |k, v|
param = action[:output][:parameters][k]
if param[:type] == 'Resource'
out << " // reply.#{k} = HaveAPI.Client.ResourceInstance("
out << "resource: #{param[:resource].join('.')}, "
if v.is_a?(::Hash)
out << v.map { |k,v| "#{k}: #{PP.pp(v, '').strip}" }.join(', ')
else
out << "id: #{v}"
end
out << ")\n"
elsif param[:type] == 'Custom' && (v.is_a?(::Hash) || v.is_a?(::Array))
json = JSON.pretty_generate(v).split("\n").map do |v|
" // #{v}"
end.join("\n")
out << " // reply.#{k} = #{json}"
else
out << " // reply.#{k} = #{PP.pp(v, '')}"
end
end
when :object_list
out << " // reply is an instance of HaveAPI.Client.ResourceInstanceList\n"
end
out
end
|