Class: TranslationEngine::Connection

Inherits:
Object
  • Object
show all
Defined in:
app/models/translation_engine/connection.rb

Constant Summary collapse

NotFound =
Class.new(Exception)

Instance Method Summary collapse

Instance Method Details

#get_release(version) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
# File 'app/models/translation_engine/connection.rb', line 46

def get_release(version)
  response = connection.get do |req|
    req.url "/api/v1/releases/#{version}.yaml"
    req.headers['Authorization'] = api_token
  end

  raise NotFound, "Release #{version} Not found" if response.status != 200

  response
end

#get_releasesObject



35
36
37
38
39
40
41
42
43
44
# File 'app/models/translation_engine/connection.rb', line 35

def get_releases
  response = connection.get do |req|
    req.url '/api/v1/releases.json'
    req.headers['Authorization'] = api_token
  end

  JSON.parse(response.body).with_indifferent_access[:releases].map do |args|
    TranslationEngine::Release.new args
  end
end

#get_translationsObject



57
58
59
60
61
62
# File 'app/models/translation_engine/connection.rb', line 57

def get_translations
  connection.get do |req|
    req.url '/api/v1/translations.yaml'
    req.headers['Authorization'] = api_token
  end
end

#get_translations_headObject



64
65
66
67
68
69
# File 'app/models/translation_engine/connection.rb', line 64

def get_translations_head
  connection.head do |req|
    req.url '/api/v1/translations.yaml'
    req.headers['Authorization'] = api_token
  end
end

#send_images(data, ip_address = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'app/models/translation_engine/connection.rb', line 7

def send_images(data, ip_address = nil)
  connection(60).post do |req|
    req.url '/api/v1/images'
    req.headers['Content-Type']  = 'application/json'
    req.headers['Authorization'] = api_token
    req.headers['Original-IP-Address'] = ip_address if ip_address.present?
    req.body = data.to_json
  end
end

#send_translations(data, ip_address = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/translation_engine/connection.rb', line 17

def send_translations(data, ip_address = nil)
  Thread.new do
    begin
      puts "Sending translations in separate thread"
      connection(60).post do |req|
        req.url '/api/v1/translations'
        req.headers['Content-Type'] = 'application/json'
        req.headers['Original-IP-Address'] = ip_address if ip_address.present?
        req.headers['Authorization'] = api_token
        req.body = data.to_json
      end
      puts "Sending translations in separate thread finished"
    rescue StandardError => e
      puts "Sending translations failed: #{e.class}: #{e.message}"
    end
  end
end