Class: HaveAPI::ClientExamples::RubyCli

Inherits:
HaveAPI::ClientExample show all
Defined in:
lib/haveapi/client_examples/ruby_cli.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 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



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

def auth(method, desc)
  case method
  when :basic
    <<~END
      # Provide credentials on command line
      #{init} --auth basic --user user --password secret

      # If username or password isn't provided, the user is asked on stdin
      #{init} --auth basic --user user
      Password: secret
    END

  when :token
    <<~END
      # Get token using username and password and save it to disk
      # Note that the client always has to call some action. APIs should provide
      # an action to get information about the current user, so that's what we're
      # calling now.
      #{init} --auth token #{auth_token_credentials(desc, password: false).map { |k, v| "--#{k} #{v}" }.join(' ')} --save user current
      Password: secret

      # Now the token is read from disk and the user does not have to provide username
      # nor password and be authenticated
      #{init} user current
    END

  when :oauth2
    '# OAuth2 is not supported by HaveAPI Ruby CLI.'
  end
end

#example(sample) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/haveapi/client_examples/ruby_cli.rb', line 49

def example(sample)
  cmd = [init]
  cmd << resource_path.join('.')
  cmd << action_name
  cmd.concat(sample[:path_params]) if sample[:path_params]

  if sample[:request] && !sample[:request].empty?
    cmd << "-- \\\n"

    res = cmd.join(' ') + sample[:request].map do |k, v|
      (' ' * 14) + input_param(k, v)
    end.join(" \\\n")

  else
    res = cmd.join(' ')
  end

  return response(sample, res) if sample[:status]

  res << "\nAction failed: #{sample[:message]}\n"

  if sample[:errors] && sample[:errors].any?
    res << "Errors:\n"
    sample[:errors].each do |param, e|
      res << "\t#{param}: #{e.join('; ')}\n"
    end
  end

  res
end

#initObject



14
15
16
# File 'lib/haveapi/client_examples/ruby_cli.rb', line 14

def init
  "$ haveapi-cli -u #{base_url} --version #{version}"
end

#input_param(name, value) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/haveapi/client_examples/ruby_cli.rb', line 111

def input_param(name, value)
  option = name.to_s.gsub('_', '-')

  if action[:input][:parameters][name][:type] == 'Boolean'
    return value ? "--#{option}" : "--no-#{name}"
  end

  "--#{option} '#{value}'"
end

#response(sample, res) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/haveapi/client_examples/ruby_cli.rb', line 80

def response(sample, res)
  return res if sample[:response].nil? || sample[:response].empty?

  cols = []

  action[:output][:parameters].each do |name, param|
    col = {
        name:,
        align: %w[Integer Float].include?(param[:type]) ? 'right' : 'left',
        label: param[:label] && !param[:label].empty? ? param[:label] : name.upcase
    }

    if param[:type] == 'Resource'
      col[:display] = proc do |r|
        next '' unless r
        next r unless r.is_a?(::Hash)

        "#{r[param[:value_label].to_sym]} (##{r[param[:value_id].to_sym]})"
      end
    end

    cols << col
  end

  res << "\n" << HaveAPI::CLI::OutputFormatter.to_s(
    sample[:response],
    cols
  )
  res
end