Class: Rack::Webtranslateit::TranslationFile::ForLocale
- Inherits:
-
Object
- Object
- Rack::Webtranslateit::TranslationFile::ForLocale
- Defined in:
- lib/rack/webtranslateit/translation_file.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
Instance Method Summary collapse
- #api_url ⇒ Object
- #committed? ⇒ Boolean
- #exists? ⇒ Boolean
- #fetch ⇒ Object
- #fetch! ⇒ Object
- #file_path ⇒ Object
- #get_translations(respect_modified_since = true) ⇒ Object
- #http_connection {|http| ... } ⇒ Object
-
#initialize(file, locale) ⇒ ForLocale
constructor
A new instance of ForLocale.
- #modified_remotely? ⇒ Boolean
- #send ⇒ Object
Constructor Details
#initialize(file, locale) ⇒ ForLocale
Returns a new instance of ForLocale.
18 19 20 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 18 def initialize(file, locale) @file, @locale = file, locale end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
17 18 19 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 17 def file @file end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
17 18 19 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 17 def locale @locale end |
Instance Method Details
#api_url ⇒ Object
77 78 79 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 77 def api_url "/api/projects/#{file.api_key}/files/#{file.id}/locales/#{locale}" end |
#committed? ⇒ Boolean
26 27 28 29 30 31 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 26 def committed? Dir.chdir(File.dirname(file_path)) do system "git status | grep 'modified: #{File.basename(file_path)}' > /dev/null" end ! $?.success? end |
#exists? ⇒ Boolean
22 23 24 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 22 def exists? File.exists?(file_path) end |
#fetch ⇒ Object
45 46 47 48 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 45 def fetch response = get_translations File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body.blank? end |
#fetch! ⇒ Object
50 51 52 53 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 50 def fetch! response = get_translations(false) File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body.blank? end |
#file_path ⇒ Object
65 66 67 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 65 def file_path @file_path ||= File.(file.file_path.gsub("[locale]", locale)) end |
#get_translations(respect_modified_since = true) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 37 def get_translations(respect_modified_since = true) http_connection do |http| request = Net::HTTP::Get.new(api_url) request.add_field('If-Modified-Since', File.mtime(File.new(file_path, 'r')).rfc2822) if File.exist?(file_path) and respect_modified_since return http.request(request) end end |
#http_connection {|http| ... } ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 69 def http_connection http = Net::HTTP.new('webtranslateit.com', 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.read_timeout = 10 yield http end |
#modified_remotely? ⇒ Boolean
33 34 35 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 33 def modified_remotely? get_translations.code.to_i == 200 end |
#send ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/rack/webtranslateit/translation_file.rb', line 55 def send File.open(file_path) do |file| http_connection do |http| request = Net::HTTP::Put::Multipart.new(api_url, "file" => UploadIO.new(file, "text/plain", file.path)) response = http.request(request) return response.code.to_i end end end |