class HaveAPI::ClientExamples::RubyClient
Public Instance Methods
Source
# 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
Source
# 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
Source
# 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
Source
# 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