Class: GridCLI::UpdateCommand

Inherits:
BaseCommand show all
Defined in:
lib/gridcli/commands/update.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#cmd, #desc

Instance Method Summary collapse

Methods inherited from BaseCommand

#add_format_option, #add_option, #error, #log, #output_format, #parse_dates, #parse_opts, #pop_arg, #pprint, #usage

Constructor Details

#initializeUpdateCommand

Returns a new instance of UpdateCommand.



3
4
5
6
# File 'lib/gridcli/commands/update.rb', line 3

def initialize
  super "update", "Download all messages"
  add_format_option
end

Instance Method Details

#run(args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gridcli/commands/update.rb', line 8

def run(args)
  parse_opts args

  posts = run_since_sha(@stats['last_sha'])
  total_posts = posts.length
  @stats['last_sha'] = save(posts) if posts.length > 0

  # If we're doing this in chunks, get more
  while posts.length == 300
    posts = run_since_sha(@stats['last_sha'])
    total_posts += posts.length
    @stats['last_sha'] = save(posts) if posts.length > 0
  end

  @stats.save
  puts "Saved #{total_posts} posts.  Now up to date."
end

#run_since_sha(last_sha) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gridcli/commands/update.rb', line 27

def run_since_sha(last_sha)
  GridCLI.hooker.invoke :before_update

  posts = []
  begin
    if last_sha.nil?
      log "Trying to download all messages."
      posts = Post::Message.find(:all)
    else
      log "Trying to download all messages since '#{last_sha}'"
      posts = Post::Message.find(:all, :params => { :id => last_sha })
    end
  rescue ActiveResource::ClientError
    puts "There was an error updating your messages."
  end

  GridCLI.hooker.invoke :after_update, posts
end

#save(posts) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/gridcli/commands/update.rb', line 46

def save(posts)
  posts.each { |p| 
    type = p.known_attributes.first
    json = p.send(type).to_json(:root => type)
    pprint json
  }
  (posts.length > 0) ? GridCLI.storage.append(posts) : nil
end