Module: Wechate::Http
- Included in:
- Client
- Defined in:
- lib/wechate/http.rb
Instance Method Summary collapse
Instance Method Details
#api_get(url, args = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/wechate/http.rb', line 6 def api_get(url,args={}) get_api_token unless @access_token url = "#{@urls[url]}?" args.each{|k,v| url = url + "&#{k.to_s}=#{v.to_s}"} if !@access_token then get_api_token return nil unless @access_token end conn = Faraday.new(:url =>@host_api) req = url + "&access_token=#{@access_token}" response = conn.get req ret = JSON.parse(response.body) if ret["errcode"]!=0 then #expired if "42001"==ret["errcode"] then return nil unless get_api_token response = conn.get url + "&access_token=#{@access_token}" else return nil end end return ret end |
#api_post(url, body) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/wechate/http.rb', line 31 def api_post(url,body) get_api_token unless @access_token conn = Faraday.new(:url => @host_api) response = conn.post "#{@urls[url]}?access_token=#{@access_token}" do |req| req.body = body end return response end |
#get_api_token ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/wechate/http.rb', line 40 def get_api_token conn = Faraday.new(:url =>@host_api) response = conn.get "/cgi-bin/gettoken" do |req| req.params['corpid'] = @appid req.params['corpsecret'] = @secret end @access_token = JSON.parse(response.body)["access_token"] end |