@@ -9,19 +9,24 @@ def initialize(config = {})
9
9
@config = config
10
10
end
11
11
12
- # Prepares and sends the API request to the URL specificed in Solusvm.config
12
+ # Public: Prepares and sends the API request to the URL specified in
13
+ # `Solusvm.config`.
13
14
#
14
- # class MyClass < Base
15
- # def create_server(name)
16
- # perform_request(:action => "name", :id => 1)
17
- # end
18
- # end
15
+ # options - A Hash of options. Any options not listed below are
16
+ # converted to HTTP query arguments and are passed along to
17
+ # the API.
18
+ # :action - Specifies which API method to execute
19
+ # force_array - see parse_response
19
20
#
20
- # Options:
21
- # * <tt>:action</tt> - Specifies which API method to execute
22
- # All other options passed in are converted to http query arguments and are passed along to the API
21
+ # Example
23
22
#
24
- # <tt>force_array</tt> - see parse_response
23
+ # class MyClass < Base
24
+ # def create_server(name)
25
+ # perform_request(:action => "name", :id => 1)
26
+ # end
27
+ # end
28
+ #
29
+ # Returns true if the request was successful.
25
30
def perform_request ( options = { } , force_array = false )
26
31
options . reject! { |_ , v | v . nil? }
27
32
@@ -32,23 +37,34 @@ def perform_request(options = {}, force_array = false)
32
37
successful?
33
38
end
34
39
35
- # Creates a Faraday connection and returns it.
40
+ # Public: Creates a Faraday connection.
41
+ #
42
+ # Returns a Faraday::Connection.
36
43
def conn
37
44
@conn ||= Faraday . new ( ssl : ssl_option ) do |c |
38
45
c . request :retry if @config . fetch ( :retry_request , false )
39
46
c . adapter :net_http
40
47
end
41
48
end
42
49
50
+ # Public: SSL options used when creating a Faraday connection.
51
+ #
52
+ # Returns a Hash.
43
53
def ssl_option
44
- ca_path = File . join ( File . dirname ( __FILE__ ) , ".." , "cacert.pem" )
45
- { verify : true , ca_file : File . expand_path ( ca_path ) }
54
+ {
55
+ verify : true ,
56
+ ca_file : File . expand_path ( "../../cacert.pem" , __FILE__ )
57
+ }
46
58
end
47
59
48
- # Converts the XML response to a Hash
60
+ # Public: Converts the XML response to a Hash.
49
61
#
50
- # <tt>force_array</tt> - Parses the xml element as an array; can be a string with the element name
51
- # or an array with element names
62
+ # status - Faraday::Response#status
63
+ # body - Faraday::Response#body
64
+ # force_array - Parses the xml element as an array; can be a string with
65
+ # the element name or an array with element names
66
+ #
67
+ # Returns a Hash.
52
68
def parse_response ( status , body , force_array = false )
53
69
parse_error ( status , body ) || begin
54
70
force_array = Array ( force_array ) if force_array
@@ -57,14 +73,23 @@ def parse_response(status, body, force_array = false)
57
73
end
58
74
end
59
75
60
- # Parses a returned_parameters value as a list, if present.
76
+ # Public: Parses a returned_parameters value as a list, if present.
77
+ #
78
+ # attribute - The attribute to check
79
+ #
80
+ # Returns an Array or nil.
61
81
def parse_returned_params_as_list ( attribute )
62
82
if returned_parameters [ attribute ] && !returned_parameters [ attribute ] . empty?
63
83
returned_parameters [ attribute ] . to_s . split ( "," )
64
84
end
65
85
end
66
86
67
- # Parses error responses.
87
+ # Public: Parses error responses.
88
+ #
89
+ # status - HTTP status code
90
+ # body - Raw body
91
+ #
92
+ # Returns a Hash or nil.
68
93
def parse_error ( status , body )
69
94
if ( 200 ..299 ) . include? ( status )
70
95
# Checks for application errors
@@ -81,49 +106,65 @@ def parse_error(status, body)
81
106
end
82
107
end
83
108
84
- # Returns true when a request has been successful
109
+ # Public: Check if the request was successful.
110
+ #
111
+ # my_class = MyClass.new
112
+ # my_class.create_server("example.com")
113
+ # my_class.successful? # => true
85
114
#
86
- # my_class = MyClass.new
87
- # my_class.create_server("example.com")
88
- # my_class.successful? # => true
115
+ # Returns true if the request was successful.
89
116
def successful?
90
117
returned_parameters [ "status" ] . nil? || returned_parameters [ "status" ] == "success"
91
118
end
92
119
93
- # Returns the API endpoint set in the instance configuration. Otherwise,
94
- # it returns the default configuration.
120
+ # Public: Returns the API endpoint set in the instance configuration.
121
+ # Otherwise, it returns the default configuration.
95
122
#
96
123
# Returns a String
97
124
def api_endpoint
98
125
@config . fetch ( :url )
99
126
end
100
127
101
- # Returns the API id set in the instance configuration. Otherwise,
128
+ # Public: Returns the API id set in the instance configuration. Otherwise,
102
129
# it returns the default configuration.
103
130
#
104
131
# Returns a String
105
132
def api_id
106
133
@config . fetch ( :api_id )
107
134
end
108
135
109
- # Returns the API key set in the instance configuration. Otherwise,
110
- # it returns the default configuration.
136
+ # Public: Returns the API key set in the instance configuration.
137
+ # Otherwise, it returns the default configuration.
111
138
#
112
- # Returns a String
139
+ # Returns a String.
113
140
def api_key
114
141
@config . fetch ( :api_key )
115
142
end
116
143
144
+ # Public: API options
145
+ #
146
+ # option - Key to fetch
147
+ #
148
+ # Returns the given option.
117
149
def api_options ( option )
118
150
if options = @config [ :options ]
119
151
options [ option . to_sym ]
120
152
end
121
153
end
122
154
155
+ # Public: API login information.
156
+ #
157
+ # Returns a Hash.
123
158
def api_login
124
- { id : api_id , key : api_key }
159
+ { id : api_id , key : api_key }
125
160
end
126
161
162
+ # Public: Logs API actions to the configured logger.
163
+ #
164
+ # options - A Hash of options
165
+ # :action - the API action
166
+ #
167
+ # Returns nothing.
127
168
def log_messages ( options )
128
169
logger , logger_method = api_options ( :logger ) , api_options ( :logger_method )
129
170
@@ -136,12 +177,20 @@ def log_messages(options)
136
177
end
137
178
end
138
179
139
- # API response message
180
+ # Public: API response message
181
+ #
182
+ # Returns a String.
140
183
def statusmsg
141
184
returned_parameters [ "statusmsg" ]
142
185
end
143
186
144
- # Validates the server type.
187
+ # Public: Validates the server type.
188
+ #
189
+ # type - The server type to check
190
+ #
191
+ # Yields a required block if given server type is valid.
192
+ #
193
+ # Returns the result of the block, or false if the server type is invalid.
145
194
def validate_server_type ( type , &block )
146
195
type = type . strip
147
196
0 commit comments