app/models/lit/source.rb in lit-0.2.1 vs app/models/lit/source.rb in lit-0.2.2
- old
+ new
@@ -18,10 +18,11 @@
unless defined?(::ActionController::StrongParameters)
attr_accessible :api_key, :identifier, :url
end
## BEFORE & AFTER
+ before_create :set_last_updated_at_upon_creation
after_validation :check_if_url_is_valid
def get_last_change
result = get_from_remote(LAST_CHANGE_PATH)
@@ -48,40 +49,55 @@
il.save!
IncommingLocalization.where(:id=>il.id).update_all ['translated_value=?', r["value"]]
end
lc = get_last_change
lc = DateTime.parse(lc) unless lc.nil?
- self.last_updated_at = lc || Time.now
+ touch_last_updated_at(lc)
self.save
end
end
end
+ def touch_last_updated_at!
+ touch_last_updated_at
+ self.save
+ end
+
private
+ def touch_last_updated_at(time=nil)
+ self.last_updated_at = time || Time.now
+ end
+
def check_if_url_is_valid
if self.errors.empty? && (self.new_record? || self.url_changed?)
self.errors.add(:url, "is not accessible") if get_last_change.nil?
end
end
def get_from_remote(path, query_values={})
result = nil
- #begin
+ begin
uri = URI(self.url+path)
query_values.each do |k,v|
- params = URI.decode_www_form(uri.query || []) << [k, v]
+ params = URI.decode_www_form(uri.query || "") << [k, v]
uri.query = URI.encode_www_form(params)
end
req = Net::HTTP::Get.new(uri.request_uri)
req.add_field("Authorization", %(Token token="#{self.api_key}"))
res = Net::HTTP.new(uri.host, uri.port).start do |http|
http.request(req)
end
if res.is_a?(Net::HTTPSuccess)
result = JSON.parse(res.body)
end
- #rescue
- #end
+ rescue
+ end
result
+ end
+
+ def set_last_updated_at_upon_creation
+ if self.last_updated_at.blank?
+ touch_last_updated_at if Lit.set_last_updated_at_upon_creation
+ end
end
end
end