lib/fanforce/main.rb in fanforce-0.3.4 vs lib/fanforce/main.rb in fanforce-0.3.6

- old
+ new

@@ -2,16 +2,22 @@ require 'fanforce/utils' require 'rest-client' class Fanforce include Fanforce::Utils - + attr_reader :params ######################################################################## - def initialize(auth_data=nil) - auth(auth_data) if is_present?(auth_data) + def initialize(params={}) + add_params(params) + auth(@params) if @params.length > 0 end + def add_params(params) + @params ||= {} + @params.merge!(collect_known_params params) + end + def get(path, 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) @@ -40,12 +46,12 @@ end end def delete(path, params={}) url = complete_url(path) - params = apply_auth(url) - RestClient.delete(url, {:query => params, :accept => :json}) do |response, request, result, &block| + params = apply_auth(params) + RestClient.delete(url, {:params => params, :accept => :json}) do |response, request, result, &block| handle_response(response, request, url, params) end end def identify(params) @@ -79,20 +85,19 @@ end response end def auth(auth_data=nil) - if is_present?(auth_data) - auth_data = auth_data.is_a?(Hash) ? auth_data.symbolize_keys : {api_key: auth_data.to_s} - @auth_hash ||= {} - @auth_hash[:api_key] = auth_data[:api_key] if is_present?(auth_data[:api_key]) - @auth_hash[:fanforce_id] = auth_data[:fanforce_id] if is_present?(auth_data[:fanforce_id]) - end - @auth_hash + @auth_hash ||= {} + return @auth_hash if is_blank?(auth_data) + auth_data = auth_data.is_a?(Hash) ? auth_data.symbolize_keys : {api_key: auth_data.to_s} + @auth_hash.merge! remove_nil_values(api_key: auth_data[:api_key], fanforce_id: auth_data[:fanforce_id], fanforce_slug: auth_data[:fanforce_slug], app_id: auth_data[:app_id], module_id: auth_data[:module_id], behavior_id: auth_data[:behavior_id], widget_id: auth_data[:widget_id]) + @params.merge!(@auth_hash) + valid_auth? end def valid_auth? - is_present?(@auth_hash) and is_present?(@auth_hash[:api_key]) + 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 || {}) end