Class: HasOffers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hasoffers/base.rb

Constant Summary collapse

@@base_uri =
'https://api.hasoffers.com/Api'
@@api_mode =
(defined?(Rails) and Rails.env.production?) or ENV['HAS_OFFERS_LIVE'] == '1') ? :live : :test
@@default_params =
nil

Class Method Summary collapse

Class Method Details

.api_modeObject


43
44
45
# File 'lib/hasoffers/base.rb', line 43

def api_mode
  @@api_mode
end

.api_mode=(mode) ⇒ Object


39
40
41
# File 'lib/hasoffers/base.rb', line 39

def api_mode=(mode)
  @@api_mode = mode
end

.base_uriObject


12
13
14
# File 'lib/hasoffers/base.rb', line 12

def base_uri
  @@base_uri
end

.base_uri=(uri) ⇒ Object


8
9
10
# File 'lib/hasoffers/base.rb', line 8

def base_uri=(uri)
  @@base_uri = uri
end

.get_request(target, method, params) ⇒ Object


47
48
49
# File 'lib/hasoffers/base.rb', line 47

def get_request(target, method, params)
  make_request(:get, target, method, params)
end

.initialize_credentialsObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hasoffers/base.rb', line 16

def initialize_credentials
  config_file = ENV['HAS_OFFERS_CONFIG_FILE'] || "config/has_offers.yml"
  if File.exists?(config_file)
    config = YAML::load(IO.read(config_file))
    @@default_params = {'Format' => 'json',
                        'Service' => 'HasOffers',
                        'Version' => '2',
                        'NetworkId' => config['network_id'],
                        'NetworkToken' => config['api_key']}
  else
    @@default_params = {}
    puts "Missing config/has_offers.yml file!"
  end
end

.live?Boolean


35
36
37
# File 'lib/hasoffers/base.rb', line 35

def live?
  @@api_mode == :live
end

.post_request(target, method, params) ⇒ Object


51
52
53
# File 'lib/hasoffers/base.rb', line 51

def post_request(target, method, params)
  make_request(:post, target, method, params)
end

.requires!(hash, required_params) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/hasoffers/base.rb', line 55

def requires!(hash, required_params)
  missing_params = []
  required_params.each do |param|
    missing_params.push param unless hash.has_key?(param)
  end
  unless missing_params.empty?
    raise ArgumentError.new("Missing required parameter(s): #{missing_params.join(', ')}")
  end
end

.test?Boolean


31
32
33
# File 'lib/hasoffers/base.rb', line 31

def test?
  @@api_mode == :test
end