Class: MtopHelper::MtopV4

Inherits:
Object
  • Object
show all
Includes:
HttpHelper, JsonHelper
Defined in:
lib/common/mtop_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonHelper

#parse_json, #to_json

Methods included from HttpHelper

#clear_cookie, #delete, #do_encode, #dont_encode, #download, #get, #post, #print_body, #print_head, #put

Constructor Details

#initialize(opts = {}) ⇒ MtopV4

Returns a new instance of MtopV4.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/common/mtop_helper.rb', line 65

def initialize(opts = {})


    mtop_info = parse_json(get('http://qacenter.alitrip.net/tools/GetMtopInfoAction.do'))
    fail 'GetMtopInfo Fail' if mtop_info['success'] != true

    @@MTOP_CONFIG = mtop_info['data']

    if opts.is_a? Hash
        opts = MtopV4.symbolize_keys opts
        @api_name       = opts[:api_name] || nil
        @api_version    = opts[:api_version] || '*'
        @api_data       = opts[:api_data] || {}
        @env_type       = opts[:env_type] || 'wapa'
        @ecode_need     = opts[:ecode_need] || false
        @user_data      = opts[:user_data] || nil
        @http_method    = opts[:http_method] || 'POST'

        @base_url       = @@MTOP_CONFIG[@env_type]['url']
        @app_key        = @@MTOP_CONFIG[@env_type]['app_key']
        @app_secret     = @@MTOP_CONFIG[@env_type]['app_secret']

        @ttid           = @@MTOP_CONFIG['ttid']
        @devid          = @@MTOP_CONFIG['devid']
        @req_header     = {
            'm-t'               => Time.now.to_i.to_s,
            'm-appkey'          => @app_key,
            'm-ttid'            => @ttid,
            'm-pv'              => '3.0',
            'm-devid'           => @devid
        }
        @cookies = {}
    end
end

Class Method Details

.symbolize_keys(hash) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/common/mtop_helper.rb', line 130

def self.symbolize_keys(hash)
    fail 'symbolize_keys requires a hash' unless hash.is_a? Hash
    result = {}
    hash.each do |key, value|
        key = key.to_sym rescue key # rubocop:disable Style/RescueModifier
        result[key] = value.is_a?(Hash) ? symbolize_keys(value) : value
    end
    result
end

Instance Method Details

#requestObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/common/mtop_helper.rb', line 113

def request
    @req_header.store 'm-sign', sign()
    if @http_method === 'GET'
        @url = "http://#{@base_url}/gw/#{@api_name}/#{@api_version}/?data=#{@api_data.to_s}&type=originaljson"
        get @url, parse_json(@api_data), @req_header
    elsif @http_method === 'POST'
        @url = "http://#{@base_url}/gw/#{@api_name}/#{@api_version}/"
        data = {"data" => @api_data};
        post @url, data, @req_header, {}
    end
    parse_json @response.body
end

#signObject



126
127
128
# File 'lib/common/mtop_helper.rb', line 126

def sign
    OpenSSL::HMAC.hexdigest('sha1', @app_secret.to_s, ['', @app_key, @api_data.to_s, @req_header['m-t'], @api_name, @api_version, '', @ttid, @devid, '', ''].join('&').to_s)
end

#to_getObject



102
103
104
# File 'lib/common/mtop_helper.rb', line 102

def to_get
    @http_method = 'GET'
end

#to_postObject



99
100
101
# File 'lib/common/mtop_helper.rb', line 99

def to_post
    @http_method = 'POST'
end

#update(opts = {}) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/common/mtop_helper.rb', line 105

def update(opts={})
    opts = MtopV4.symbolize_keys opts
    @api_name       = opts[:api_name] || nil
    @api_version    = opts[:api_version] || '*'
    @api_data       = opts[:api_data] || {}
    @env_type       = opts[:env_type] || 'waptest'
    @http_method    = opts[:http_method] || 'GET'
end