Module: Buhos::Helpers

Overview

Misc helpers for Buhos

Instance Method Summary collapse

Instance Method Details

#config_get(id) ⇒ Object

Get an APP config



93
94
95
# File 'lib/buhos/helpers.rb', line 93

def config_get(id)
  Configuration.get(id)
end

#config_set(id, valor) ⇒ Object

Set an APP config



97
98
99
# File 'lib/buhos/helpers.rb', line 97

def config_set(id,valor)
  Configuration.set(id,valor)
end

#dir_baseObject

Base dir por whole app



42
43
44
# File 'lib/buhos/helpers.rb', line 42

def dir_base
  File.expand_path(File.dirname(__FILE__)+"/../..")
end

#dir_filesObject



45
46
47
48
49
50
# File 'lib/buhos/helpers.rb', line 45

def dir_files
  dir_files= $test_mode ? "/spec/usr/files" : "/usr/files"
  dir=File.expand_path(dir_base + "/"+ dir_files)
  FileUtils.mkdir_p(dir) unless File.exist? dir
  dir
end

#ds_to_json(res) ⇒ Object



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

def ds_to_json(res)
  require 'json'
  content_type :json

  res.map {|v|
    {id:v[:id],
     value:v[:text],
     tokens:v[:text].split(/\s+/)
    }
  }.to_json
end

#GET idObject



94
# File 'lib/buhos/helpers.rb', line 94

Configuration.get(id)

#get_title_headObject

Get title header within 80 chars



66
67
68
69
70
71
72
73
# File 'lib/buhos/helpers.rb', line 66

def get_title_head
  if @title.length>80
    @title[0..80]+"..."
  else
    @title
  end

end

#logObject

Provides log access



37
38
39
# File 'lib/buhos/helpers.rb', line 37

def log
  $log
end

#percent_from_proportion(x) ⇒ Object



121
122
123
# File 'lib/buhos/helpers.rb', line 121

def percent_from_proportion(x)
  sprintf("%0.1f%%",x.to_f*100)
end

#process_abstract_text(t) ⇒ Object

Remove innecesary whitespaces



88
89
90
# File 'lib/buhos/helpers.rb', line 88

def process_abstract_text(t)
  t.gsub("\.\n","***").gsub(/\s+/," ").gsub("***",".\n")
end

#protect_encoding(x) ⇒ Object

Force text x to be utf-8 compatible



116
117
118
# File 'lib/buhos/helpers.rb', line 116

def protect_encoding(x)
  x.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end

#pubmed_available?Boolean

Is NCBI API KEY available?

Returns:

  • (Boolean)


61
62
63
# File 'lib/buhos/helpers.rb', line 61

def pubmed_available?
  !ENV['NCBI_API_KEY'].nil?
end

#scopus_available?Boolean

Is SCOPUS API key available?

Returns:

  • (Boolean)


56
57
58
# File 'lib/buhos/helpers.rb', line 56

def scopus_available?
  !ENV['SCOPUS_KEY'].nil?
end

#time_sql(time) ⇒ Object



101
102
103
# File 'lib/buhos/helpers.rb', line 101

def time_sql(time)
  time.strftime("%Y-%m-%d %H:%M:%S")
end

#title(title) ⇒ Object

Set title header for app



52
53
54
# File 'lib/buhos/helpers.rb', line 52

def title(title)
  @title=title
end

#truncate(text, length = 30, truncate_string = "...") ⇒ Object

Truncate a text to given length and add truncate_string at the end



105
106
107
108
109
110
111
112
113
114
# File 'lib/buhos/helpers.rb', line 105

def truncate(text, length = 30, truncate_string = "...")
  if text
    l = length - truncate_string.chars.length
    chars = text.chars
    #$log.info(chars[0..10])
    (chars.length > length ) ?
         (chars[0...l] + truncate_string.chars).join('').to_s :
         text
  end
end