Class: Squall::Base
- Inherits:
-
Object
- Object
- Squall::Base
- Defined in:
- lib/squall/support/base.rb
Overview
All OnApp API classes subclass Base to get access to HTTP methods and other convenience methods.
Direct Known Subclasses
DataStoreZone, Disk, FirewallRule, Hypervisor, HypervisorZone, IpAddress, IpAddressJoin, Network, NetworkZone, Payment, Role, Statistic, Template, Transaction, User, UserGroup, VirtualMachine, Whitelist
Instance Attribute Summary (collapse)
-
- (Object) result
readonly
Faraday response body.
-
- (Object) success
readonly
Returns true/false for successful/unsuccessful requests.
Instance Method Summary (collapse)
-
- (void) check_config
Ensures `Squall.config` is set.
-
- (Object) default_params(*options)
Sets the default URL params for requests and merges `options`.
-
- (Object) key_for_class
Sets the default param container for request.
-
- (Object) request(request_method, path, options = {})
Performs an HTTP Request.
Instance Attribute Details
- (Object) result (readonly)
Faraday response body
9 10 11 |
# File 'lib/squall/support/base.rb', line 9 def result @result end |
- (Object) success (readonly)
Returns true/false for successful/unsuccessful requests
6 7 8 |
# File 'lib/squall/support/base.rb', line 6 def success @success end |
Instance Method Details
- (void) check_config
This method returns an undefined value.
Ensures `Squall.config` is set.
66 67 68 |
# File 'lib/squall/support/base.rb', line 66 def check_config raise NoConfig, "Squall.config must be specified" if Squall.config.empty? end |
- (Object) default_params(*options)
Sets the default URL params for requests and merges `options`
*options - One or more options
20 21 22 |
# File 'lib/squall/support/base.rb', line 20 def default_params(*) .empty? ? {} : { query: { key_for_class => .first } } end |
- (Object) key_for_class
Sets the default param container for request. It is derived from the class name. Given the class name `Sandwich` and a param `bread` the resulting params would be `bob=wheat`.
75 76 77 78 79 80 81 82 83 |
# File 'lib/squall/support/base.rb', line 75 def key_for_class word = self.class.name.split("::").last.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word.to_sym end |
- (Object) request(request_method, path, options = {})
Performs an HTTP Request
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/squall/support/base.rb', line 40 def request(request_method, path, = {}) check_config conn = Faraday.new(url: Squall.config[:base_uri]) do |c| c.basic_auth Squall.config[:username], Squall.config[:password] c.params = ([:query] || {}) c.request :url_encoded c.response :json c.adapter :net_http if Squall.config[:debug] c.use Faraday::Response::Logger end end response = conn.send(request_method, path) @success = (200..207).include?(response.env[:status]) @result = response.body end |