require 'rest-client' require_relative 'utils' require_relative 'response' class Fanforce::API include Fanforce::API::Utils include Fanforce::Errors 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 url(path, req_params={}) req_params = apply_auth(req_params) "#{complete_url(path)}?#{to_query_string(req_params)}" end def curl_command(method, path, req_params={}) req_params = apply_auth(req_params) Fanforce::Utils.curl_command(method, complete_url(path), req_params) end def get(path, req_params={}) req_params = apply_auth(req_params) RestClient.get(url(path, req_params), :accept => :json) do |response, request, result, &block| Fanforce::API::Response.process(response, request, complete_url(path), req_params) end end def post(path, req_params={}, retries=0) url = complete_url(path) req_params = apply_auth(req_params) unless retries > 0 RestClient.post(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block| if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2 puts "retried #{path} #{retries+1} times" post(path, req_params, retries+1) else Fanforce::API::Response.process(response, request, url, req_params) end end end def put(path, req_params={}, retries=0) url = complete_url(path) req_params = apply_auth(req_params) unless retries > 0 RestClient.put(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block| if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2 puts "retried #{path} #{retries+1} times" post(path, req_params, retries+1) else Fanforce::API::Response.process(response, request, url, req_params) end end end def delete(path, req_params={}) url = complete_url(path) req_params = apply_auth(req_params) RestClient.delete(url(path, req_params), :accept => :json) do |response, request, result, &block| Fanforce::API::Response.process(response, request, url, req_params) end end def identify(req_params) post '/bie/identify', req_params end def track(plugin_id, req_params) post '/bie/track', req_params.merge(plugin_id: plugin_id) end def recommend(addon_id, unique_key, req_params={}) req_params.update(unique_key: "#{addon_id}:#{unique_key}") post '/bie/recommend', req_params end def auth(auth_data=nil) @auth_hash ||= {} return @auth_hash if is_blank?(auth_data) auth_data = auth_data.is_a?(Hash) ? symbolize_keys(auth_data) : {api_key: auth_data.to_s} @auth_hash.merge! remove_nil_values(api_key: auth_data[:api_key], organization_id: auth_data[:organization_id], organization_slug: auth_data[:organization_slug]) 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[:organization_id]) end def apply_auth(req_params) req_params.merge(@auth_hash || {}) end def complete_url(path) 'http://' + Fanforce.api_domain + path end end