Class: Hackpad::Migration::Migrator
- Inherits:
-
Object
- Object
- Hackpad::Migration::Migrator
- Defined in:
- lib/hackpad/migration/migrator.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #exit_if_error(result, msg) ⇒ Object
-
#initialize(last_result) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
Constructor Details
#initialize(last_result) ⇒ Migrator
Returns a new instance of Migrator.
5 6 7 8 9 10 |
# File 'lib/hackpad/migration/migrator.rb', line 5 def initialize(last_result) = last_result. @source = API.new(['source_site'], ['source_client_id'], ['source_secret']) @target = API.new(['target_site'], ['target_client_id'], ['target_secret']) @result = last_result end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
4 5 6 |
# File 'lib/hackpad/migration/migrator.rb', line 4 def result @result end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
4 5 6 |
# File 'lib/hackpad/migration/migrator.rb', line 4 def source @source end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
4 5 6 |
# File 'lib/hackpad/migration/migrator.rb', line 4 def target @target end |
Instance Method Details
#exit_if_error(result, msg) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/hackpad/migration/migrator.rb', line 36 def exit_if_error(result, msg) if result.is_a?(Hash) && !result['success'] puts "#{msg}, #{result['error']}" exit(1) end end |
#migrate ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hackpad/migration/migrator.rb', line 12 def migrate pad_list = source.list exit_if_error(pad_list, 'Get error when fetching pad list from source site') pad_list.each do |source_pid| body = source.get(source_pid) unless body[/You'll need to turn on JavaScript to use Hackpad in all of its/].nil? puts "WARNING: the pad from source #{source_pid} seems not found." end target_pid = result.source_and_target_map[source_pid] if target_pid.nil? pad = target.create(body) exit_if_error(pad, 'Got error when creating pad for target site') # pad {"padId"=>"RAtutOXxMGF1", "globalPadId"=>"2$RAtutOXxMGF1"} result.add_source_and_target(source_pid, pad['padId']) puts "created pad #{pad['padId']} from source pad #{source_pid}" result.write! else puts "Updating pad #{target_pid} from source pad #{source_pid}" pad = target.update(target_pid, body) exit_if_error(pad, 'Got error when update pad for target site') end end end |