Class: Skytap::Commands::HttpBase
- Inherits:
-
Base
- Object
- Base
- Skytap::Commands::HttpBase
show all
- Defined in:
- lib/skytap/commands/http.rb
Instance Attribute Summary
Attributes inherited from Base
#args, #command_options, #error, #global_options, #invoker, #logger
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#api_token, #ask, #ask_param, #command_line_params, command_name, #composed_params, #expected_options, #file_params, #find_id, #initialize, #interactive_params, #invoke, make_from, #noninteractive_params, #programmatic?, #solicit_user_input?, #username
Methods included from Help
#description, #help!, #help?, included, #parameters, #synopsis, #version?
Class Method Details
.make_for(command_class, spec = {}) ⇒ Object
This is a factory that is called on subclasses.
10
11
12
13
14
15
16
17
|
# File 'lib/skytap/commands/http.rb', line 10
def make_for(command_class, spec={})
command_name = self.command_name
Class.new(self).tap do |klass|
klass.parent = command_class
klass.singleton_class.send(:define_method, :command_name) { command_name }
klass.spec = spec || {}
end
end
|
Instance Method Details
#delete(*args) ⇒ Object
127
|
# File 'lib/skytap/commands/http.rb', line 127
def delete(*args) ; request('DELETE', *args) ; end
|
#encode_body(params) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/skytap/commands/http.rb', line 40
def encode_body(params)
return unless params.present?
case format = global_options[:'http-format']
when 'json'
JSON.dump(params)
when 'xml'
params.to_xml(:root => resource)
else
raise "Unknown http-format: #{format.inspect}"
end
end
|
#expected_args ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/skytap/commands/http.rb', line 93
def expected_args
ActiveSupport::OrderedHash.new.tap do |expected|
if parent_resources = parent.spec['parent_resources']
example_path = join_paths(*parent_resources.collect {|r| [r.pluralize, "#{r.singularize.upcase}-ID"]}.flatten)
expected['parent_path'] = "path of parent #{'resource'.pluralize(parent_resources.length)}, in the form #{example_path}"
end
end
end
|
#get(*args) ⇒ Object
124
|
# File 'lib/skytap/commands/http.rb', line 124
def get(*args) ; request('GET', *args) ; end
|
#join_paths(*parts) ⇒ Object
120
121
122
|
# File 'lib/skytap/commands/http.rb', line 120
def join_paths(*parts)
'/' + parts.collect{|p| p.split('/')}.flatten.reject(&:blank?).join('/')
end
|
#options_for_request ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/skytap/commands/http.rb', line 83
def options_for_request
{:raise => false}.tap do |options|
if ask_interactively
options[:body] = encode_body(composed_params)
else
options[:params] = composed_params
end
end
end
|
#parent_path ⇒ Object
102
103
104
105
106
107
|
# File 'lib/skytap/commands/http.rb', line 102
def parent_path
if expected_args['parent_path']
index = expected_args.keys.index('parent_path')
path = args[index].dup
end
end
|
#path ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/skytap/commands/http.rb', line 109
def path
path = parent_path || ''
if expected_args['id']
index = expected_args.keys.index('id')
id = args[index]
join_paths(path, resource_path(id))
else
join_paths(path, root_path)
end
end
|
#post(*args) ⇒ Object
125
|
# File 'lib/skytap/commands/http.rb', line 125
def post(*args) ; request('POST', *args) ; end
|
#put(*args) ⇒ Object
126
|
# File 'lib/skytap/commands/http.rb', line 126
def put(*args) ; request('PUT', *args) ; end
|
#request(method, path, options = {}) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/skytap/commands/http.rb', line 53
def request(method, path, options={})
response = requester.request(method, path, options_for_request)
success = response.code.start_with?('2')
logger.info "Code: #{response.code} #{response.message}".color(success ? :green : :red).bright
logger.puts response.pretty_body
response.tap do |resp|
resp.singleton_class.instance_eval do
define_method(:payload) do
return unless body.present?
case self['Content-Type']
when /json/i
JSON.load(body)
when /xml/i
parsed = Hash.from_xml(body)
if parsed.is_a?(Hash)
parsed.values.first
else
parsed
end
else
body
end
end
end
end
end
|
#requester ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/skytap/commands/http.rb', line 20
def requester
base_url = global_options[:'base-url'] or raise Skytap::Error.new 'Must provide base-url option'
http_format = global_options[:'http-format'] or raise Skytap::Error.new 'Must provide http-format option'
verify_certs = global_options.has_key?(:'verify-certs') ? global_options[:'verify-certs'] : true
@requester ||= Requester.new(logger, username, api_token, base_url, http_format, verify_certs)
end
|
#resource ⇒ Object
36
37
38
|
# File 'lib/skytap/commands/http.rb', line 36
def resource
parent.command_name
end
|
#resource_path(id) ⇒ Object
32
33
34
|
# File 'lib/skytap/commands/http.rb', line 32
def resource_path(id)
"#{root_path}/#{id}"
end
|
#root_path ⇒ Object
28
29
30
|
# File 'lib/skytap/commands/http.rb', line 28
def root_path
"/#{resource.pluralize}"
end
|