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.



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

def initialize(file, locale)
  @file, @locale = file, locale
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



20
21
22
# File 'lib/rack/webtranslateit/translation_file.rb', line 20

def file
  @file
end

#localeObject (readonly)

Returns the value of attribute locale.



20
21
22
# File 'lib/rack/webtranslateit/translation_file.rb', line 20

def locale
  @locale
end

Instance Method Details

#api_urlObject



84
85
86
# File 'lib/rack/webtranslateit/translation_file.rb', line 84

def api_url
  "/api/projects/#{file.api_key}/files/#{file.id}/locales/#{locale}"
end

#committed?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/rack/webtranslateit/translation_file.rb', line 29

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)


25
26
27
# File 'lib/rack/webtranslateit/translation_file.rb', line 25

def exists?
  File.exists?(file_path)
end

#fetchObject



52
53
54
55
# File 'lib/rack/webtranslateit/translation_file.rb', line 52

def fetch
  response = get_translations
  File.open(file_path, 'w'){|f| f << response.body } if response.code.to_i == 200 and not response.body == ""
end

#fetch!Object



57
58
59
60
# File 'lib/rack/webtranslateit/translation_file.rb', line 57

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 == ""
end

#file_pathObject



72
73
74
# File 'lib/rack/webtranslateit/translation_file.rb', line 72

def file_path
  @file_path ||= File.expand_path(file.file_path.gsub("[locale]", locale))
end

#get_translations(respect_modified_since = true) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rack/webtranslateit/translation_file.rb', line 44

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
    http.request(request)
  end
end

#http_connection {|http| ... } ⇒ Object

Yields:

  • (http)


76
77
78
79
80
81
82
# File 'lib/rack/webtranslateit/translation_file.rb', line 76

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

#master?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/webtranslateit/translation_file.rb', line 36

def master?
  @file.master_locale == locale
end

#modified_remotely?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rack/webtranslateit/translation_file.rb', line 40

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

#uploadObject



62
63
64
65
66
67
68
69
70
# File 'lib/rack/webtranslateit/translation_file.rb', line 62

def upload
  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)
      raise "Failed to Upload File, #{response.code} response recieved" unless response.code.to_i < 400
    end
  end
end