lib/squall/support/base.rb in squall-1.3.0 vs lib/squall/support/base.rb in squall-1.3.1

- old
+ new

@@ -1,51 +1,48 @@ module Squall - # All OnApp API classes subclass Base to get access to - # HTTParty methods and other convenience methods + # All OnApp API classes subclass Base to get access to HTTP methods and + # other convenience methods. class Base - # Params instance - attr_reader :params - - # Returns true/false for successful/unsuccessful requests + # Public: Returns true/false for successful/unsuccessful requests attr_reader :success - # HTTPart request result + # Public: Faraday response body attr_reader :result - # Returns a Params.new - def params - @params = Squall::Params.new - end - - # Sets the default URL params for requests and merges +options+ + # Public: Sets the default URL params for requests and merges `options` # - # ==== Options + # *options - One or more options # - # * +options+ + # Example # - # ==== Example + # default_params(something: 1) # - # default_params(:something => 1) + # Returns a Hash. def default_params(*options) - options.empty? ? {} : {:query => { key_for_class => options.first}} + options.empty? ? {} : { query: { key_for_class => options.first } } end - # Peforms an HTTP Request + # Public: Performs an HTTP Request # - # ==== Options - # * +request_method+ - The HTTP verb for the #request. (:get/:post/:delete etc) - # * +path+ - URL path - # * +options+ - HTTP query params + # request_method - The HTTP verb for the request, one of + # :get/:post/:delete/:put, etc + # path - URL path + # options - HTTP query params # - # ==== Example + # Example # - # request :get, '/something.json' # GET /seomthing.json - # request :put, '/something.json', :something => 1 # PUT /something.json?something=1 + # # GET /something.json + # request :get, '/something.json' + # + # # PUT /something.json?something=1 + # request :put, '/something.json', something: 1 + # + # Returns the JSON response. def request(request_method, path, options = {}) check_config - conn = Faraday.new(:url => Squall.config[:base_uri]) do |c| + conn = Faraday.new(url: Squall.config[:base_uri]) do |c| c.basic_auth Squall.config[:username], Squall.config[:password] c.params = (options[:query] || {}) c.request :url_encoded c.response :json c.adapter :net_http @@ -58,17 +55,24 @@ @success = (200..207).include?(response.env[:status]) @result = response.body end - # Raises an error if a request is made without first calling Squall.config + # Public: Ensures `Squall.config` is set. + # + # Raises Squall::NoConfig if a request is made without first calling + # Squall.config. + # + # Returns nothing. def check_config raise NoConfig, "Squall.config must be specified" if Squall.config.empty? end - # 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[bread]=wheat' + # Public: 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[bread]=wheat`. + # + # Returns a String 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')