require_relative 'errors' require_relative 'utils' require 'rest-client' class Fanforce include Fanforce::Utils ######################################################################## def initialize(arg={}) if arg.is_a?(Hash) add_params(arg) auth(@params) if @params.length > 0 elsif arg.is_a?(String) auth(arg) end end def params @params ||= {} end def add_params(params_to_add) params.merge!(collect_known_params params_to_add) end def get(path, req_params={}) url = complete_url(path) 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, req_params={}) url = complete_url(path) req_params = apply_auth(req_params) "#{url}?#{to_query_string(req_params)}" end def get_curl_command(method, path, req_params={}) case method when :get "curl \"#{path}?#{to_param(req_params)}\"" when :post "curl -X POST -d \"#{to_param(req_params)}\" #{path}" when :put "curl -X PUT -d \"#{to_param(req_params)}\" #{path.to_json}" when :delete "curl --request DELETE \"#{path}?#{to_param(req_params)}\"" else raise 'Unknown request method' end end def post(path, req_params={}) url = complete_url(path) req_params = apply_auth(req_params) RestClient.post(url, req_params.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block| handle_response(response, request, url, req_params) end end def put(path, req_params={}) url = complete_url(path) req_params = apply_auth(req_params) RestClient.put(url, req_params.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block| handle_response(response, request, url, req_params) end end def delete(path, req_params={}) url = complete_url(path) 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(req_params) post '/bie/identify', req_params {success: true} end def track(behavior_plugin_id, req_params) post '/bie/track', req_params.merge(behavior_plugin_id: behavior_plugin_id) {success: true} end 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, req_params) end when 400 raise BadRequestError.new(response, request, url, req_params) when 403 raise ForbiddenError.new(response, request, url, req_params) when 404 raise NotFoundError.new(response, request, url, req_params) when 422 raise UnprocessableEntityError.new(response, request, url, req_params) else raise UnknownError.new(response, request, url, req_params) end response end def auth(auth_data=nil) @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], plugin_id: auth_data[:plugin_id], behavior_plugin_id: auth_data[:behavior_plugin_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]) and is_present?(@auth_hash[:fanforce_id]) end def apply_auth(req_params) req_params.merge(@auth_hash || {}) end def complete_url(path) 'http://' + $API_DOMAIN + path end end