struggle 2.2.0 → 2.2.2
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.
- checksums.yaml +4 -4
- data/lib/struggle/concerns/decimal_extend.rb +7 -0
- data/lib/struggle/ftp_tool.rb +183 -0
- data/lib/struggle/zip_tool.rb +77 -0
- data/lib/struggle.rb +5 -0
- metadata +20 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0de663c31ad25e4b730c910a5bb3a1d75fe00ac5
         | 
| 4 | 
            +
              data.tar.gz: 1e85ed90acdac7bb95e1ed61eef1607999a24a3d
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 4611e93533123a3025394be48eaf1d07e29478f8b97cb92f70e2efc3b4355ae73a38560c1722a3bc9ae0f117c7eabf7ab7d66ead4b790a02da140c3702dba00a
         | 
| 7 | 
            +
              data.tar.gz: d5869ca27cd5feefad64df206681d0078a8d37b7e034e24ec8fccce7631c5577749aa3aecf525d8955a31481345ae369fb83a98123484d92ba39d16e419afefd
         | 
| @@ -0,0 +1,183 @@ | |
| 1 | 
            +
            module Struggle
         | 
| 2 | 
            +
              class FtpTool
         | 
| 3 | 
            +
                #实例化
         | 
| 4 | 
            +
                def initialize(host, user, password, port = 21, mode = true)
         | 
| 5 | 
            +
                  require "net/ftp"
         | 
| 6 | 
            +
                  @current_ftp = Net::FTP.new
         | 
| 7 | 
            +
                  @current_ftp.connect(host, port)
         | 
| 8 | 
            +
                  @current_ftp.login(user, password)
         | 
| 9 | 
            +
                  @current_ftp.passive = mode
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                # 获取指定格式的文件名称列表
         | 
| 13 | 
            +
                # 例如: source = "test/*.txt"
         | 
| 14 | 
            +
                # 返回: [source/file_name.txt]
         | 
| 15 | 
            +
                def fetch_remote_filenames(source)
         | 
| 16 | 
            +
                  return [] if source.blank?
         | 
| 17 | 
            +
                  log_info("source is " + source)
         | 
| 18 | 
            +
                  filenames = @current_ftp.nlst(source)
         | 
| 19 | 
            +
                  filenames
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                # 获取服务器上确切名称的文件
         | 
| 23 | 
            +
                # 例如: get("test/test.txt")
         | 
| 24 | 
            +
                # 文件将被保存到本地 tmp/test/test.txt
         | 
| 25 | 
            +
                def get(origin_file)
         | 
| 26 | 
            +
                  local_file = local_file(origin_file)
         | 
| 27 | 
            +
                  local_file.gsub("\\", "\\\\") #此处注意是window下执行, 在linux下需要注意改成/
         | 
| 28 | 
            +
                  log_info("Ftp Get: #{origin_file} -> #{local_file}")
         | 
| 29 | 
            +
                  begin
         | 
| 30 | 
            +
                    @current_ftp.getbinaryfile(origin_file, local_file+".tmp")
         | 
| 31 | 
            +
                  rescue
         | 
| 32 | 
            +
                    delete_local_file(local_file+".tmp")
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
                  rename_local_file(local_file+".tmp", local_file) if File.exist?(local_file+".tmp")
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
             | 
| 38 | 
            +
                # 上传文件到指定的路径
         | 
| 39 | 
            +
                # 例如: put("tmp\\test\\test.txt", "/test/")
         | 
| 40 | 
            +
                #
         | 
| 41 | 
            +
                def put(origin_file, remote_path)
         | 
| 42 | 
            +
                  return nil if not File.exist?(origin_file)
         | 
| 43 | 
            +
                  _file_name = File.basename(origin_file)
         | 
| 44 | 
            +
                  _root = @current_ftp.getdir
         | 
| 45 | 
            +
                  begin
         | 
| 46 | 
            +
                    @current_ftp.chdir(remote_path)
         | 
| 47 | 
            +
                  rescue
         | 
| 48 | 
            +
                    @current_ftp.mkdir(remote_path)
         | 
