Class: JustizSync::Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/justiz_sync/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Transfer

Returns a new instance of Transfer.



20
21
22
23
24
# File 'lib/justiz_sync/cli.rb', line 20

def initialize(options)
  @options = options
  STDOUT.sync = true
  open_connection
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/justiz_sync/cli.rb', line 19

def options
  @options
end

#streamObject (readonly)

Returns the value of attribute stream.



19
20
21
# File 'lib/justiz_sync/cli.rb', line 19

def stream
  @stream
end

Instance Method Details

#syncObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/justiz_sync/cli.rb', line 26

def sync
  @stream = Stream.new
  delete_unused = false
  if (state = options['state'])
    sync_state(state)
  else
    sync_all
    delete_unused = true
  end
  stream.close(delete_unused)
end

#sync_allObject



38
39
40
41
42
43
# File 'lib/justiz_sync/cli.rb', line 38

def sync_all
  states = scraper.states
  states.keys.each do |state|
    sync_state(state)
  end
end

#sync_courts(courts) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/justiz_sync/cli.rb', line 51

def sync_courts(courts)
  courts.each_with_index do |court, index|
    stream.sync(court)
    puts if verbose && index && (index % 20 == 0)
    putc('.') if verbose
  end
  puts if verbose
end

#sync_state(state) ⇒ Object



45
46
47
48
49
# File 'lib/justiz_sync/cli.rb', line 45

def sync_state(state)
  courts = scraper.contacts_for(state)
  puts "Syncing #{state} #{courts.length} courts"
  sync_courts(courts)
end