class HaveAPI::ClientExamples::Curl

Public Instance Methods

auth(method, desc) click to toggle source
# File lib/haveapi/client_examples/curl.rb, line 14
    def auth(method, desc)
      case method
      when :basic
        <<~END
          # Password is asked on standard input
          $ curl --request OPTIONS \\
                 --user username \\
                 '#{base_url}'
          Password: secret

          # Password given on the command line
          $ curl --request OPTIONS \\
                 --user username:secret \\
                 '#{base_url}'
        END

      when :token
        login = auth_token_credentials(desc).merge(lifetime: 'fixed')

        <<~END
          # Acquire the token
          $ curl --request POST \\
                 --header 'Content-Type: application/json' \\
                 --data-binary "#{format_data(token: login)}" \\
                 '#{File.join(base_url, '_auth', 'token', 'tokens')}'

          # Use a previously acquired token
          $ curl --request OPTIONS \\
                 --header '#{desc[:http_header]}: thetoken' \\
                 '#{base_url}'
        END

      when :oauth2
        '# See RFC 6749 for authorization process and RFC 6750 for access token usage.'
      end
    end
format_data(data) click to toggle source
# File lib/haveapi/client_examples/curl.rb, line 82
def format_data(data)
  json = JSON.pretty_generate(data)
  json.split("\n").map do |line|
    out = ''
    PP.pp(line, out).strip[1..-2]
  end.join("\n")
end
init() click to toggle source
# File lib/haveapi/client_examples/curl.rb, line 10
def init
  "$ curl --request OPTIONS '#{version_url}'"
end
request(sample) click to toggle source
# File lib/haveapi/client_examples/curl.rb, line 51
    def request(sample)
      url = File.join(
        base_url,
        resolve_path(
          action[:method],
          action[:path],
          sample[:path_params] || [],
          sample[:request]
        )
      )

      data = format_data({
          action[:input][:namespace] => sample[:request]
      })

      <<~END
        $ curl --request #{action[:method]} \\
               --data-binary "#{data}" \\
               '#{url}'
      END
    end
response(sample) click to toggle source
# File lib/haveapi/client_examples/curl.rb, line 73
def response(sample)
  JSON.pretty_generate({
      status: sample[:status],
      message: sample[:message],
      response: { action[:output][:namespace] => sample[:response] },
      errors: sample[:errors]
  })
end