ruby 示例脚本em ruby

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 示例脚本em ruby相关的知识,希望对你有一定的参考价值。

require 'faraday'
require 'parallel'

class Params
  def initialize args
    @args = args
  end

  def opt? key
    @args.each { |arg| return true if arg.include?("-#{key}") }
    false
  end

  def include? key
    @args.each { |arg| return true if arg.include?("--#{key}") }
    false
  end

  def get key
    @args.each { |arg| return arg.split('=').last if arg.include?("--#{key}") }
    nil
  end

  def get_s key
    get(key)
  end

  def get_a key, separator = ','
    get(key).to_s.split(separator)
  end

  def get_i key
    get(key).to_i
  end
 
  def get_f key
    get(key).to_f
  end

  def get_sym key
    value = get(key)
    value.to_sym if value
  end
end 

params = Params.new(ARGV)

class Skyhub
  attr_accessor :email, :token

  def initialize email, token
    @email = email
    @token = token
  end

  def connection
    @connection ||= get_connection
  end

  def get_connection
    Faraday.new('https://in.skyhub.com.br') do |faraday|
      faraday.request  :url_encoded             # form-encode POST params
      #faraday.response :logger                  # log requests to STDOUT
      faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      faraday.headers['X-User-Email'] = @email  #'sephora@skyhub.com.br'
      faraday.headers['X-User-Token'] = @token #'9zqEvdqRjRQVJU-JjjPL'
      faraday.headers['Content-Type'] = 'application/json'
      faraday.headers['Accept'] = 'application/json'
    end
  end
end

class Product
  def initialize skyhub
    @skyhub = skyhub
  end

  def delete code
    @skyhub.connection.delete("/products/#{code}")
  end
end

product = Product.new(Skyhub.new(params.get_s(:email), params.get_s(:token)))

codes = []

sleep_time = [1,2,3,4,5]

Parallel.each(codes, in_threads: 20) do |code|
  response = product.delete(code)
  sleep(sleep_time[(rand*5).to_i])
  puts "sku: #{code};status: #{response.status};body: #{response.body}"
end

以上是关于ruby 示例脚本em ruby的主要内容,如果未能解决你的问题,请参考以下文章

WebSocket 与 Ruby 和 EM::WebSocket::Server 握手

ruby 中的安全 Websocket 客户端

ruby em-http-request在重定向上捕获和传递cookie

redis集群环境搭建的错误

ruby Ruby脚本,看看是否用openssl编译了ruby

Centos7:Redis集群搭建