Module: UnpSmart::UnpSmartMethod
- Included in:
- UnpSmart
- Defined in:
- lib/unp_smart/unp_smart_method.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#config ⇒ Object
Returns the value of attribute config.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #_invoke_api(http_method, api, params = {}) ⇒ Object
- #get(api, params = {}) ⇒ Object
- #load_config ⇒ Object
- #post(api, params = {}) ⇒ Object
- #request_params(params = {}) ⇒ Object
- #sign_params(params, secret_key) ⇒ Object
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
5 6 7 |
# File 'lib/unp_smart/unp_smart_method.rb', line 5 def api @api end |
#config ⇒ Object
Returns the value of attribute config.
5 6 7 |
# File 'lib/unp_smart/unp_smart_method.rb', line 5 def config @config end |
#debug ⇒ Object
Returns the value of attribute debug.
5 6 7 |
# File 'lib/unp_smart/unp_smart_method.rb', line 5 def debug @debug end |
#http_method ⇒ Object
Returns the value of attribute http_method.
5 6 7 |
# File 'lib/unp_smart/unp_smart_method.rb', line 5 def http_method @http_method end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/unp_smart/unp_smart_method.rb', line 5 def params @params end |
#verbose ⇒ Object
Returns the value of attribute verbose.
5 6 7 |
# File 'lib/unp_smart/unp_smart_method.rb', line 5 def verbose @verbose end |
Class Method Details
.extended(base) ⇒ Object
7 8 9 |
# File 'lib/unp_smart/unp_smart_method.rb', line 7 def self.extended(base) base.send :load_config end |
Instance Method Details
#_invoke_api(http_method, api, params = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/unp_smart/unp_smart_method.rb', line 19 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 |
#get(api, params = {}) ⇒ Object
11 12 13 |
# File 'lib/unp_smart/unp_smart_method.rb', line 11 def get api,params={} _invoke_api :get,api,params end |
#load_config ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/unp_smart/unp_smart_method.rb', line 93 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 |
#post(api, params = {}) ⇒ Object
15 16 17 |
# File 'lib/unp_smart/unp_smart_method.rb', line 15 def post api,params={} _invoke_api :post,api,params end |
#request_params(params = {}) ⇒ Object
89 90 91 |
# File 'lib/unp_smart/unp_smart_method.rb', line 89 def request_params params={} params.except(:action, :controller,:api_id,:id,:api_list,:format,:sign,:user,:source,:timestamp,:version,:secret_key) end |
#sign_params(params, secret_key) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/unp_smart/unp_smart_method.rb', line 81 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 |