Sha256: 6235b35c928084d63025ad615e3a959d203e5f461fa70356c0a0611cca8e9fda
Contents?: true
Size: 1.59 KB
Versions: 1
Compression:
Stored size: 1.59 KB
Contents
require 'yaml' require 'rest-client' require 'time' module LocaleApp class Poller include ::LocaleApp::ApiCall # when we last asked the service for updates attr_accessor :polled_at # the last time the service had updates for us attr_accessor :updated_at def initialize @polled_at = synchronization_data[:polled_at] || 0 @updated_at = synchronization_data[:updated_at] || 0 end def synchronization_data if File.exists?(LocaleApp.configuration.synchronization_data_file) YAML.load_file(LocaleApp.configuration.synchronization_data_file) else {} end end def write_synchronization_data!(polled_at, updated_at) File.open(LocaleApp.configuration.synchronization_data_file, 'w+') do |f| f.write({:polled_at => polled_at, :updated_at => updated_at}.to_yaml) end end def needs_polling? synchronization_data[:polled_at] < (Time.now.to_i - LocaleApp.configuration.poll_interval) end def needs_reloading? synchronization_data[:updated_at] != @updated_at end def poll! api_call :translations, :url_options => { :query => { :updated_at => updated_at }}, :success => :handle_success, :failure => :handle_failure, :max_connection_attempts => 1 @success end def handle_success(response) @success = true LocaleApp.updater.update(JSON.parse(response)) write_synchronization_data!(Time.now.to_i, Time.parse(response.headers[:date]).to_i) end def handle_failure(response) @success = false end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
localeapp-0.0.7 | lib/locale_app/poller.rb |