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
- #sync_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 19 |
# File 'lib/localeapp/poller.rb', line 15 def initialize @sync_file = SyncFile.new( Localeapp.configuration.synchronization_data_file ) @polled_at = sync_data.polled_at @updated_at = sync_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
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/localeapp/poller.rb', line 54 def handle_failure(response) case response.code when 304 Localeapp.log_with_time "No new data" # Nothing new, update synchronization files write_synchronization_data!(current_time, updated_at) when 404 fail APIResponseError, "API returned #{response.code} status code" end @success = false end |
#handle_success(response) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/localeapp/poller.rb', line 47 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
30 31 32 |
# File 'lib/localeapp/poller.rb', line 30 def needs_polling? sync_data.polled_at < (Time.now.to_i - Localeapp.configuration.poll_interval) end |
#needs_reloading? ⇒ Boolean
34 35 36 |
# File 'lib/localeapp/poller.rb', line 34 def needs_reloading? sync_data.updated_at != @updated_at end |
#poll! ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/localeapp/poller.rb', line 38 def poll! api_call :translations, :url_options => { :query => { :updated_at => updated_at }}, :success => :handle_success, :failure => :handle_failure, :max_connection_attempts => 1 @success end |
#sync_data ⇒ Object
21 22 23 24 |
# File 'lib/localeapp/poller.rb', line 21 def sync_data sync_file.refresh sync_file.data end |
#write_synchronization_data!(polled_at, updated_at) ⇒ Object
26 27 28 |
# File 'lib/localeapp/poller.rb', line 26 def write_synchronization_data!(polled_at, updated_at) sync_file.write(polled_at, updated_at) end |