rubic-rb 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db904df42fe403d01ef04fa7a020224a0ad7c313
4
+ data.tar.gz: 506372dcee6dffb831285455799d5255223f2409
5
+ SHA512:
6
+ metadata.gz: 5b97aba8f32e4cfb57967380b5001e84e429925ea2ea1a37ad1f0e7082372442143403268a0011377f2868190e45b2f713bb77edcd3e36edd9d638a445c35906
7
+ data.tar.gz: ceb74f1e7044e6f847d99a3aeb03a21dd5f8cd2f3875793c728fc0e75e00d9b4167b18b45a35e1590b1f5e73a0fb506a1360decc1ef3d759030d62a4bf827e6b
@@ -0,0 +1,21 @@
1
+ require 'bundler/setup'
2
+ require 'active_record'
3
+ require 'rubygems'
4
+
5
+ module Rubik
6
+ module Utils
7
+ class Active
8
+ def self.load(env)
9
+ project_root = File.expand_path('../../../', __FILE__)
10
+ Dir.glob(project_root + '/models/*.rb').each{|f| require f}
11
+
12
+ connection_details = YAML.load File.read(File.join(File.expand_path('../../../config', __FILE__), 'database.yml'))
13
+ ActiveRecord::Base.establish_connection(connection_details[env])
14
+
15
+ if __FILE__ == $0
16
+ Rubik::Logging.logger.info { "Count of Pages: #{Page.count}" }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ require 'net/ftp'
2
+ require 'net/http'
3
+
4
+ module Rubik
5
+ module Utils
6
+ class Downloader
7
+
8
+ def self.download(url, dest, user=nil, pwd=nil)
9
+ uri = URI.parse(URI.encode(url))
10
+ host = uri.host
11
+ path = File.dirname(uri.path)
12
+ file = File.basename(uri.path)
13
+ return ftp_download(host, path, file, dest, user, pwd) if url.start_with?('ftp')
14
+ return http_download(host, path, file, dest, user, pwd) if url.start_with?('http')
15
+ raise 'Invalid url'
16
+ end
17
+
18
+ def ftp_download(domain, remotedir, file, dest, user=nil, pwd=nil)
19
+ ftp = Net::FTP::new(domain)
20
+ ftp.passive = true
21
+ ftp.login(user, pwd) if user && pwd
22
+ ftp.chdir(remotedir)
23
+ ftp.getbinaryfile(file, dest, 1024)
24
+ ftp.close
25
+ return dest
26
+ end
27
+
28
+ def http_download(domain, remotedir, file, dest, user=nil, pwd=nil)
29
+ Net::HTTP.start(domain) do |http|
30
+ resp = http.get("#{remotedir}/#{file}")
31
+ open(dest, "wb") do |file|
32
+ file.write(resp.body)
33
+ end
34
+ end
35
+ return dest
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubik/logging'
2
+ require 'yaml'
3
+
4
+ module Rubik
5
+ module Utils
6
+ class EnvLoader
7
+ def self.load(env = 'local', path = './')
8
+ env_file = File.join(path, "#{env}_env.yml")
9
+ YAML.load(File.open(env_file)).each do |key, value|
10
+ ENV["#{key.to_s}"] = value
11
+ end if File.exists?(env_file)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Rubik
2
+ module Utils
3
+ class File
4
+ def self.info(file)
5
+
6
+ results = {}
7
+ encoding = `nkf -g #{file}`.strip
8
+ encoding = 'SJIS' if encoding == 'Shift_JIS'
9
+ results[:encoding] = encoding
10
+
11
+ head = `head -n 1 #{file}`
12
+ col_sep = (head.count("\t") >= head.count(",") ? "\t" : "," )
13
+ col_sep = (head.count(col_sep) >= head.count("|") ? col_sep : "|" )
14
+ results[:col_sep] = col_sep
15
+
16
+ file_format = 'TSV'
17
+ file_format = 'CSV' if col_sep == ","
18
+ file_format = 'PSV' if col_sep == "|"
19
+ results[:file_format] = file_format
20
+
21
+ quote_char = "\x00"
22
+ if `grep -m 1 '#{col_sep}"' #{file} | wc -l`.strip.to_i > 0
23
+ quote_char = "\""
24
+ end
25
+ if quote_char == "\x00" && `grep -m 1 "#{col_sep}'" #{file} | wc -l`.strip.to_i > 0
26
+ quote_char = "'"
27
+ end
28
+ if quote_char == "\x00" && `grep -m 1 '#{col_sep}|' #{file} | wc -l`.strip.to_i > 0
29
+ quote_char = "|"
30
+ end
31
+ results[:quote_char] = quote_char
32
+ return results
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ require 'time'
2
+ require 'logger'
3
+
4
+ module Rubik
5
+ module Utils
6
+ module Logging
7
+ class Pretty < Logger::Formatter
8
+ # Provide a call() method that returns the formatted message.
9
+ def call(severity, time, program_name, message)
10
+ "#{time.utc.iso8601} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
11
+ end
12
+
13
+ def context
14
+ c = Thread.current[:rubik_context]
15
+ c ? " #{c}" : ''
16
+ end
17
+ end
18
+
19
+ def self.with_context(msg)
20
+ Thread.current[:rubik_context] = msg
21
+ yield
22
+ ensure
23
+ Thread.current[:rubik_context] = nil
24
+ end
25
+
26
+ def self.initialize_logger(log_target = STDOUT)
27
+ @logger = Logger.new(log_target)
28
+ @logger.level = Logger::INFO
29
+ @logger.formatter = Pretty.new
30
+ @logger
31
+ end
32
+
33
+ def self.logger
34
+ @logger || initialize_logger
35
+ end
36
+
37
+ def self.logger=(log)
38
+ @logger = (log ? log : Logger.new('/dev/null'))
39
+ end
40
+
41
+ def logger
42
+ Rubik::Logging.logger
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,56 @@
1
+ module Rubik
2
+ module Rule
3
+ def self.validate_rule(row, rule)
4
+ if rule['type'] == 'string'
5
+ return false unless row[rule['field'].to_sym].instance_of? String
6
+ case rule['is']
7
+ when '=='
8
+ return row[rule['field'].to_sym] == rule['to']
9
+ when '!='
10
+ return row[rule['field'].to_sym] != rule['to']
11
+ when 'startsWith'
12
+ return row[rule['field'].to_sym].start_with? rule['to']
13
+ when '!startsWith'
14
+ return !(row[rule['field'].to_sym].start_with? rule['to'])
15
+ when 'endsWith'
16
+ return row[rule['field'].to_sym].end_with? rule['to']
17
+ when '!endsWith'
18
+ return !(row[rule['field'].to_sym].end_with? rule['to'])
19
+ when 'include'
20
+ return row[rule['field'].to_sym].include? rule['to']
21
+ when '!include'
22
+ return !(row[rule['field'].to_sym].include? rule['to'])
23
+ end
24
+ end
25
+ if rule['type'] == 'number'
26
+ return false unless row[rule['field'].to_sym].instance_of? Fixnum
27
+ return eval("#{row[rule['field'].to_sym]} #{rule['is']} #{rule['to']}")
28
+ end
29
+ return true
30
+ end
31
+
32
+ def self.validate_row(row, rules)
33
+ rules.each do |rule|
34
+ return false unless Rubik::Rule::validate_rule(row, rule)
35
+ end
36
+ return true
37
+ end
38
+
39
+ def self.augment_row(row, field_rules)
40
+ field_rules.keys.each do |name|
41
+ row[name] = field_rules[name]['default']
42
+ field_rules[name]['when'].each do |cond|
43
+ row[name] = cond['then'] if Rubik::Rule::validate_rule(row, cond)
44
+ end
45
+ end
46
+ row.keys.each do |key_1|
47
+ if (row[key_1].instance_of?(String) and row[key_1].include?('{{') and row[key_1].include?('}}'))
48
+ row.keys.each do |key_2|
49
+ row[key_1] = row[key_1].gsub("{{#{key_2.to_s}}}", row[key_2].to_s)
50
+ end
51
+ end
52
+ end
53
+ return row
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ module Rubik
2
+ module Utils
3
+ class Storage
4
+ def self.download_from_s3(local_file, key, bucket)
5
+ service = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
6
+ object = service.buckets[bucket].objects[key]
7
+ File.open(local_file, 'wb') do |file|
8
+ object.read do |chunk|
9
+ file.write(chunk)
10
+ end
11
+ end
12
+ return local_file
13
+ end
14
+
15
+ def self.upload_to_s3(dest_file, key, bucket)
16
+ service = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
17
+ service.buckets[bucket].objects[key].write(:file => dest_file)
18
+ return key
19
+ end
20
+
21
+ def self.delete_from_s3(key, bucket)
22
+ service = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
23
+ service.buckets[bucket].objects[key].delete
24
+ return key
25
+ end
26
+
27
+ def self.list_from_s3(bucket)
28
+ service = AWS::S3.new(:access_key_id => ENV['AWS_ACCESS_KEY_ID'], :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
29
+ service.buckets[bucket].objects.map(&:key)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,38 @@
1
+ require 'net/ftp'
2
+ require 'net/sftp'
3
+ require 'net/scp'
4
+
5
+ module Rubik
6
+ module Utils
7
+ class Uploader
8
+
9
+ def self.upload(file, settings)
10
+ host = URI.parse(settings.url).host
11
+ dir_name = File.dirname(URI.parse(settings.url).path)
12
+ return ftp_upload(file, host, dir_name, settings.username, settings.password) if settings.url.start_with?('ftp')
13
+ return scp_upload(file, host, dir_name, settings.username, settings.password) if settings.url.start_with?('scp')
14
+ return sftp_upload(file, host, dir_name, settings.username, settings.password) if settings.url.start_with?('sftp')
15
+ raise 'Invalid url'
16
+ end
17
+
18
+ private
19
+
20
+ def ftp_upload(file, host, dir_name, user=nil, pwd=nil)
21
+ Net::FTP.open(host, user, pwd) do |ftp|
22
+ ftp.chdir(dir_name)
23
+ ftp.putbinaryfile(file)
24
+ end
25
+ end
26
+
27
+ def scp_upload(file, host, dir_name, user=nil, pwd=nil)
28
+ Net::SCP.upload!(host, user, file, dir_name, :password => pwd)
29
+ end
30
+
31
+ def sftp_upload(file, host, dir_name, user=nil, pwd=nil)
32
+ Net::SFTP.start(host, user, :password => pwd) do |sftp|
33
+ sftp.upload!(file, dir_name)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubic-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Quentin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - rubic-gem/active.rb
20
+ - rubic-gem/downloader.rb
21
+ - rubic-gem/env_loader.rb
22
+ - rubic-gem/file.rb
23
+ - rubic-gem/logging.rb
24
+ - rubic-gem/rule-2.rb
25
+ - rubic-gem/storage.rb
26
+ - rubic-gem/uploader.rb
27
+ homepage:
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Rubic Gem
50
+ test_files: []