null+****@clear*****
null+****@clear*****
Fri Apr 27 20:30:32 JST 2012
SUZUKI Miho 2012-04-27 20:30:32 +0900 (Fri, 27 Apr 2012) New Revision: 3a73aba1ed8e86c2cc39aaa6ae330402724e93da Merged dc203a9: Merge pull request #58 from logaling/import-tmx Log: check file/url exist before open external glossary Modified files: lib/logaling/external_glossaries/tmx.rb lib/logaling/external_glossary.rb Modified: lib/logaling/external_glossaries/tmx.rb (+0 -2) =================================================================== --- lib/logaling/external_glossaries/tmx.rb 2012-04-27 19:17:10 +0900 (66a7f27) +++ lib/logaling/external_glossaries/tmx.rb 2012-04-27 20:30:32 +0900 (02ac5ba) @@ -48,8 +48,6 @@ module Logaling end csv << [original, translation, notes.join(" | ")] if original && translation end - rescue - raise Logaling::GlossaryNotFound, "Failed open url / path <#{glossary_info[:url]}>" end end end Modified: lib/logaling/external_glossary.rb (+18 -0) =================================================================== --- lib/logaling/external_glossary.rb 2012-04-27 19:17:10 +0900 (e418fe3) +++ lib/logaling/external_glossary.rb 2012-04-27 20:30:32 +0900 (2e8e24a) @@ -14,6 +14,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. require 'active_support/inflector' +require 'open-uri' +require 'net/http' class Logaling::ExternalGlossary class << self @@ -67,6 +69,11 @@ class Logaling::ExternalGlossary end def import(glossary_info=nil) + if glossary_info && glossary_info[:url] + unless file_exists?(glossary_info[:url]) + raise Logaling::GlossaryNotFound, "Failed open url/path <#{glossary_info[:url]}>" + end + end File.open(import_file_name(glossary_info), "w") do |output| output_format = self.class.output_format output_format = output_format.to_s if output_format.is_a?(Symbol) @@ -97,4 +104,15 @@ class Logaling::ExternalGlossary self.class.target_language, self.class.output_format].join('.') end end + + def file_exists?(url_org) + url = URI.parse(url_org) + if url.host + Net::HTTP.start(url.host, url.port) do |http| + http.head(url.request_uri).code == "200" + end + else + File.exist?(url_org) + end + end end