Class: TerminalPingsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/terminal_pings_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_terminal

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/terminal_pings_controller.rb', line 4

def create
  begin
    profile = @terminal.terminal_profile

    ping_data = params[:terminal_ping]
    remote_timestamp = ping_data[:providers_updated_at].blank? ? nil : DateTime.parse(ping_data[:providers_updated_at])
    local_timestamp = nil
    profile.cached_providers_lock.lock { local_timestamp = profile.actual_timestamp }

    @terminal.ping!(TerminalPing.new ping_data)

    response = {
      :time => DateTime.now,
      :address => @terminal.address,
      :profile => {
        :support_phone => profile.support_phone,
        :logo          => profile..url,
        :modified_at   => profile.updated_at
      },
      :orders => @terminal.terminal_orders.unsent.as_json(:only => [:id, :keyword, :args, :created_at]),
      :update_providers => remote_timestamp.blank? || local_timestamp.to_i > remote_timestamp.to_i, # to drop microseconds
      :last_session_started_at => @terminal.last_session_started_at
    }

    render :json => response
  rescue ActiveRecord::RecordInvalid
    render :text => nil, :status => 400
  end
end

#providersObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/terminal_pings_controller.rb', line 34

def providers
  profile = @terminal.terminal_profile

  providers = nil
  profile.cached_providers_lock.lock do
    providers = profile.cached_providers.value

    if providers.nil?
      ActiveRecord::Base.transaction do
        providers = {
          :providers  => profile.providers_dump,
          :groups     => profile.provider_groups_dump,
          :promotions => profile.promotions_dump,
          :updated_at => profile.actual_timestamp
        }
      end

      providers = ActiveSupport::JSON.encode(providers)
      profile.cached_providers.value = providers
    end
  end

  send_data providers, :type => 'application/json', :disposition => 'inline'
end