Class: Eagleplatform::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/eagleplatform/translation.rb

Overview

Translations

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(id) ⇒ String

Delete translation from Eagleplatform

Examples:

Eagleplatform::Translation.delete(1234)

Returns:

  • (String)

    ‘Translation id: #id is deleted’ if translation deleted successfully

Raises:

  • (ArgumentError)


93
94
95
96
97
98
# File 'lib/eagleplatform/translation.rb', line 93

def self.delete(id)
  raise ArgumentError, 'id must be numeric' unless id.is_a? Numeric
  api_method = {method: Methods::TRANSLATION_DELETE[:method], 
                path: Methods::TRANSLATION_DELETE[:path].gsub(':id',id.to_s)}        
  Eagleplatform.call_api(api_method) == "ok" ? "Translation id: '#{id}' is switched off" : (raise "Can't switch off translation")
end

.find(id) ⇒ Eagleplaform::Translation

Find Translation by ID

Examples:

Eagleplatform::Translation.find(45632)

Parameters:

  • id (Numeric)

    ID of translation

Returns:

  • (Eagleplaform::Translation)

    if translation present

Raises:

  • (ArgumentError)

    id must be numeric



55
56
57
58
59
60
61
62
63
# File 'lib/eagleplatform/translation.rb', line 55

def self.find(id)
   raise ArgumentError, "id must be numeric" unless id.is_a? Numeric
   api_method = {method: Methods::TRANSLATION_GET_INFO[:method], 
                 path: Methods::TRANSLATION_GET_INFO[:path].gsub(':id',id.to_s)}

   result = Eagleplatform.call_api(api_method).first[1].to_options      
   trans = self.new
   trans.each_pair { |k,v| trans[k] = result[k] }
end

.list(per_page = 50, page = 1) ⇒ Array

Get list of translations

Examples:

translations = Eagleplatform::Translation.list

#Get translations page=2, per_page=20
translations = Eagleplatform::Translation.list(20,2) 

Parameters:

  • per_page (Numeric) (defaults to: 50)

    translations number per page

  • page (Numeric) (defaults to: 1)

    translations page

Returns:

  • (Array)

    Array of Translations objects



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/eagleplatform/translation.rb', line 26

def self.list(per_page = 50, page = 1)
  params = {
    per_page: per_page.to_s,
    page: page.to_s
  }
  result = Eagleplatform.call_api(Methods::TRANSLATIONS_GET_LIST, params).to_options
  translations = []
  result[:translations].each do |translation|
    t = self.new
    t.each_pair { |k,v| t[k] = translation[k.to_s]}
    translations.push(t)
  end
  if result[:total_pages] > 1
    puts "Translations per_page: #{per_page}"
    puts "Current page: #{result[:current_page]}"
    puts "Total pages: #{result[:total_pages]}"
    puts "Total entries: #{result[:total_entries]}"        
  end
  translations
end

.update(args = {}) ⇒ Hash

Update translation on Eagleplatform

Examples:

Eagleplatform::Translation.update(id: 1234, name: 'Hello world', description: 'Heyy')

Parameters:

  • translation (Hash)

    Hash of translation fields

Returns:

  • (Hash)

    Updated translation

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/eagleplatform/translation.rb', line 75

def self.update(args = {})
  raise ArgumentError, 'ID is blank' if args[:id].blank?  
  raise ArgumentError, 'id must be numeric' unless args[:id].is_a? Numeric
  params = {
    translation: args
  }
  
  api_method = {method: Methods::TRANSLATION_UPDATE[:method], 
                path: Methods::TRANSLATION_UPDATE[:path].gsub(':id',args[:id].to_s)}
  result = Eagleplatform.call_api(api_method, params).first[1].to_options
end

Instance Method Details

#deleteString

Switch off translation from Eagleplaform

Examples:

t = Eagleplatform::translation.find(1234)
t.delete

Returns:

  • (String)

    ‘ok’ if translation switched off successfully



131
132
133
134
135
# File 'lib/eagleplatform/translation.rb', line 131

def delete
  api_method = {method: Methods::TRANSLATION_DELETE[:method], 
                path: Methods::TRANSLATION_DELETE[:path].gsub(':id',id.to_s)}        
  Eagleplatform.call_api(api_method) == "ok" ? "Translation id: '#{self.id}', name:#{self.name} is switched off" : (raise "Can't switch off translation")
end

#statistics(args = {}) ⇒ Array

Get current translation statistics

Date format is - 'dd.mm.yyyy'

Examples:

t = Eagleplatform::Translation.find(12345)
t.statistics(date_from: '1.5.2012', date_to: '25.5.2012')

Parameters:

  • args (Hash) (defaults to: {})

    the statistics options

Options Hash (args):

  • :date_from (String) — default: 'yesterday'

    yesterday date

  • :date_to (String) — default: 'today'

    today date

Returns:

  • (Array)

    return translation statistics

Raises:

  • (ArgumentError)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/eagleplatform/translation.rb', line 147

def statistics(args = {})
  raise "self.id is blank" if self.id.blank?
  params = {
    date_from: args[:date_from] || (Time.now - 1.day).strftime('%d.%m.%Y'),
    date_to: args[:date_to] || Time.now.strftime('%d.%m.%Y')
  }

  raise ArgumentError, "Wrong 'date_from' format. Must be 'dd.mm.yyyy'" unless DATE_FORMAT =~ params[:date_from] 
  raise ArgumentError, "Wrong 'date_to' format. Must be 'dd.mm.yyyy'" unless DATE_FORMAT =~ params[:date_from]
  raise ArgumentError, "date_from: #{params[:date_from]} > date_to: #{params[:date_from]}" if params[:date_from].to_date > params[:date_to].to_date

  api_method = {method: Methods::TRANSLATION_GET_STATISTICS[:method], 
                path: Methods::TRANSLATION_GET_STATISTICS[:path].gsub(':id',id.to_s)}
  result = Eagleplatform.call_api(api_method, params).first[1]
end

#updateEagleplatform::Translation

Update translation on Eagleplatform

Examples:

t = Eagleplatform::Translation.find(1234)
t.description = 'Mega stream'
t.update

Returns:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/eagleplatform/translation.rb', line 108

def update
   api_method = {method: Methods::TRANSLATION_UPDATE[:method], 
                 path: Methods::TRANSLATION_UPDATE[:path].gsub(':id',id.to_s)}
   params = {}
   translation = self.to_hash
   translation.delete(:user_id)
   translation.delete(:account_id)
   translation.delete(:status)
   translation.delete(:created_at)
   translation.delete(:updated_at)
   translation.delete(:starts_at)
   
   params[:translation] = translation
   result = Eagleplatform.call_api(api_method, params).first[1].to_options
   translation.diff(result).keys.include?(:updated_at) ? self : 'Something wrong'  
end