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

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

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

  when :token
    <<END
#{init}

# Get token using username and password
client.authenticate(:token, username: "user", password: "secret")

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

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

#example(sample) ⇒ Object



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

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



62
63
64
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
# File 'lib/haveapi/client_examples/ruby_client.rb', line 62

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 |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"

      else
        out << "# reply.#{k} = #{PP.pp(v, '')}"
      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