Class: HaveAPI::ClientExamples::RubyClient

Inherits:
HaveAPI::ClientExample show all
Defined in:
lib/haveapi/client_examples/ruby_client.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
# File 'lib/haveapi/client_examples/ruby_client.rb', line 18

def auth(method, desc)
  case method
  when :basic
    <<~END
      #{init}

      client.authenticate(:basic, user: "user", password: "secret")
    END

  when :token
    <<~END
      #{init}

      # Get token using username and password
      client.authenticate(:token, #{auth_token_credentials(desc).map { |k, v| "#{k}: \"#{v}\"" }.join(', ')})

      puts "Token = \#{client.auth.token}"

      # Next time, the client can authenticate using the token directly
      client.authenticate(:token, token: saved_token)
    END

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

#example(sample) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/haveapi/client_examples/ruby_client.rb', line 45

def example(sample)
  args = []

  args.concat(sample[:path_params]) if sample[:path_params]

  if sample[:request] && !sample[:request].empty?
    args << PP.pp(sample[:request], '').strip
  end

  out = "#{init}\n"
  out << "reply = client.#{resource_path.join('.')}.#{action_name}"
  out << "(#{args.join(', ')})" unless args.empty?

  return (out << response(sample)) if sample[:status]

  out << "\n"
  out << '# Raises exception HaveAPI::Client::ActionFailed'
  out
end

#initObject



10
11
12
13
14
15
16
# File 'lib/haveapi/client_examples/ruby_client.rb', line 10

def init
  <<~END
    require 'haveapi-client'

    client = HaveAPI::Client.new("#{base_url}", version: "#{version}")
  END
end

#response(sample) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
# File 'lib/haveapi/client_examples/ruby_client.rb', line 65

def response(sample)
  out = "\n\n"

  case action[:output][:layout]
  when :hash
    out << "# reply is an instance of HaveAPI::Client::Response\n"
    out << "# reply.response() returns a hash of output parameters:\n"
    out << PP.pp(sample[:response] || {}, '').split("\n").map { |v| "# #{v}" }.join("\n")

  when :hash_list
    out << "# reply is an instance of HaveAPI::Client::Response\n"
    out << "# reply.response() returns an array of hashes:\n"
    out << PP.pp(sample[:response] || [], '').split("\n").map { |v| "# #{v}" }.join("\n")

  when :object
    out << "# reply is an instance of HaveAPI::Client::ResourceInstance\n"

    (sample[:response] || {}).each do |pn, pv|
      param = action[:output][:parameters][pn]

      if param[:type] == 'Resource'
        out << "# reply.#{pn} = HaveAPI::Client::ResourceInstance("
        out << "resource: #{param[:resource].join('.')}, "

        out << if pv.is_a?(::Hash)
                 pv.map { |k, v| "#{k}: #{PP.pp(v, '').strip}" }.join(', ')
               else
                 "id: #{pv}"
               end

        out << ")\n"

      else
        out << "# reply.#{pn} = #{PP.pp(pv, '')}"
      end
    end

  when :object_list
    out << "# reply is an instance of HaveAPI::Client::ResourceInstanceList,\n"
    out << '# which is a subclass of Array'
  end

  out
end