class Skippr::Api

Public Class Methods

app_key() click to toggle source
# File lib/skippr/api.rb, line 126
def app_key
  self.thread_container['app_key']
end
app_token() click to toggle source
# File lib/skippr/api.rb, line 130
def app_token
  self.thread_container['app_token']
end
assert_configuration!(conf_hash) click to toggle source
# File lib/skippr/api.rb, line 147
def assert_configuration!(conf_hash)
  required_confs = [:app_key, :app_token, :client_user_api_key, :client_user_api_token, :client_name]

  unknowns = conf_hash.symbolize_keys.except(*required_confs)
  unless unknowns.empty?
    raise Skippr::UnknownConfigurationError.new("Unknown in api configuration: #{unknowns.keys}")
  end

  missings = required_confs - conf_hash.symbolize_keys.keys
  unless missings.empty?
    raise Skippr::MissingConfigurationError.new("Missing in api configuration: #{missings}")
  end
end
auth_params() click to toggle source

Thread safe auth params

# File lib/skippr/api.rb, line 90
def auth_params
  assert_configuration!(self.thread_container)

  valid_until = 1.hour.from_now
  sig_src = [self.client_user_api_token, self.app_token, valid_until.to_time.to_i.to_s].join(":")
  signature = Digest::SHA2.hexdigest(sig_src)
  {
    app:        self.app_key,
    user:       self.client_user_api_key,
    validuntil: valid_until.to_time.to_i.to_s,
    signature:  signature
  }
end
client_name() click to toggle source
# File lib/skippr/api.rb, line 122
def client_name
  self.thread_container['client_name']
end
client_user_api_key() click to toggle source
# File lib/skippr/api.rb, line 134
def client_user_api_key
  self.thread_container['client_user_api_key']
end
client_user_api_token() click to toggle source
# File lib/skippr/api.rb, line 138
def client_user_api_token
  self.thread_container['client_user_api_token']
end
collection_path(prefix_options = {}, query_options = nil) click to toggle source
# File lib/skippr/api.rb, line 36
def collection_path(prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  query_options = ( query_options || {} ).merge(self.auth_params)
  "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end
configure(configuration_hash) click to toggle source
# File lib/skippr/api.rb, line 105
def configure(configuration_hash)
  configuration_hash.each do |key, value|
    self.thread_container["#{key}"] = value
  end

  reset_connection
end
configure_endpoint(configuration_hash) click to toggle source
# File lib/skippr/api.rb, line 113
def configure_endpoint(configuration_hash)
  configuration_hash[:protocol] ||= "https"
  configuration_hash[:domain] ||= "skippr.com"
  endpoint_site = [configuration_hash[:protocol], configuration_hash[:domain]].join("://")
  endpoint_site += ":" + configuration_hash[:port].to_s if configuration_hash[:port].present?
  self.site = endpoint_site + "/api/v2/"
  reset_connection
end
delete(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 51
def delete(custom_method_name, options={})
  super(custom_method_name, options.merge(self.auth_params))
end
element_path(id, prefix_options = {}, query_options = nil) click to toggle source

Thread safety for active_resource, see monkey patch in monkey.rb

# File lib/skippr/api.rb, line 30
def element_path(id, prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  query_options = ( query_options || {} ).merge(self.auth_params)
  "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
end
get(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 42
def get(custom_method_name, options={})
  super(custom_method_name, options.merge(self.auth_params))
end
include_root_in_json() click to toggle source
# File lib/skippr/api.rb, line 24
def include_root_in_json
  false
end
post(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 45
def post(custom_method_name, options={})
  super(custom_method_name, options.merge(self.auth_params))
end
put(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 48
def put(custom_method_name, options={})
  super(custom_method_name, options.merge(self.auth_params))
end
thread_container() click to toggle source
# File lib/skippr/api.rb, line 142
def thread_container
  Thread.current['skippr.api.conf'] ||= {}
  Thread.current['skippr.api.conf']
end
valid?() click to toggle source

END ActiveResource API

# File lib/skippr/api.rb, line 57
def valid?
  begin
    uri = URI.parse(self.site.to_s + 'auth/valid?' + self.auth_params.to_param)
    http = Net::HTTP.new(uri.host, uri.port)
    if self.site.scheme == 'https'
      http.use_ssl= true
    end
    rq = Net::HTTP::Get.new(uri.request_uri)
    unless self.user.blank?
      rq.basic_auth(self.user, self.password)
    end
    response = http.request(rq)
    body = response.read_body
    if response.code == '200' && body == 'Ok'
      true
    else
      Rails.logger.info "Server responded: (#{response.code}) #{body}"
      false
    end

  rescue Timeout::Error
    Rails.logger.warn "timeout"
    false
  rescue Errno::ECONNREFUSED
    Rails.logger.warn "no api connection"
    false
  rescue
    Rails.logger.info "exception when trying to authenticate"
    false
  end
end

Public Instance Methods

delete(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 15
def delete(custom_method_name, options={})
  super(custom_method_name, options.merge(self.class.auth_params))
end
get(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 6
def get(custom_method_name, options={})
  super(custom_method_name, options.merge(self.class.auth_params))
end
post(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 9
def post(custom_method_name, options={})
  super(custom_method_name, options.merge(self.class.auth_params))
end
put(custom_method_name, options={}) click to toggle source
Calls superclass method
# File lib/skippr/api.rb, line 12
def put(custom_method_name, options={})
  super(custom_method_name, options.merge(self.class.auth_params))
end