| 49 | 
            +
                    @current_ftp.chdir(remote_path)
         | 
| 50 | 
            +
                  end
         | 
| 51 | 
            +
                  log_info("Ftp put: #{origin_file} -> #{remote_path}")
         | 
| 52 | 
            +
                  begin
         | 
| 53 | 
            +
                    @current_ftp.putbinaryfile(origin_file, remote_path + _file_name + ".tmp")
         | 
| 54 | 
            +
                  rescue
         | 
| 55 | 
            +
                    delete(remote_path + _file_name + ".tmp")
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
                  @current_ftp.chdir(_root)
         | 
| 58 | 
            +
                  rename(remote_path + _file_name + ".tmp", remote_path + _file_name)
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                # 关闭ftp
         | 
| 62 | 
            +
                def close
         | 
| 63 | 
            +
                  @current_ftp.close if @current_ftp
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                # 服务器copy文件
         | 
| 67 | 
            +
                def copy(origin_file, file_path)
         | 
| 68 | 
            +
                  local_file = local_file(origin_file)
         | 
| 69 | 
            +
                  _file_name = File.basename(origin_file)
         | 
| 70 | 
            +
                  begin
         | 
| 71 | 
            +
                    #1. 到本地
         | 
| 72 | 
            +
                    log_info("FTP get file to:" + local_file+".tmp")
         | 
| 73 | 
            +
                    @current_ftp.getbinaryfile(origin_file, local_file+".tmp")
         | 
| 74 | 
            +
                    return nil if not File.exist?(local_file+".tmp")
         | 
| 75 | 
            +
                    #2. 到服务器
         | 
| 76 | 
            +
                    log_info("FTP put file to :" + file_path + _file_name + ".tmp")
         | 
| 77 | 
            +
                    @current_ftp.putbinaryfile(local_file+".tmp", file_path + _file_name + ".tmp")
         | 
| 78 | 
            +
                    #3. 改名字
         | 
| 79 | 
            +
                    rename(file_path + _file_name + ".tmp", file_path + _file_name)
         | 
| 80 | 
            +
                    #5. 删除本地
         | 
| 81 | 
            +
                    delete_local_file(local_file + ".tmp")
         | 
| 82 | 
            +
                  rescue => e
         | 
| 83 | 
            +
                    log_info(e)
         | 
| 84 | 
            +
                    #4. 删除服务器上临时文件
         | 
| 85 | 
            +
                    delete(file_path + origin_file + ".tmp")
         | 
| 86 | 
            +
                    #5. 删除本地
         | 
| 87 | 
            +
                    delete_local_file(local_file + ".tmp")
         | 
| 88 | 
            +
                  end
         | 
| 89 | 
            +
                end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                # 服务器上移动文件
         | 
| 92 | 
            +
                def move(origin_file, file_path)
         | 
| 93 | 
            +
                  _file_name = File.basename(origin_file)
         | 
| 94 | 
            +
                  begin
         | 
| 95 | 
            +
                    copy(origin_file, file_path)
         | 
| 96 | 
            +
                    # 删除服务器上源文件
         | 
| 97 | 
            +
                    delete(origin_file)
         | 
| 98 | 
            +
                  rescue => e
         | 
| 99 | 
            +
                    log_info(e)
         | 
| 100 | 
            +
                    # 删除临时文件,如果存在
         | 
| 101 | 
            +
                    delete(file_path + _file_name + ".tmp")
         | 
| 102 | 
            +
                    # 删除服务器上目标文件, 如果存在
         | 
| 103 | 
            +
                    delete(file_path + _file_name)
         | 
| 104 | 
            +
                  end
         | 
| 105 | 
            +
                end
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                # 重命名服务器文件
         | 
| 108 | 
            +
                def rename(origin_file, file)
         | 
| 109 | 
            +
                  if not @current_ftp.list(origin_file).blank?
         | 
| 110 | 
            +
                    log_info("FTP rename #{origin_file} to #{file}")
         | 
| 111 | 
            +
                    @current_ftp.rename(origin_file, file)
         | 
| 112 | 
            +
                  end
         | 
