#encoding: utf-8 module UnpSmart module UnpSmartMethod attr_accessor :api,:params,:http_method,:config,:debug,:verbose def self.extended(base) base.send :load_config end def get api,params={} _invoke_api :get,api,params end def post api,params={} _invoke_api :post,api,params end def _invoke_api http_method,api,params={} log "request api:#{api},params:#{params}" raise 'api is not allowed blank!' if api.blank? @api = api @params = params @http_method = http_method @debug = get_variable :debug @config = {} if @config.nil? @verbose = get_variable :verbose @api = "/#{@api}" unless @api.start_with?("/") account = get_variable :account log "account:", account if @verbose raise 'account is not allowed blank!' if account.blank? secret_key = get_variable :secret_key log "secret_key:",secret_key if @verbose raise 'invalid secret_key!' if secret_key.blank? extracted_params = request_params(params) extracted_params[:account] = account extracted_params[:source] = 'ruby-sdk' extracted_params[:timestamp] =Time.now.to_f extracted_params[:version] = '1.0' extracted_params[:sign] = sign_params extracted_params,secret_key request_url = "#{get_variable :server}#{@api}?#{extracted_params.map{|key,value| "#{key}=#{CGI.escape value.nil? ? "" : value.to_s}"}.join("&")}" log "request:",request_url if @debug return_response = JSON.parse RestClient::Request.execute(url: request_url, method: http_method, verify_ssl: false, timeout: get_variable(:timeout)) log "response:",return_response if @debug if block_given? log "invoke block" if @debug yield return_response else if get_variable(:raw_response) log "return raw response" if @verbose return_response else log "parse result" if return_response['resCode'] == '0000' return true,return_response else return false,"##{return_response['resCode']}:#{return_response['resMsg']}" end end end # rescue => e # log "!!!invoke http service error",e.inspect # return false,e.message end def sign_params params,secret_key sign_str = "#{params.sort_by { |k, v| k.to_s }.flatten.join}#{secret_key}" log "sign str",sign_str if @verbose Digest::MD5.hexdigest(sign_str).upcase.tap do |str| log "sign",str if @verbose end end def request_params params={} params.except(:action, :controller,:api_id,:id,:api_list,:format,:sign,:user,:source,:timestamp,:version,:secret_key) end def load_config log "loading config" path = "#{app_root}/config/unp_smart.yml" unless File.exist?(path) path = "#{File.expand_path('~')}/.unp_smart.yml" log "try use home config #{path}" if @verbose unless File.exist?(path) log "can't load config,will use env or params config" return end end log "config file path",path @config ||= YAML.load_file(path)[current_env] log "config:#{@config}" if @verbose # rescue => e # log "load config error:#{e.inspect}" end private def get_variable key get_variable_from_params(key).presence || get_variable_from_env(key).presence || get_variable_from_config(key) end def get_variable_from_params key @params[key.to_sym] end def get_variable_from_env key ENV[key.to_s]||ENV["#{key.to_s}_#{current_env}"] end def get_variable_from_config key @config[key.to_s] if @config.present? end def log *msg file, line, _ = caller.first.split(":") messages = "#{file.split("/").last}:#{line}:#{msg.join(" ")}" @logger ||= rails_app? ? Logger.new("#{app_root}/log/api.log") : Logger.new(STDOUT) @logger.debug messages end def app_root root = Rails.root if rails_app? root.present? ? root : '.' rescue => e log "access rails root got error:#{e.message}" '.' end def current_env rails_app? ? Rails.env : 'development' end def rails_app? defined?(Rails) end end end