Sha256: 079519c507cff01ca143990bc501fb49aa92baabf03980657e46dd2bf15eab59

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

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 ||= Rails.application.class.parent_name || ''
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
casino_hub-0.1.6 lib/casino_hub.rb
casino_hub-0.1.5 lib/casino_hub.rb