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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/haveapi/client_examples/ruby_client.rb', line 17

def auth(method, desc)
  case method
  when :basic
    "\#{init}\n\nclient.authenticate(:basic, username: \"user\", password: \"secret\")\n"

  when :token
    "\#{init}\n\n# Get token using username and password\nclient.authenticate(:token, username: \"user\", password: \"secret\")\n\nputs \"Token = \\\#{client.auth.token}\"\n\n# Next time, the client can authenticate using the token directly\nclient.authenticate(:token, token: saved_token)\n"
  end
end

#example(sample) ⇒ Object



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

def example(sample)
  args = []

  args.concat(sample[:url_params]) if sample[:url_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



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

def init
  "require 'haveapi-client'\n\nclient = HaveAPI::Client.new(\"\#{base_url}\", version: \"\#{version}\")\n"
end

#response(sample) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/haveapi/client_examples/ruby_client.rb', line 61

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