| 113 | 
            +
                end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                # 删除服务器上的文件
         | 
| 116 | 
            +
                def delete(origin_file)
         | 
| 117 | 
            +
                  if not @current_ftp.list(origin_file).blank?
         | 
| 118 | 
            +
                    log_info("FTP delete #{origin_file}")
         | 
| 119 | 
            +
                    @current_ftp.delete(origin_file)
         | 
| 120 | 
            +
                  end
         | 
| 121 | 
            +
                end
         | 
| 122 | 
            +
             | 
| 123 | 
            +
                # ftp 是否关闭
         | 
| 124 | 
            +
                def closed?
         | 
| 125 | 
            +
                  @current_ftp.closed?
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
                class << self
         | 
| 129 | 
            +
                  # 文件编码转换
         | 
| 130 | 
            +
                  def convert(src_file, dest_file, from_encode, to_encode)
         | 
| 131 | 
            +
                    log_info("Convert #{src_file} to #{dest_file}")
         | 
| 132 | 
            +
                    cd = Iconv.new(to_encode, from_encode)
         | 
| 133 | 
            +
                    File.open(dest_file, "w") do |out|
         | 
| 134 | 
            +
                      File.open(src_file) do |in_stream|
         | 
| 135 | 
            +
                        in_stream.each_line do |line|
         | 
| 136 | 
            +
                          begin
         | 
| 137 | 
            +
                            new_line = cd.iconv(line)
         | 
| 138 | 
            +
                            out.write(new_line)
         | 
| 139 | 
            +
                          rescue => e
         | 
| 140 | 
            +
                            log_info "convert line error : #{line}"
         | 
| 141 | 
            +
                            next
         | 
| 142 | 
            +
                          end
         | 
| 143 | 
            +
                        end
         | 
| 144 | 
            +
                      end
         | 
| 145 | 
            +
                    end
         | 
| 146 | 
            +
                    cd.close
         | 
| 147 | 
            +
                    dest_file
         | 
| 148 | 
            +
                  end
         | 
| 149 | 
            +
                end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
                protected
         | 
| 152 | 
            +
             | 
| 153 | 
            +
                #本地路径
         | 
| 154 | 
            +
                def local_file(file)
         | 
| 155 | 
            +
                  local = File.join("tmp/", file)
         | 
| 156 | 
            +
                  FileUtils.makedirs(File.dirname(local))
         | 
| 157 | 
            +
                  local
         | 
| 158 | 
            +
                end
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                # 删除本地文件
         | 
| 161 | 
            +
                def delete_local_file(file)
         | 
| 162 | 
            +
                  if File.exist?(file)
         | 
| 163 | 
            +
                    log_info("delete local file : " + file)
         | 
| 164 | 
            +
                    File.delete(file)
         | 
| 165 | 
            +
                  end
         | 
| 166 | 
            +
                end
         | 
| 167 | 
            +
             | 
| 168 | 
            +
                # 重命名本地文件
         | 
| 169 | 
            +
                def rename_local_file(origin_file, file)
         | 
| 170 | 
            +
                  if File.exist?(origin_file)
         | 
| 171 | 
            +
                    log_info("rename local file : " + origin_file + " to " + file)
         | 
| 172 | 
            +
                    File.rename(origin_file, file)
         | 
| 173 | 
            +
                  end
         | 
| 174 | 
            +
                end
         | 
| 175 | 
            +
             | 
| 176 | 
            +
                private
         | 
| 177 | 
            +
                def log_info(log)
         | 
| 178 | 
            +
                  if Rails.env == "development"
         | 
| 179 | 
            +
                    puts log
         | 
| 180 | 
            +
                  end
         | 
| 181 | 
            +
                end
         | 
| 182 | 
            +
              end
         | 
| 183 | 
            +
            end
         | 
| @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            # This is a simple example which uses rubyzip to
         | 
| 2 | 
            +
            # recursively generate a zip file from the contents of
         | 
| 3 | 
            +
            # a specified directory. The directory itself is not
         | 
| 4 | 
            +
            # included in the archive, rather just its contents.
         | 
