require "rest-client" require 'json' require 'active_support/inflector' require "casino_hub/version" require "casino_hub/util" require "casino_hub/operations/get" require "casino_hub/operations/create" require "casino_hub/api_resource" require "casino_hub/payment" module CasinoHub @api_version = nil @api_base = 'https://casino.openfan.net' @api_key = nil @source = nil class << self attr_accessor :api_version, :api_base, :company_token, :source def api_version=(version=1.0) @api_version = version end # change base url based on env def environment=(env) @environment = env if env != 'production' @api_base = 'https://casino.openfan.net/api' end end def source @source ||= File.basename(Dir.getwd) end end def self.api_url(url='', api_base_url=nil) (api_base_url || @api_base) + url end def self.request(method, url, params, opts, api_base_url=nil) api_base_url = api_base_url || @api_base request_opts = {} url = api_url(url, api_base_url) headers = { 'Access-Token' => params.fetch(:company_token, company_token) } params.delete(:company_token) params.merge!(source: @source.parameterize.underscore) case method.to_s.downcase.to_sym when :get, :head, :delete # Make params into GET parameters url += "#{URI.parse(url).query ? '&' : '?'}#{Util.encode_parameters(params)}" if params && params.any? payload = nil else payload = params end request_opts.update(method: method, headers: headers, payload: payload, url: url) response = execute_request(request_opts) JSON.parse(response.body) end def self.execute_request(opts) RestClient::Request.execute(opts) end end