Class: WADL::CLI

Inherits:
Cyclops
  • Object
show all
Defined in:
lib/wadl/cli.rb

Constant Summary

OPTION_RE =
%r{\A--?(.+?)(?:=(.+))?\z}
RESOURCE_PATH_RE =
%r{[. /]}
QUERY_SEPARATOR_RE =
%r{[&;]}n
ARRAY_SUFFIX_RE =
%r{\[\]\z}
HASH_SUFFIX_RE =
%r{\[(.+)\]\z}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query



64
65
66
# File 'lib/wadl/cli.rb', line 64

def query
  @query
end

#resource_pathObject (readonly)

Returns the value of attribute resource_path



64
65
66
# File 'lib/wadl/cli.rb', line 64

def resource_path
  @resource_path
end

Class Method Details

.defaultsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/wadl/cli.rb', line 51

def defaults
  super.merge(
    config:            'config.yaml',
    method:            HTTPRequest::DEFAULT_METHOD.upcase,
    user:              ENV['USER'] || '',
    request_token_url: '%s/oauth/request_token',
    access_token_url:  '%s/oauth/access_token',
    authorize_url:     '%s/oauth/authorize'
  )
end

.usageObject



47
48
49
# File 'lib/wadl/cli.rb', line 47

def usage(*)
  "#{super} <resource-path> [-- arguments]"
end

Instance Method Details

#apiObject



91
92
93
# File 'lib/wadl/cli.rb', line 91

def api
  @api ||= Application.from_wadl(open(options[:wadl]))
end

#auth_resourceObject



105
106
107
108
109
110
# File 'lib/wadl/cli.rb', line 105

def auth_resource
  @auth_resource ||= options[:skip_auth] ? resource            :
                     options[:oauth]     ? oauth_resource      :
                     options[:basic]     ? basic_auth_resource :
                                           resource
end

#reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR) ⇒ Object



112
113
114
115
# File 'lib/wadl/cli.rb', line 112

def reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR)
  super
  @api = @resource = @auth_resource = nil
end

#resourceObject



95
96
97
98
99
100
101
102
103
# File 'lib/wadl/cli.rb', line 95

def resource
  @resource ||= begin
    path = [options[:api_base], *resource_path].compact.join('/')
    path = path.sub(/\A\//, '').split(RESOURCE_PATH_RE)

    path.inject(api) { |m, n| m.send(:find_resource_by_path, n) or
      quit "Resource not found: #{path.join('/')}: #{n}" }
  end
end

#run(arguments) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/wadl/cli.rb', line 66

def run(arguments)
  if options.delete(:dump_config)
    stdout.puts(YAML.dump(options))
    exit
  end

  parse_arguments(arguments)
  quit if resource_path.empty?

  quit "WADL location is required! (Specify with '--wadl' or see '--help')" unless options[:wadl]
  options[:wadl] %= options[:base_url] if options[:base_url]

  if debug = options[:debug]
    debug = 1 unless debug.is_a?(Integer)

    stderr.puts api.paths if debug >= 1
    stderr.puts api       if debug >= 2
  end

  response = auth_resource.send(options[:method].downcase, query: query)

  stderr.puts response.code.join(' ')
  stdout.puts response.representation unless response.code.first =~ /\A[45]/
end