| 5 | 
            +
            #
         | 
| 6 | 
            +
            # Usage:
         | 
| 7 | 
            +
            #   directory_to_zip = "/tmp/input"
         | 
| 8 | 
            +
            #   output_file = "/tmp/out.zip"
         | 
| 9 | 
            +
            #   ZipTool.dir(directory_to_zip, output_file)
         | 
| 10 | 
            +
            #   ZipTool.dir("1.txt", output_file)
         | 
| 11 | 
            +
            module Struggle
         | 
| 12 | 
            +
              class ZipTool
         | 
| 13 | 
            +
                class << self
         | 
| 14 | 
            +
                  def dir(input_dir, output_file)
         | 
| 15 | 
            +
                    @input_dir = input_dir
         | 
| 16 | 
            +
                    @output_file = output_file
         | 
| 17 | 
            +
                    entries = Dir.entries(@input_dir) - %w(. ..)
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                    ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |io|
         | 
| 20 | 
            +
                      write_entries entries, '', io
         | 
| 21 | 
            +
                    end
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                  def file(input_files, output_file)
         | 
| 25 | 
            +
                    Zip::File.open(output_file, Zip::File::CREATE) do |zipfile|
         | 
| 26 | 
            +
                      if input_files.class == Array
         | 
| 27 | 
            +
                        input_files.each do |filename|
         | 
| 28 | 
            +
                          idex = filename.rindex("/")
         | 
| 29 | 
            +
                          if idex
         | 
| 30 | 
            +
                            zipfile.add(filename[idex+1, filename.length-idex], filename)
         | 
| 31 | 
            +
                          else
         | 
| 32 | 
            +
                            zipfile.add(filename, filename)
         | 
| 33 | 
            +
                          end
         | 
| 34 | 
            +
                        end
         | 
| 35 | 
            +
                      elsif input_files.class == String
         | 
| 36 | 
            +
                        idex = input_files.rindex("/")
         | 
| 37 | 
            +
                        if idex
         | 
| 38 | 
            +
                          zipfile.add(input_files[idex+1, input_files.length-idex], input_files)
         | 
| 39 | 
            +
                        else
         | 
| 40 | 
            +
                          zipfile.add(input_files, input_files)
         | 
| 41 | 
            +
                        end
         | 
| 42 | 
            +
                      end
         | 
| 43 | 
            +
                    end
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                private
         | 
| 48 | 
            +
                class << self
         | 
| 49 | 
            +
                  # A helper method to make the recursion work.
         | 
| 50 | 
            +
                  def write_entries(entries, path, io)
         | 
| 51 | 
            +
                    entries.each do |e|
         | 
| 52 | 
            +
                      zip_file_path = path == '' ? e : File.join(path, e)
         | 
| 53 | 
            +
                      disk_file_path = File.join(@input_dir, zip_file_path)
         | 
| 54 | 
            +
                      puts "Deflating #{disk_file_path}"
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                      if File.directory? disk_file_path
         | 
| 57 | 
            +
                        recursively_deflate_directory(disk_file_path, io, zip_file_path)
         | 
| 58 | 
            +
                      else
         | 
| 59 | 
            +
                        put_into_archive(disk_file_path, io, zip_file_path)
         | 
| 60 | 
            +
                      end
         | 
| 61 | 
            +
                    end
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                  def recursively_deflate_directory(disk_file_path, io, zip_file_path)
         | 
| 65 | 
            +
                    io.mkdir zip_file_path
         | 
| 66 | 
            +
                    subdir = Dir.entries(disk_file_path) - %w(. ..)
         | 
| 67 | 
            +
                    write_entries subdir, zip_file_path, io
         | 
| 68 | 
            +
                  end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                  def put_into_archive(disk_file_path, io, zip_file_path)
         | 
| 71 | 
            +
                    io.get_output_stream(zip_file_path) do |f|
         | 
| 72 | 
            +
                      f.write(File.open(disk_file_path, 'rb').read)
         | 
| 73 | 
            +
                    end
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
            end
         | 
    
        data/lib/struggle.rb
    CHANGED
    
    | @@ -5,6 +5,7 @@ begin | |
