module Sailplay::Rails::Client

Public Class Methods

included(base) click to toggle source
# File lib/sailplay/rails/client.rb, line 4
def self.included(base)
  base.send :around_filter, :prepare_sailplay_options

  base.send :helper_method, :render_sailplay_client
end

Protected Instance Methods

add_sailplay_points(user_id, points, comment) click to toggle source
# File lib/sailplay/rails/client.rb, line 56
def add_sailplay_points(user_id, points, comment)
  key = Sailplay.add_points(user_id, points, comment)
  sailplay_options :public_key => key
rescue Sailplay::Error => e
  logger.error "Error adding points: #{e.message}"
end
assign_sailplay_user(user) click to toggle source
# File lib/sailplay/rails/client.rb, line 12
def assign_sailplay_user(user)
  sailplay_options(:origin_user_id => user.sailplay_user_id) if user.respond_to?(:sailplay_user_id)
  sailplay_options(:probable_user_phone => user.sailplay_phone) if user.respond_to?(:sailplay_user_id)
end
authenticate_sailplay_user(phone, force_reload = false) click to toggle source
# File lib/sailplay/rails/client.rb, line 28
def authenticate_sailplay_user(phone, force_reload = false)
  return if phone.nil?

  if force_reload || (session[:sailplay] && session[:sailplay][:auth_expires] < Time.now)
    session[:sailplay] = nil
  end

  unless session[:sailplay]
    user = begin
      Sailplay.find_user(phone, :auth => true)
    rescue Sailplay::APIError
      Sailplay.create_user(phone, :auth => true) rescue nil
    end

    if user
      sailplay_options :auth_hash => user.auth_hash, :auth_expires => 3.days.from_now
    end
  end
end
render_sailplay_client(options = {}) click to toggle source
# File lib/sailplay/rails/client.rb, line 17
def render_sailplay_client(options = {})
  @_sailplay_client_fired = true
  result = sailplay_compile_template(sailplay_options.merge(options))

  if result.respond_to?(:html_safe)
    result.html_safe
  else
    result
  end
end
report_sailplay_purchase(user_id, order_id, price, points_rate = 0.15) click to toggle source
# File lib/sailplay/rails/client.rb, line 48
def report_sailplay_purchase(user_id, order_id, price, points_rate = 0.15)
  purchase = Sailplay.create_purchase(user_id, price, :order_id => order_id, :points_rate => points_rate)
  sailplay_options :public_key => purchase.public_key
  purchase
rescue Sailplay::Error => e
  logger.error "Error reporting purchase to Sailplay: #{e.message}"
end
sailplay_options(options = {}) click to toggle source
# File lib/sailplay/rails/client.rb, line 64
def sailplay_options(options = {})
  (@_sailplay_options ||= {}).merge! options
end

Private Instance Methods

load_sailplay_options() click to toggle source
# File lib/sailplay/rails/client.rb, line 83
def load_sailplay_options
  @_sailplay_options = session.delete :sailplay
end
prepare_sailplay_options() { || ... } click to toggle source
# File lib/sailplay/rails/client.rb, line 70
def prepare_sailplay_options
  load_sailplay_options
  yield
  if @_sailplay_client_fired
    reset_sailplay_options
  end
  save_sailplay_options
end
reset_sailplay_options() click to toggle source
# File lib/sailplay/rails/client.rb, line 87
def reset_sailplay_options
  @_sailplay_options = nil
  session[:sailplay] = nil
end
sailplay_compile_template(options) click to toggle source
# File lib/sailplay/rails/client.rb, line 93
def sailplay_compile_template(options)
  template_options = {
    :file          => File.join(File.dirname(__FILE__), '..', '..', 'templates', 'sailplay_client'),
    :layout        => false,
    :use_full_path => false,
    :handlers      => [:erb],
    :locals        => {
      :host           => Sailplay.configuration.host,
      :api_path       => Sailplay.configuration.js_api_path,
      :store_id       => Sailplay.configuration.store_id,
      :position       => Sailplay.configuration.js_position.to_s.split('_'),
      :skin           => Sailplay.configuration.skin,
      :origin_user_id => '',
      :user_phone     => '',
      :auth_hash      => '',
      :public_key     => 'none',
      :link           => '',
      :pic            => ''
    }
  }

  template_options[:locals].merge!(options)

  case @template
    when ActionView::Template
      @template.render template_options
    else
      render_to_string template_options
  end
end
save_sailplay_options() click to toggle source
# File lib/sailplay/rails/client.rb, line 79
def save_sailplay_options
  (session[:sailplay] ||= {}).merge! @_sailplay_options if @_sailplay_options
end