lib/fanforce/main.rb in fanforce-0.3.12 vs lib/fanforce/main.rb in fanforce-0.3.13

- old
+ new

@@ -6,90 +6,90 @@ include Fanforce::Utils ######################################################################## def initialize(arg={}) if arg.is_a?(Hash) add_params(arg) - auth(params) if params.length > 0 + auth(@params) if @params.length > 0 elsif arg.is_a?(String) auth(arg) end end def params @params ||= {} end - def add_params(params) - params.merge!(collect_known_params params) + def add_params(params_to_add) + params.merge!(collect_known_params params_to_add) end - def get(path, params={}) + def get(path, req_params={}) url = complete_url(path) - params = apply_auth(params) - RestClient.get(url, {:params => params, :accept => :json}) do |response, request, result, &block| - handle_response(response, request, url, params) + req_params = apply_auth(req_params) + RestClient.get(url, {:params => req_params, :accept => :json}) do |response, request, result, &block| + handle_response(response, request, url, req_params) end end - def get_url(path, params={}) + def get_url(path, req_params={}) url = complete_url(path) - params = apply_auth(params) - "#{url}?#{to_query_string(params)}" + req_params = apply_auth(req_params) + "#{url}?#{to_query_string(req_params)}" end - def post(path, params={}) + def post(path, req_params={}) url = complete_url(path) - params = apply_auth(params) - RestClient.post(url, params, {:accept => :json}) do |response, request, result, &block| - handle_response(response, request, url, params) + req_params = apply_auth(req_params) + RestClient.post(url, req_params, {:accept => :json}) do |response, request, result, &block| + handle_response(response, request, url, req_params) end end - def put(path, params={}) + def put(path, req_params={}) url = complete_url(path) - params = apply_auth(params) - RestClient.put(url, params, {:accept => :json}) do |response, request, result, &block| - handle_response(response, request, url, params) + req_params = apply_auth(req_params) + RestClient.put(url, req_params, {:accept => :json}) do |response, request, result, &block| + handle_response(response, request, url, req_params) end end - def delete(path, params={}) + def delete(path, req_params={}) url = complete_url(path) - params = apply_auth(params) - RestClient.delete(url, {:params => params, :accept => :json}) do |response, request, result, &block| - handle_response(response, request, url, params) + req_params = apply_auth(req_params) + RestClient.delete(url, {:params => req_params, :accept => :json}) do |response, request, result, &block| + handle_response(response, request, url, req_params) end end - def identify(params) - post '/bie/identify', params + def identify(req_params) + post '/bie/identify', req_params {success: true} end - def track(behavior_id, params) - post '/bie/track', params.merge(behavior_id: behavior_id) + def track(behavior_id, req_params) + post '/bie/track', req_params.merge(behavior_id: behavior_id) {success: true} end - def handle_response(response, request, url, params) + def handle_response(response, request, url, req_params) case response.code when 200, 201 begin response = decode_json(response) rescue - raise UnknownError.new(response, request, url, params) + raise UnknownError.new(response, request, url, req_params) end when 400 - raise BadRequestError.new(response, request, url, params) + raise BadRequestError.new(response, request, url, req_params) when 403 - raise ForbiddenError.new(response, request, url, params) + raise ForbiddenError.new(response, request, url, req_params) when 404 - raise NotFoundError.new(response, request, url, params) + raise NotFoundError.new(response, request, url, req_params) when 422 - raise UnprocessableEntityError.new(response, request, url, params) + raise UnprocessableEntityError.new(response, request, url, req_params) else - raise UnknownError.new(response, request, url, params) + raise UnknownError.new(response, request, url, req_params) end response end def auth(auth_data=nil) @@ -103,11 +103,11 @@ def valid_auth? is_present?(@auth_hash) and is_present?(@auth_hash[:api_key]) and is_present?(@auth_hash[:fanforce_id]) end - def apply_auth(params) - params.merge(@auth_hash || {}) + def apply_auth(req_params) + req_params.merge(@auth_hash || {}) end def complete_url(path) 'http://' + $API_DOMAIN + path end