class Qihusem::Base

Public Class Methods

new(attrs={}) click to toggle source
# File lib/qihusem/base.rb, line 9
def initialize(attrs={})
  @username = attrs[:username]
  @apiSecret = attrs[:apiSecret]
  @apiKey = attrs[:apiKey]
  @password = attrs[:password]
  @debug = attrs[:debug]

  ps = Digest::MD5.hexdigest(@password)

  key = @apiSecret[0..15]
  iv = @apiSecret[16..-1]

  aes = OpenSSL::Cipher::Cipher.new("AES-128-CBC")
  aes.encrypt
  aes.padding = 0
  aes.key = key
  aes.iv = iv
  encrypted = aes.update(ps) << aes.final
  @password = encrypted.unpack('H*').join

  refresh_token
end

Public Instance Methods

call(service,method,body=nil,version='2.0',base_uri='https://api.e.360.cn') click to toggle source
# File lib/qihusem/base.rb, line 32
def call(service,method,body=nil,version='2.0',base_uri='https://api.e.360.cn')
  response = {}

  headers = {
      'serveToken' => Time.now.to_i.to_s,
      'apiKey' => @apiKey,
      'content-type' => 'application/x-www-form-urlencoded'
  }

  headers['accessToken'] = get_token unless service.to_s == 'account' && method.to_s == 'clientLogin'

  5.times do |i|
    response = HTTParty.post("#{base_uri}/#{version}/#{service}/#{method}?format=json",
                             :body => body,
                             :headers => headers,
                             :verify => false
    )

    pp response if @debug

    break # if response['header']['desc'] == 'success'

    sleep(20*(i+1))
  end

  response
end
get_token() click to toggle source
# File lib/qihusem/base.rb, line 60
def get_token
  refresh_token if ((Time.now - @access_token_updated_at) > 3600)
  @access_token
end
refresh_token() click to toggle source
# File lib/qihusem/base.rb, line 65
def refresh_token
  # @access_token = call('account','clientLogin',{:username => @username,:passwd => @password})['accessToken']
  @access_token = HTTParty.post("https://api.e.360.cn/account/clientLogin?format=json",
                    :body => {:username => @username,:passwd => @password},
                    :headers => {
                        'serveToken' => Time.now.to_i.to_s,
                        'apiKey' => @apiKey,
                        'content-type' => 'application/x-www-form-urlencoded'
                    },
                    :verify => false
      )['accessToken']
  @access_token_updated_at = Time.now
end