Module: Glib::Json::NewDynamicText

Defined in:
app/controllers/concerns/glib/json/new_dynamic_text.rb

Defined Under Namespace

Classes: TextSpec

Instance Method Summary collapse

Instance Method Details

#__json_traversal_register_dynamic_textObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/concerns/glib/json/new_dynamic_text.rb', line 2

def __json_traversal_register_dynamic_text
  @__specs = {}
  @__specs_db = {}

  __json_traversal_on_traverse do |view|
    extract_spec(view, 'text')
  end

  __json_traversal_on_complete do |view|
    if @__specs.keys.size > 0
      # translated_texts = retrieve_remote_texts(@__specs.keys)
      translated_texts = retrieve_local_texts(@__specs.keys).merge(retrieve_remote_texts(@__specs.keys))
      translated_texts.each do |key, value|
        @__specs[key].each do |spec|
          spec.substitute_with(value)
        end

        if @__specs_db[key] && @__specs_db[key].images.attached?
          @__specs[key].each do |spec|
            spec.substitute_image_with(@__specs_db[key].images)
          end
        end
      end
    end
  end
end

#extract_spec(view, prop) ⇒ Object



98
99
100
101
102
103
104
105
# File 'app/controllers/concerns/glib/json/new_dynamic_text.rb', line 98

def extract_spec(view, prop)
  if (spec = view[prop])
    if spec.is_a?(Hash) && (key = spec['dt_key'])
      @__specs[key] ||= []
      @__specs[key] << TextSpec.new(view, prop, spec)
    end
  end
end

#retrieve_example_texts(keys) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/concerns/glib/json/new_dynamic_text.rb', line 29

def retrieve_example_texts(keys)
  examples_translations = {
    'home.json_ui_garage.hello' => 'Hello {{name}}',
    'home.json_ui_garage.greeting' => 'Good day!',
    'home.json_ui_garage.greeting_with_image' => 'Good day! (with image)',
  }

  translated_texts = {}
  keys.each do |key|
    translated_texts[key] = examples_translations[key]
  end
  translated_texts
end

#retrieve_local_texts(keys) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/concerns/glib/json/new_dynamic_text.rb', line 60

def retrieve_local_texts(keys)
  translated_texts = {}
  db_keys = []

  redis = Glib::DynamicText::Config.redis
  contents = redis.pipelined do
    keys.each do |key|
      args = @__specs[key].first.args
      options = {
        scope: args.fetch(:scope, 'itinerarybuilder'),
        lang: args.fetch(:lang, 'en')
      }

      scope_key = "#{options[:scope]}.#{options[:lang]}.#{key}"
      redis.get(scope_key)
    end
  end

  keys.each_with_index do |key, index|
    if content = contents[index]
      translated_texts[key] = content
      db_keys << key if content.match(/\{\{image(\d)\}\}/)
    else
      db_keys << key
    end
  end

  if db_keys.size > 0
    texts = Glib::Text.where(key: db_keys)
    texts.each do |text|
      translated_texts[text.key] = text.content
      @__specs_db[text.key] = text
    end
  end

  translated_texts
end

#retrieve_remote_texts(keys) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/concerns/glib/json/new_dynamic_text.rb', line 43

def retrieve_remote_texts(keys)
  retries = 0

  begin
    response = RestClient.get(ENV['DTR_URL'], { params: { keys: keys } })
  rescue Errno::ECONNREFUSED, RestClient::InternalServerError, RestClient::Exceptions::OpenTimeout, RestClient::Exceptions::ReadTimeout
    if (retries += 1) <= 3
      puts "Timeout, retrying..."
      retry
    else
      raise
    end
  end

  JSON.parse(response)
end