| 5 5 | 
             
            rescue LoadError
         | 
| 6 6 | 
             
              #do nothing
         | 
| 7 7 | 
             
            end
         | 
| 8 | 
            +
            require 'zip'
         | 
| 8 9 | 
             
            require 'struggle/tfile'
         | 
| 9 10 | 
             
            require 'struggle/http'
         | 
| 10 11 | 
             
            require 'struggle/logistic'
         | 
| @@ -15,6 +16,10 @@ require 'struggle/code' | |
| 15 16 | 
             
            require 'struggle/aes'
         | 
| 16 17 | 
             
            require 'struggle/concerns/string_extend'
         | 
| 17 18 | 
             
            require 'struggle/concerns/int_extend'
         | 
| 19 | 
            +
            require 'struggle/concerns/decimal_extend'
         | 
| 20 | 
            +
            require "struggle/ftp_tool"
         | 
| 21 | 
            +
            require "struggle/zip_tool"
         | 
| 18 22 | 
             
            require 'pay/jd_gateway'
         | 
| 19 23 | 
             
            String.send :include, Struggle::StringExtend
         | 
| 20 24 | 
             
            Integer.send :include, Struggle::IntExtend
         | 
| 25 | 
            +
            Numeric.send :include, Struggle::DecimalExtend
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: struggle
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 2.2. | 
| 4 | 
            +
              version: 2.2.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - lean
         | 
| @@ -38,7 +38,21 @@ dependencies: | |
| 38 38 | 
             
                - - ">="
         | 
| 39 39 | 
             
                  - !ruby/object:Gem::Version
         | 
| 40 40 | 
             
                    version: '0'
         | 
| 41 | 
            -
             | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: rubyzip
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - ">="
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '0'
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: '0'
         | 
| 55 | 
            +
            description: code,tfile,pager,http,logistic,pay,tmagick,class_extend,aes,ftp_tool,zip_tool;
         | 
| 42 56 | 
             
            email: 54850915@qq.com
         | 
| 43 57 | 
             
            executables: []
         | 
| 44 58 | 
             
            extensions: []
         | 
| @@ -50,15 +64,18 @@ files: | |
| 50 64 | 
             
            - lib/struggle.rb
         | 
| 51 65 | 
             
            - lib/struggle/aes.rb
         | 
| 52 66 | 
             
            - lib/struggle/code.rb
         | 
| 67 | 
            +
            - lib/struggle/concerns/decimal_extend.rb
         | 
| 53 68 | 
             
            - lib/struggle/concerns/int_extend.rb
         | 
| 54 69 | 
             
            - lib/struggle/concerns/string_extend.rb
         | 
| 55 70 | 
             
            - lib/struggle/font/font.ttf
         | 
| 71 | 
            +
            - lib/struggle/ftp_tool.rb
         | 
| 56 72 | 
             
            - lib/struggle/http.rb
         | 
| 57 73 | 
             
            - lib/struggle/logistic.rb
         | 
| 58 74 | 
             
            - lib/struggle/pager.rb
         | 
| 59 75 | 
             
            - lib/struggle/sms.rb
         | 
| 60 76 | 
             
            - lib/struggle/tfile.rb
         | 
| 61 77 | 
             
            - lib/struggle/tmagick.rb
         | 
| 78 | 
            +
            - lib/struggle/zip_tool.rb
         | 
| 62 79 | 
             
            homepage: http://rubygems.org/gems/struggle
         | 
| 63 80 | 
             
            licenses:
         | 
| 64 81 | 
             
            - struggle
         | 
| @@ -79,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 79 96 | 
             
                  version: '0'
         | 
| 80 97 | 
             
            requirements: []
         | 
| 81 98 | 
             
            rubyforge_project: 
         | 
| 82 | 
            -
            rubygems_version: 2. | 
| 99 | 
            +
            rubygems_version: 2.6.10
         | 
| 83 100 | 
             
            signing_key: 
         | 
| 84 101 | 
             
            specification_version: 4
         | 
| 85 102 | 
             
            summary: struggle!
         |