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] || 0 @updated_at = synchronization_data[:updated_at] || 0 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
57 58 59 |
# File 'lib/localeapp/poller.rb', line 57 def handle_failure(response) @success = false end |
#handle_success(response) ⇒ Object
51 52 53 54 55 |
# File 'lib/localeapp/poller.rb', line 51 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 |
#needs_polling? ⇒ Boolean
34 35 36 |
# File 'lib/localeapp/poller.rb', line 34 def needs_polling? synchronization_data[:polled_at] < (Time.now.to_i - Localeapp.configuration.poll_interval) end |
#needs_reloading? ⇒ Boolean
38 39 40 |
# File 'lib/localeapp/poller.rb', line 38 def needs_reloading? synchronization_data[:updated_at] != @updated_at end |
#poll! ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/localeapp/poller.rb', line 42 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 |
# File 'lib/localeapp/poller.rb', line 20 def synchronization_data if File.exists?(Localeapp.configuration.synchronization_data_file) YAML.load_file(Localeapp.configuration.synchronization_data_file) else {} end end |
#write_synchronization_data!(polled_at, updated_at) ⇒ Object
28 29 30 31 32 |
# File 'lib/localeapp/poller.rb', line 28 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 |