Class: Onering::CLI::Call

Inherits:
Plugin
  • Object
show all
Defined in:
lib/onering/cli/call.rb

Class Method Summary collapse

Methods inherited from Plugin

default_format, inherited, registered_plugins

Class Method Details

.configure(global = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/onering/cli/call.rb', line 4

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

.run(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/onering/cli/call.rb', line 32

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