Class: WebTranslateIt::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/web_translate_it/project.rb

Class Method Summary collapse

Class Method Details

.create_locale(locale_code) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/web_translate_it/project.rb', line 58

def self.create_locale(locale_code)
  success = true
  tries ||= 3
  begin
    request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/locales")
    request.add_field("X-Client-Name", "web_translate_it")
    request.add_field("X-Client-Version", WebTranslateIt::Util.version)
    request.set_form_data({ 'id' => locale_code }, ';')
    Util.handle_response(Connection.http_connection.request(request), true)
  rescue Timeout::Error
    puts "Request timeout. Will retry in 5 seconds."
    if (tries -= 1) > 0
      sleep(5)
      retry
    else
      success = false
    end
  end
  success
end

.delete_locale(locale_code) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/web_translate_it/project.rb', line 79

def self.delete_locale(locale_code)
  success = true
  tries ||= 3
  begin
    request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/locales/#{locale_code}")
    request.add_field("X-Client-Name", "web_translate_it")
    request.add_field("X-Client-Version", WebTranslateIt::Util.version)
    Util.handle_response(Connection.http_connection.request(request), true)
  rescue Timeout::Error
    puts "Request timeout. Will retry in 5 seconds."
    if (tries -= 1) > 0
      sleep(5)
      retry
    else
      success = false
    end
  end
  success
end

.fetch_info(api_key) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/web_translate_it/project.rb', line 5

def self.fetch_info(api_key)
  success = true
  tries ||= 3
  begin
    WebTranslateIt::Connection.new(api_key) do |http|
      request = Net::HTTP::Get.new("/api/projects/#{api_key}.yaml")
      request.add_field("X-Client-Name", "web_translate_it")
      request.add_field("X-Client-Version", WebTranslateIt::Util.version)
      response = http.request(request)
      if response.is_a?(Net::HTTPSuccess)
        return response.body
      else
        puts "An error occured while fetching the project information:"
        puts StringUtil.failure(response.body)
        exit 1
      end
    end
  rescue Timeout::Error
    puts "Request timeout. Will retry in 5 seconds."
    if (tries -= 1) > 0
      sleep(5)
      retry
    else
      success = false
    end
  rescue
    puts $!.inspect
  end
  success
end

.fetch_stats(api_key) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/web_translate_it/project.rb', line 36

def self.fetch_stats(api_key)
  success = true
  tries ||= 3
  begin
    WebTranslateIt::Connection.new(api_key) do |http|
      request = Net::HTTP::Get.new("/api/projects/#{api_key}/stats.yaml")
      request.add_field("X-Client-Name", "web_translate_it")
      request.add_field("X-Client-Version", WebTranslateIt::Util.version)
      return Util.handle_response(http.request(request), true)
    end
  rescue Timeout::Error
    puts "Request timeout. Will retry in 5 seconds."
    if (tries -= 1) > 0
      sleep(5)
      retry
    else
      success = false
    end
  end
  success
end