Class: Localeapp::Poller
- Inherits:
-
Object
- Object
- Localeapp::Poller
- Includes:
- ApiCall
- Defined in:
- lib/localeapp/poller.rb
Instance Attribute Summary collapse
-
#polled_at ⇒ Object
when we last asked the service for updates.
-
#updated_at ⇒ Object
the last time the service had updates for us.
Instance Method Summary collapse
- #handle_failure(response) ⇒ Object
- #handle_success(response) ⇒ Object
-
#initialize ⇒ Poller
constructor
A new instance of Poller.
- #needs_polling? ⇒ Boolean
- #needs_reloading? ⇒ Boolean
- #poll! ⇒ Object
- #synchronization_data ⇒ Object
- #write_synchronization_data!(polled_at, updated_at) ⇒ Object
Methods included from ApiCall
Constructor Details
#initialize ⇒ Poller
Returns a new instance of Poller.
15 16 17 18 |
# File 'lib/localeapp/poller.rb', line 15 def initialize @polled_at = synchronization_data[:polled_at] @updated_at = synchronization_data[:updated_at] end |
Instance Attribute Details
#polled_at ⇒ Object
when we last asked the service for updates
10 11 12 |
# File 'lib/localeapp/poller.rb', line 10 def polled_at @polled_at end |
#updated_at ⇒ Object
the last time the service had updates for us
13 14 15 |
# File 'lib/localeapp/poller.rb', line 13 def updated_at @updated_at end |
Instance Method Details
#handle_failure(response) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/localeapp/poller.rb', line 59 def handle_failure(response) if response.code == 304 Localeapp.log_with_time "No new data" # Nothing new, update synchronization files write_synchronization_data!(current_time, updated_at) end @success = false end |
#handle_success(response) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/localeapp/poller.rb', line 52 def handle_success(response) Localeapp.log_with_time "poll success" @success = true Localeapp.updater.update(Localeapp.load_yaml(response)) write_synchronization_data!(current_time, Time.parse(response.headers[:date])) end |
#needs_polling? ⇒ Boolean
35 36 37 |
# File 'lib/localeapp/poller.rb', line 35 def needs_polling? synchronization_data[:polled_at] < (Time.now.to_i - Localeapp.configuration.poll_interval) end |
#needs_reloading? ⇒ Boolean
39 40 41 |
# File 'lib/localeapp/poller.rb', line 39 def needs_reloading? synchronization_data[:updated_at] != @updated_at end |
#poll! ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/localeapp/poller.rb', line 43 def poll! api_call :translations, :url_options => { :query => { :updated_at => updated_at }}, :success => :handle_success, :failure => :handle_failure, :max_connection_attempts => 1 @success end |
#synchronization_data ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/localeapp/poller.rb', line 20 def synchronization_data if File.exists?(Localeapp.configuration.synchronization_data_file) Localeapp.load_yaml_file(Localeapp.configuration.synchronization_data_file) || default_synchronization_data else default_synchronization_data end end |
#write_synchronization_data!(polled_at, updated_at) ⇒ Object
29 30 31 32 33 |
# File 'lib/localeapp/poller.rb', line 29 def write_synchronization_data!(polled_at, updated_at) File.open(Localeapp.configuration.synchronization_data_file, 'w+') do |f| f.write({:polled_at => polled_at.to_i, :updated_at => updated_at.to_i}.to_yaml) end end |