Module: CampfireExport::IO

Included in:
Account, Message, Room, Transcript, Upload
Defined in:
lib/campfire_export.rb

Instance Method Summary collapse

Instance Method Details

#api_url(path) ⇒ Object



36
37
38
# File 'lib/campfire_export.rb', line 36

def api_url(path)
  "#{CampfireExport::Account.base_url}#{path}"
end

#export_dirObject

Requires that room and date be defined in the calling object.



56
57
58
59
# File 'lib/campfire_export.rb', line 56

def export_dir
  "campfire/#{Account.subdomain}/#{room.name}/" +
    "#{date.year}/#{zero_pad(date.mon)}/#{zero_pad(date.day)}"
end

#export_file(content, filename, mode = 'w') ⇒ Object

Requires that room_name and date be defined in the calling object.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/campfire_export.rb', line 62

def export_file(content, filename, mode='w')
  # Check to make sure we're writing into the target directory tree.
  true_path = File.expand_path(File.join(export_dir, filename))
  
  unless true_path.start_with?(File.expand_path(export_dir))
    raise CampfireExport::Exception.new("#{export_dir}/#{filename}",
      "can't export file to a directory higher than target directory; " +
      "expected: #{File.expand_path(export_dir)}, actual: #{true_path}.")
  end
  
  if File.exists?("#{export_dir}/#{filename}")
    log(:error, "#{export_dir}/#{filename} failed: file already exists")
  else
    open("#{export_dir}/#{filename}", mode) do |file|
      file.write content
    end
  end
end

#get(path, params = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/campfire_export.rb', line 40

def get(path, params = {})
  url = api_url(path)
  response = HTTParty.get(url, :query => params, :basic_auth => 
    {:username => CampfireExport::Account.api_token, :password => 'X'})

  if response.code >= 400
    raise CampfireExport::Exception.new(url, response.message, response.code)
  end
  response
end

#log(level, message, exception = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/campfire_export.rb', line 94

def log(level, message, exception=nil)
  case level
  when :error
    short_error = ["*** Error: #{message}", exception].compact.join(": ")
    $stderr.puts short_error
    open("campfire/export_errors.txt", 'a') do |log|
      log.write short_error
      unless exception.nil?
        log.write %Q{\n\t#{exception.backtrace.join("\n\t")}}
      end
      log.write "\n"
    end
  else
    print message
    $stdout.flush
  end
end

#verify_export(filename, expected_size) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/campfire_export.rb', line 81

def verify_export(filename, expected_size)
  full_path = "#{export_dir}/#{filename}"
  unless File.exists?(full_path)
    raise CampfireExport::Exception.new(full_path, 
      "file should have been exported but did not make it to disk")
  end
  unless File.size(full_path) == expected_size
    raise CampfireExport::Exception.new(full_path, 
      "exported file exists but is not the right size " +
      "(expected: #{expected_size}, actual: #{File.size(full_path)})")
  end
end

#zero_pad(number) ⇒ Object



51
52
53
# File 'lib/campfire_export.rb', line 51

def zero_pad(number)
  "%02d" % number
end