Class: SODA::SmartUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/soda/smartupdate.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ SmartUpdate

Creates a new SmartUpdate client.

  • config - A hash of the options to initialize the client with. The options are the same as SODA::Client

Config Options

  • :domain - The domain you want to access

  • :username - Your Socrata username (optional, only necessary for modifying data)

  • :password - Your Socrata password (optional, only necessary for modifying data)

  • :app_token - Your Socrata application token (register at dev.socrata.com/register)

  • :ignore_ssl - Ignore ssl errors (defaults to false)

Returns a SODA::Client instance.

Example

client = SODA::Client.new({ :domain => "data.agency.gov", :app_token => "CGxarwoQlgQSev4zyUh5aR5J3" })


32
33
34
35
36
37
38
# File 'lib/soda/smartupdate.rb', line 32

def initialize(config = {})
  @config = config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
  @client = SODA::Client.new(@config)

  # Fetch version and blocksize information from the service
  @blocksize = @client.get("/datasync/version.json")["max-block-size"]
end

Instance Method Details

#check(uid, job_id) ⇒ Object

Check on the status of a job



59
60
61
# File 'lib/soda/smartupdate.rb', line 59

def check(uid, job_id)
  @client.get("/datasync/id/#{uid}/status/#{job_id}", nil, nil)
end

#commit(uid, filename, chunks) ⇒ Object

Commits a set of chunks to be written



54
55
56
# File 'lib/soda/smartupdate.rb', line 54

def commit(uid, filename, chunks)
  @client.post("/datasync/id/#{uid}/commit", {:filename => filename, :chunks => chunks}).jobId
end

#upload(uid, filename, options = {}) ⇒ Object

Chunks a file for smart-updating and uploads it



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/soda/smartupdate.rb', line 41

def upload(uid, filename, options = {})
  options = DEFAULT_OPTIONS.merge(options)

  # Determine the file size, and ideal chunk size
  filesize = File.size(filename)
  blocks = (0..filesize).step(@blocksize).collect do |offset|
    @client.post("/datasync/id/#{uid}", 
                 IO.read(filename, @blocksize, offset)).blobId
  end
  return blocks
end