Sha256: 3e0fd00db640365215e3e407e8d2d6912ef004c575820fa77ad1401f5008a04c

Contents?: true

Size: 1.48 KB

Versions: 25

Compression:

Stored size: 1.48 KB

Contents

module Onering
  module CLI
    module Call
      def self.configure(global={})
        @api = Onering::CLI.connect(global)

        @opts = ::Trollop::options do
          banner <<-EOS
Call an arbitrary Onering API endpoint and return the output

Usage:
    onering call [options] [endpoint]

Examples:
    # Returns the API status page at path /api/
    $ onering call /

    # Returns details about the authenticated user
    $ onering call users/current

    # Delete the device called '0bf29c'
    $ onering call devices/0bf29c -m delete

Options:
EOS
          opt :method, "The HTTP method to use when performing the request", :default => 'get', :short => "-m", :type => :string
          opt :query,  "A query string attribute to add to the request in the form of NAME=VALUE", :short => '-a', :type => :string, :multi => true
          opt :header, "A 'Name: Value' header to add to the request", :short => '-H', :type => :string, :multi => true
        end
      end

      def self.run(args)
        data = STDIN.read() unless STDIN.tty?

        headers = {
          'Content-Type' => 'application/json'
        }.merge(Hash[@opts[:header].collect{|i|
            i.split(/[\:=]\s*/,2)
        }])

        rv = @api.request(@opts[:method], args.first, {
          :body    => data,
          :headers => headers,
          :query   => Hash[@opts[:query].collect{|i|
            i.split('=',2)
          }]
        })

        return (rv.parsed_response rescue rv.response.body)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
onering-client-0.2.1 lib/onering/cli/call.rb
onering-client-0.2.0 lib/onering/cli/call.rb
onering-client-0.1.7 lib/onering/cli/call.rb
onering-client-0.1.6 lib/onering/cli/call.rb
onering-client-0.1.5 lib/onering/cli/call.rb
onering-client-0.1.4 lib/onering/cli/call.rb
onering-client-0.1.3 lib/onering/cli/call.rb
onering-client-0.1.2 lib/onering/cli/call.rb
onering-client-0.1.1 lib/onering/cli/call.rb
onering-client-0.0.99 lib/onering/cli/call.rb
onering-client-0.0.98 lib/onering/cli/call.rb
onering-client-0.0.97 lib/onering/cli/call.rb
onering-client-0.0.96 lib/onering/cli/call.rb
onering-client-0.0.95 lib/onering/cli/call.rb
onering-client-0.0.94 lib/onering/cli/call.rb
onering-client-0.0.93 lib/onering/cli/call.rb
onering-client-0.0.92 lib/onering/cli/call.rb
onering-client-0.0.91 lib/onering/cli/call.rb
onering-client-0.0.90 lib/onering/cli/call.rb
onering-client-0.0.89 lib/onering/cli/call.rb