Class: HaveAPI::ClientExamples::Curl

Inherits:
Http show all
Defined in:
lib/haveapi/client_examples/curl.rb

Instance Attribute Summary

Attributes inherited from HaveAPI::ClientExample

#action, #action_name, #base_url, #host, #resource, #resource_path, #version

Instance Method Summary collapse

Methods inherited from Http

#resolve_path

Methods inherited from HaveAPI::ClientExample

auth, clients, code, example, init, #initialize, label, order, register, #version_url

Constructor Details

This class inherits a constructor from HaveAPI::ClientExample

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
# File 'lib/haveapi/client_examples/curl.rb', line 14

def auth(method, desc)
   = {user: '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: )}" \\
   '#{File.join(base_url, '_auth', 'token', 'tokens')}'

# Use a previously acquired token
$ curl --request OPTIONS \\
   --header '#{desc[:http_header]}: thetoken' \\
   '#{base_url}'
END
  end
end

#format_data(data) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/haveapi/client_examples/curl.rb', line 79

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

#initObject



10
11
12
# File 'lib/haveapi/client_examples/curl.rb', line 10

def init
  "$ curl --request OPTIONS '#{version_url}'"
end

#request(sample) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/haveapi/client_examples/curl.rb', line 48

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



70
71
72
73
74
75
76
77
# File 'lib/haveapi/client_examples/curl.rb', line 70

def response(sample)
  JSON.pretty_generate({
      status: sample[:status],
      message: sample[:message],
      response: {action[:output][:namespace] => sample[:response]},
      errors: sample[:errors],
  })
end