Class: TfUploader::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/tf_uploader/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, token) ⇒ Uploader

Returns a new instance of Uploader.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/tf_uploader/uploader.rb', line 4

def initialize(directory, token)
  @base_path = File.expand_path(directory)
  @token = token

  @connection = ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3",
    :database  => File.join(@base_path, "TombfinderNew.sqlite"))

  unless Tomb.columns.map(&:name).include? "uploaded"
    @connection.connection.add_column('ZTOMB', 'uploaded', 'boolean', :default => false)
    Tomb.reset_column_information
  end
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



2
3
4
# File 'lib/tf_uploader/uploader.rb', line 2

def base_path
  @base_path
end

#connectionObject

Returns the value of attribute connection.



2
3
4
# File 'lib/tf_uploader/uploader.rb', line 2

def connection
  @connection
end

#tokenObject

Returns the value of attribute token.



2
3
4
# File 'lib/tf_uploader/uploader.rb', line 2

def token
  @token
end

Instance Method Details

#post(tomb) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tf_uploader/uploader.rb', line 29

def post(tomb)
  picture = square_pic(tomb)

  response = RestClient.post(
    "#{TfUploader::HOST}/api/records.json?auth_token=#{token}",
    :record => tomb.as_json(:gravepic => picture),
    :favorite => "false",
    :name => "record",
    :secret => "U8BsP0TZF",
    :timeout => 90
  )
  if response.code == 201
    tomb.update_attribute(:uploaded, true)
    print "201 "
  else
    print "#{response.code} "
  end
rescue RestClient::UnprocessableEntity => e
  print "422 "
rescue RestClient::RequestTimeout => e
  print "<|> "

rescue RestClient::InternalServerError => e
  print "500 "
  sleep 20
ensure
  STDOUT.flush
end

#square_pic(tomb) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tf_uploader/uploader.rb', line 58

def square_pic(tomb)
  image = MiniMagick::Image.open(File.join(base_path, tomb.file_system_file.path))
  if image['width'] > image['height']
    geometry = "#{image['height']}x#{image['height']}+#{image['height']/2}+0"
  else
    geometry = "#{image['width']}x#{image['width']}+0+#{image['width']/2}"
  end

  image.crop geometry
  image.auto_orient

  cropped_path = File.join(base_path, "cropped_" + tomb.file_system_file.path)
  image.write cropped_path
  File.new(cropped_path, 'rb')
end

#upload!Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/tf_uploader/uploader.rb', line 18

def upload!
  tombs = Tomb.where(:uploaded => false)
  puts "Posting #{tombs.count} records (#{Tomb.where(:uploaded => true).count} already posted)"
  puts "Response Codes:"
  tombs.each do |tomb|
    post(tomb)
  end
  puts ""
  puts "Done!"
end