Module: SweetTooth

Defined in:
lib/sweettooth.rb,
lib/sweettooth/json.rb,
lib/sweettooth/util.rb,
lib/sweettooth/version.rb,
lib/sweettooth/activity.rb,
lib/sweettooth/customer.rb,
lib/sweettooth/spending.rb,
lib/sweettooth/api_resource.rb,
lib/sweettooth/spending_option.rb,
lib/sweettooth/errors/api_error.rb,
lib/sweettooth/collection_object.rb,
lib/sweettooth/sweettooth_object.rb,
lib/sweettooth/api_operations/list.rb,
lib/sweettooth/api_operations/create.rb,
lib/sweettooth/api_operations/delete.rb,
lib/sweettooth/api_operations/update.rb,
lib/sweettooth/singleton_api_resource.rb,
lib/sweettooth/errors/sweettooth_error.rb,
lib/sweettooth/errors/api_connection_error.rb,
lib/sweettooth/errors/authentication_error.rb,
lib/sweettooth/errors/invalid_request_error.rb

Defined Under Namespace

Modules: APIOperations, JSON, Util Classes: APIConnectionError, APIError, APIResource, Activity, AuthenticationError, CollectionObject, Customer, InvalidRequestError, SingletonAPIResource, Spending, SpendingOption, SweetToothError, SweetToothObject

Constant Summary collapse

VERSION =
'1.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_baseObject

Returns the value of attribute api_base.



44
45
46
# File 'lib/sweettooth.rb', line 44

def api_base
  @api_base
end

.api_keyObject

Returns the value of attribute api_key.



44
45
46
# File 'lib/sweettooth.rb', line 44

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



44
45
46
# File 'lib/sweettooth.rb', line 44

def api_version
  @api_version
end

.verify_ssl_certsObject

Returns the value of attribute verify_ssl_certs.



44
45
46
# File 'lib/sweettooth.rb', line 44

def verify_ssl_certs
  @verify_ssl_certs
end

Class Method Details

.api_url(url = '') ⇒ Object



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

def self.api_url(url='')
  @api_base + url
end

.request(method, url, api_key, params = {}, headers = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sweettooth.rb', line 51

def self.request(method, url, api_key, params={}, headers={})
  unless api_key ||= @api_key
    raise AuthenticationError.new('No API key provided. ' +
      'Set your API key using "SweetTooth.api_key = <API-KEY>". ' +
      'You can generate API keys from the Sweet Tooth web interface. ' +
      'See https://www.sweettoothrewards.com/api for details, or email [email protected] ' +
      'if you have any questions.')
  end

  if api_key =~ /\s/
    raise AuthenticationError.new('Your API key is invalid, as it contains ' +
      'whitespace. (HINT: You can double-check your API key from the ' +
      'Sweet Tooth web interface. See https://www.sweettoothrewards.com/api for details, or ' +
      'email [email protected] if you have any questions.)')
  end

  request_opts = { :verify_ssl => false }

  params = Util.objects_to_ids(params)
  url = api_url(url)

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    payload = uri_encode(params)
  end

  request_opts.update(:headers => request_headers(api_key).update(headers),
                      :method => method, :open_timeout => 30,
                      :payload => payload, :url => url, :timeout => 80)

  begin
    response = execute_request(request_opts)
  rescue SocketError => e
    handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      handle_api_error(rcode, rbody)
    else
      handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    handle_restclient_error(e)
  end

  [parse(response), api_key]
end