module Haoyaoshi
Constants
- CUSTOM_ENDPOINT
用于标记endpoint可以直接使用url作为完整请求API
- GLOBAL_CODES
- GRANT_TYPE
- OK_CODE
- OK_MSG
- VERSION
Attributes
config[RW]
Public Class Methods
api_base_url()
click to toggle source
# File lib/haoyaoshi/config.rb, line 37 def api_base_url if config.nil? return "https://api.ehaoyao.com" end @api_base_url ||= config.api_base_url end
api_endpoint()
click to toggle source
# File lib/haoyaoshi.rb, line 91 def api_endpoint api_base_url end
calculate_expire(expires_in)
click to toggle source
# File lib/haoyaoshi.rb, line 115 def calculate_expire(expires_in) Time.now.to_i + expires_in.to_i - key_expired.to_i end
configure() { |config ||= config| ... }
click to toggle source
# File lib/haoyaoshi/config.rb, line 7 def configure yield self.config ||= Config.new end
endpoint_url(endpoint, url)
click to toggle source
# File lib/haoyaoshi.rb, line 80 def endpoint_url(endpoint, url) # 此处为了应对第三方开发者如果自助对接接口时,URL不规范的情况下,可以直接使用URL当为endpoint return url if endpoint == CUSTOM_ENDPOINT Rails.logger.info("#{endpoint}_endpoint") send("#{endpoint}_endpoint") + url end
haoyaoshi_redis()
click to toggle source
# File lib/haoyaoshi/config.rb, line 11 def haoyaoshi_redis return nil if config.nil? @redis ||= config.redis end
http_delete_without_token(url, delete_body={}, url_params={}, endpoint="plain")
click to toggle source
# File lib/haoyaoshi.rb, line 51 def http_delete_without_token(url, delete_body={}, url_params={}, endpoint="plain") delete_api_url = endpoint_url(endpoint, url) if endpoint == "plain" || endpoint == CUSTOM_ENDPOINT delete_body = JSON.dump(delete_body) else delete_body = JSON.dump(delete_body) end Rails.logger.info("====好药师order请求参数===>#{delete_api_url}===#{delete_body}=======#{url_params}=#{endpoint}") load_json(resource(delete_api_url).delete(params: url_params,raw_response: true,:content_type => :json)) end
http_get_without_token(url, url_params={}, endpoint="plain")
click to toggle source
# File lib/haoyaoshi.rb, line 34 def http_get_without_token(url, url_params={}, endpoint="plain") get_api_url = endpoint_url(endpoint, url) Rails.logger.info("====好药师order请求参数===>#{get_api_url}===#{url_params}===#{endpoint}") load_json(resource(get_api_url).get(params: url_params)) end
http_post_without_token(url, post_body={}, url_params={}, endpoint="plain")
click to toggle source
# File lib/haoyaoshi.rb, line 40 def http_post_without_token(url, post_body={}, url_params={}, endpoint="plain") post_api_url = endpoint_url(endpoint, url) if endpoint == "plain" || endpoint == CUSTOM_ENDPOINT post_body = JSON.dump(post_body) else post_body = JSON.dump(post_body) end Rails.logger.info("====好药师order请求参数===>#{post_api_url}===#{post_body}=======#{url_params}=#{endpoint}") load_json(resource(post_api_url).post(post_body, params: url_params,raw_response: true,:content_type => :json)) end
img_base_url()
click to toggle source
# File lib/haoyaoshi/config.rb, line 23 def img_base_url if config.nil? return "http://img01.img.ehaoyao.com" end @img_base_url ||= config.img_base_url end
img_endpoint(url)
click to toggle source
# File lib/haoyaoshi.rb, line 111 def img_endpoint(url) "#{img_base_url}#{url}" end
key_expired()
click to toggle source
# File lib/haoyaoshi/config.rb, line 16 def key_expired if config.nil? return 100 end @key_expired ||= config.key_expired end
load_json(string)
click to toggle source
return hash
# File lib/haoyaoshi.rb, line 67 def load_json(string) result_hash = JSON.parse(string.force_encoding("UTF-8").gsub(/[\u0011-\u001F]/, "")) if result_hash.present? code = result_hash.delete("errcode") en_msg = result_hash.delete("errmsg") else result_hash = {} code = 0 en_msg = "请求成功" end ResultHandler.new(code, en_msg, result_hash) end
open_base_url()
click to toggle source
# File lib/haoyaoshi/config.rb, line 30 def open_base_url if config.nil? return "http://api.goodscenter.ehaoyao.com" end @open_base_url ||= config.open_base_url end
open_endpoint()
click to toggle source
# File lib/haoyaoshi.rb, line 107 def open_endpoint open_base_url end
order_base_url()
click to toggle source
# File lib/haoyaoshi/config.rb, line 44 def order_base_url if config.nil? return "https://api.ehaoyao.com" end @order_base_url ||= config.order_base_url end
order_center_base_url()
click to toggle source
# File lib/haoyaoshi/config.rb, line 51 def order_center_base_url if config.nil? return "https://internal.api.ehaoyao.com" end @order_center_base_url ||= config.order_center_base_url end
order_center_endpoint()
click to toggle source
# File lib/haoyaoshi.rb, line 99 def order_center_endpoint order_center_base_url end
order_endpoint()
click to toggle source
# File lib/haoyaoshi.rb, line 95 def order_endpoint order_base_url end
plain_endpoint()
click to toggle source
# File lib/haoyaoshi.rb, line 87 def plain_endpoint "#{api_endpoint}" end
resource(url)
click to toggle source
# File lib/haoyaoshi.rb, line 62 def resource(url) RestClient::Resource.new(url, rest_client_options) end
rest_client_options()
click to toggle source
可选配置: RestClient timeout, etc. key 必须是符号 如果出现 RestClient::SSLCertificateNotVerified Exception: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed 这个错,除了改 verify_ssl: true,请参考:www.extendi.it/blog/2015/5/23/47-sslv3-read-server-certificate-b-certificate-verify-failed
# File lib/haoyaoshi/config.rb, line 62 def rest_client_options if config.nil? return {timeout: 5, open_timeout: 5, verify_ssl: true} end @rest_client_options ||= config.rest_client_options end
token_endpoint(url)
click to toggle source
# File lib/haoyaoshi.rb, line 103 def token_endpoint(url) "#{api_endpoint}#{url}" end