Class: Rack::Webtranslateit::TranslationFile::ForLocale

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/webtranslateit/translation_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fileObject (readonly)

Returns the value of attribute file.



17
18
19
# File 'lib/rack/webtranslateit/translation_file.rb', line 17

def file
  @file
end

#localeObject (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_urlObject



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

Returns:

  • (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

Returns:

  • (Boolean)


22
23
24
# File 'lib/rack/webtranslateit/translation_file.rb', line 22

def exists?
  File.exists?(file_path)
end

#fetchObject



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_pathObject



65
66
67
# File 'lib/rack/webtranslateit/translation_file.rb', line 65

def file_path
  @file_path ||= File.expand_path(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

Yields:

  • (http)


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

Returns:

  • (Boolean)


33
34
35
# File 'lib/rack/webtranslateit/translation_file.rb', line 33

def modified_remotely?
  get_translations.code.to_i == 200
end

#sendObject



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