This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcharacter.rb
105 lines (84 loc) · 2.3 KB
/
character.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'active_support/core_ext'
require 'aws/s3'
require 'data_mapper'
require 'net/http'
require 'json'
class APINotOkError < StandardError
end
class Character
class << self
def bucket
bucket_name = ENV["AWS_S3_BUCKET"]
bucket_name += "-development" if ENV["RACK_ENV"] == "development"
@@bucket ||= AWS::S3.new(
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]).buckets[bucket_name]
@@bucket
end
def dm_setup
DataMapper::Model.raise_on_save_failure = true
DataMapper.setup(:default, (ENV["DATABASE_URL"] || "sqlite3:///#{Dir.pwd}/development.sqlite3"))
DataMapper.auto_upgrade!
end
end
include DataMapper::Resource
before :create, :titleize
before :destroy, :delete_img
property :id, Serial
property :region, String, required: true, format: /^[a-z]{2}$/
property :realm, String, required: true
property :char, String, required: true, format: /^[[:alpha:]]+$/
property :created_at, DateTime
property :updated_at, DateTime
def char_path
"#{region}/#{realm}/#{char}"
end
def armory_url
"http://#{region}.battle.net/wow/en/character/#{realm}/#{char}/"
end
def img_uri
unless img_s3.exists?
update_img
else
Thread.new { update_img } unless @updating
end
img_s3.public_url
end
def update_img
return unless updated_at < 3.hours.ago or not img_s3.exists?
begin
@updating = true
json = JSON.parse(Net::HTTP.get(api_uri))
raise APINotOkError, json["msg"] unless json["status"] == "ok"
img_s3.write(Net::HTTP.get URI(json["link"]))
img_s3.acl = :public_read
self.update updated_at: Time.now
rescue Exception => e
raise e unless img_s3.exists?
ensure
@updating = false
end
end
private
def titleize
self.region = region.downcase
self.realm = realm.titleize
self.char = char.capitalize
end
def api_uri
uri = URI('http://www.best-signatures.com/api/')
uri.query = URI.encode_www_form({
region: region,
realm: realm,
char: char,
type: "Sign9"
})
return uri
end
def delete_img
img_s3.delete if img_s3.exists?
end
def img_s3
Character.bucket.objects["#{char_path}.png"]
end
end