class Dianping::Api::Client

Attributes

app_key[R]
redirect_url[RW]
site[R]
token[R]
token_root[R]

Public Class Methods

new(app_key, secret, **options) click to toggle source
# File lib/dianping/api/client.rb, line 13
def initialize(app_key, secret, **options)
  @app_key = app_key
  @secret = secret
  @site = options[:site] || 'https://openapi.dianping.com'
  @redirect_url = options[:redirect_url]
  @token_root = options[:token_root] || '/tmp'
  @token = Token.new(self)
end

Public Instance Methods

auth(auth_code) click to toggle source
# File lib/dianping/api/client.rb, line 43
def auth(auth_code)
  token.auth(auth_code)
end
auth_conn() click to toggle source
# File lib/dianping/api/client.rb, line 47
def auth_conn
  Faraday.new(url: @site) do |conn|
    conn.request :url_encoded
    conn.response :logger
    conn.response :raise_error
    conn.adapter :net_http
  end
end
auth_token(auth_code) click to toggle source
# File lib/dianping/api/client.rb, line 56
def auth_token(auth_code)
  res = auth_conn.post('/router/oauth/token') do |req|
    req.body = {
      app_key: @app_key,
      app_secret: @secret,
      auth_code: auth_code,
      grant_type: 'authorization_code',
      redirect_url: redirect_url
    }
  end
  json(res.body)
end
connection() click to toggle source
# File lib/dianping/api/client.rb, line 22
def connection
  Faraday.new(url: @site) do |conn|
    conn.request :retry
    conn.request :dianping, self
    conn.request :url_encoded
    conn.response :logger
    conn.response :raise_error
    conn.adapter :net_http
  end
end
get(url = nil, params = nil, headers = nil) click to toggle source
# File lib/dianping/api/client.rb, line 33
def get(url = nil, params = nil, headers = nil)
  res = connection.get(url, sign_with_share(params), headers)
  json(res.body)
end
json(text) click to toggle source
# File lib/dianping/api/client.rb, line 85
def json(text)
  MultiJson.load(text || '{}', symbolize_keys: true)
end
merge_params(params) click to toggle source
# File lib/dianping/api/client.rb, line 113
def merge_params(params)
  share_params.merge(params || {}).dup.reject { |_k, v| v.nil? }
end
post(url = nil, body = nil, headers = nil) click to toggle source
# File lib/dianping/api/client.rb, line 38
def post(url = nil, body = nil, headers = nil)
  res = connection.post(url, sign_with_share(body), headers)
  json(res.body)
end
refresh_token(refresh_code) click to toggle source
# File lib/dianping/api/client.rb, line 69
def refresh_token(refresh_code)
  res = auth_conn.post('/router/oauth/token') do |req|
    req.body = {
      app_key: @app_key,
      app_secret: @secret,
      refresh_token: refresh_code,
      grant_type: 'refresh_token'
    }
  end
  json(res.body)
end
requestid() click to toggle source
# File lib/dianping/api/client.rb, line 89
def requestid
  SecureRandom.hex
end
scope_shops() click to toggle source
# File lib/dianping/api/client.rb, line 81
def scope_shops
  get('/router/oauth/session/scope', bid: token.bid)
end
share_params() click to toggle source
# File lib/dianping/api/client.rb, line 93
def share_params
  {
    app_key: app_key,
    timestamp: Time.now.strftime('%F %T'),
    session: token.access_token,
    format: 'json',
    v: 1,
    sign_method: 'MD5'
  }
end
sign_with_share(params = {}) click to toggle source
# File lib/dianping/api/client.rb, line 104
def sign_with_share(params = {})
  merged = merge_params(params)
  content = merged.to_a.sort.flatten.join.encode!('UTF-8')
  # puts @secret + content
  Api.logger.debug { format('content: %s', content) }
  sign = Digest::MD5.hexdigest([@secret, content, @secret].compact.join)
  merged.merge(sign: sign)
end