lib/squall/support/base.rb in squall-1.1.0 vs lib/squall/support/base.rb in squall-1.2.0beta1
- old
+ new
@@ -1,5 +1,7 @@
+Faraday.register_middleware :response, :on_app_errors => Squall::OnAppErrors
+
module Squall
# All OnApp API classes subclass Base to get access to
# HTTParty methods and other convenience methods
class Base
# Params instance
@@ -9,25 +11,10 @@
attr_reader :success
# HTTPart request result
attr_reader :result
- # HTTParty class methods
- include HTTParty
-
- def initialize
- self.class.base_uri Squall::config[:base_uri]
- self.class.basic_auth Squall::config[:username], Squall::config[:password]
- self.class.format :json
- self.class.headers 'Content-Type' => 'application/json'
- if Squall::config[:debug]
- self.class.debug_output
- else
- self.class.debug_output(nil)
- end
- end
-
# Returns a Params.new
def params
@params = Squall::Params.new
end
@@ -42,21 +29,10 @@
# default_params(:something => 1)
def default_params(*options)
options.empty? ? {} : {:query => { key_for_class => options.first}}
end
- # Returns an array of Request errors
- def errors
- return [] if @success
- err = {}
- @result.each do |k,v|
- err[k] ||= []
- err[k].push v.respond_to?(:first) ? v.first : v
- end
- err
- end
-
# Peforms an HTTP Request
#
# ==== Options
# * +request_method+ - The HTTP verb for the #request. (:get/:post/:delete etc)
# * +path+ - URL path
@@ -66,21 +42,20 @@
#
# request :get, '/something.json' # GET /seomthing.json
# request :put, '/something.json', :something => 1 # PUT /something.json?something=1
def request(request_method, path, options = {})
check_config
- @result = self.class.send(request_method, path, options)
- @success = (200..207).include?(@result.code)
- case @result.code
- when (200..207)
- @result
- when 404
- raise NotFound, @result
- when 422
- raise RequestError, @result
- else
- raise ServerError, @result
+ 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 :on_app_errors
+ c.response :json
+ c.adapter :net_http
end
+ response = conn.send(request_method, path)
+ @success = (200..207).include?(response.env[:status])
+ @result = conn.send(request_method, path).body
end
# Raises an error if a request is made without first calling Squall.config
def check_config
raise NoConfig, "Squall.config must be specified" if Squall.config.empty?