Class: WhiplashApi::Base

Inherits:
ActiveResource::Base
  • Object
show all
Defined in:
lib/whiplash_api/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/whiplash_api/base.rb', line 10

def api_key
  @api_key
end

.api_versionObject

Returns the value of attribute api_version.



10
11
12
# File 'lib/whiplash_api/base.rb', line 10

def api_version
  @api_version
end

.customer_idObject

Returns the value of attribute customer_id.



10
11
12
# File 'lib/whiplash_api/base.rb', line 10

def customer_id
  @customer_id
end

.shop_idObject

Returns the value of attribute shop_id.



10
11
12
# File 'lib/whiplash_api/base.rb', line 10

def shop_id
  @shop_id
end

Class Method Details

.connection(refresh = false) ⇒ Object

Override the connection that ActiveResource uses, so that we can add our own error messages for the weird cases when API returns 422 error.



25
26
27
28
# File 'lib/whiplash_api/base.rb', line 25

def connection(refresh = false)
  @connection = WhiplashApi::Connection.new(site, format) if refresh || @connection.nil?
  super
end

.headersObject



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

def headers
  Thread.current["active.resource.currentthread.headers"] = {} if Thread.current["active.resource.currentthread.headers"].blank?
  Thread.current["active.resource.currentthread.headers"]
end

.reset_headers!Object



78
79
80
81
82
# File 'lib/whiplash_api/base.rb', line 78

def reset_headers!
  %w[X-API-KEY X-API-VERSION Authorization].each do |key|
    headers.delete(key)
  end
end

.testing!Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/whiplash_api/base.rb', line 12

def testing!
  self.site = 'https://testing.whiplashmerch.com/api/'
  # self.site = 'http://localhost:3000/api/'

  ActiveSupport::Notifications.subscribe("request.active_resource") do |*args|
    puts "[ActiveResource] Headers: #{WhiplashApi::Base.headers}"
    puts "[ActiveResource] Request:  #{args.last[:request_uri]}"
    puts "[ActiveResource] Response: #{args.last[:result].body}"
  end if ENV['DEBUG'].present?
end