Commit 5c8dab03 by Francis Zhou

change namespace

parent fe3ee552
lib = File.expand_path("../lib", __FILE__) lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "dingtalk/version" require "dingtalk_sdk/version"
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "dingtalk-sdk" spec.name = "dingtalk_sdk"
spec.version = Dingtalk::VERSION spec.version = DingtalkSdk::VERSION
spec.authors = ["francis"] spec.authors = ["francis"]
spec.email = ["francis.tm@gmail.com"] spec.email = ["francis.tm@gmail.com"]
......
require 'uri' require 'uri'
require 'base64' require 'base64'
require 'openssl' require 'openssl'
require "dingtalk/version"
require "dingtalk/auth" require "dingtalk_sdk/version"
require "dingtalk/user" require "dingtalk_sdk/auth"
require "dingtalk/department" require "dingtalk_sdk/user"
require "dingtalk/access_token" require "dingtalk_sdk/department"
require "dingtalk/corp_conversation" require "dingtalk_sdk/access_token"
require "dingtalk_sdk/corp_conversation"
module Dingtalk module DingtalkSdk
class Error < StandardError; end class Error < StandardError; end
class RequestFailedError < Error class RequestFailedError < Error
......
require "dingtalk/core" require "dingtalk_sdk/core"
require "dingtalk/request_url" require "dingtalk_sdk/request_url"
module Dingtalk module DingtalkSdk
module AccessToken module AccessToken
extend Dingtalk::Core extend DingtalkSdk::Core
# 获取 access_token # 获取 access_token
# {https://ding-doc.dingtalk.com/doc#/serverapi2/eev437} # {https://ding-doc.dingtalk.com/doc#/serverapi2/eev437}
# @!method get_access_token # @!method get_access_token
# @return [Hash] # @return [Hash]
add_request :get_access_token, :get, Dingtalk::RequestUrl::ACCESS_TOKEN do |request| add_request :get_access_token, :get, DingtalkSdk::RequestUrl::ACCESS_TOKEN do |request|
request.add_const :appkey, ->(r) { r.app_key }, in: :query request.add_const :appkey, ->(r) { r.app_key }, in: :query
request.add_const :appsecret, ->(r) { r.app_secret }, in: :query request.add_const :appsecret, ->(r) { r.app_secret }, in: :query
end end
......
require "dingtalk/core" require "dingtalk_sdk/core"
require "dingtalk/request_url" require "dingtalk_sdk/request_url"
require "active_support/core_ext/object/to_query" require "active_support/core_ext/object/to_query"
module Dingtalk module DingtalkSdk
module Auth module Auth
extend Dingtalk::Core extend DingtalkSdk::Core
# 企业内部应用免登录 用户ID获取 # 企业内部应用免登录 用户ID获取
# {https://ding-doc.dingtalk.com/doc#/serverapi2/clotub} # {https://ding-doc.dingtalk.com/doc#/serverapi2/clotub}
# @!method get_int_login_free_user_id(code:, access_token:) # @!method get_int_login_free_user_id(code:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_int_login_free_user_id, :get, Dingtalk::RequestUrl::INT_LOGIN_FREE_GET_USER_INFO do |request| add_request :get_int_login_free_user_id, :get, DingtalkSdk::RequestUrl::INT_LOGIN_FREE_GET_USER_INFO do |request|
request.add_arg :code, required: true, in: :query request.add_arg :code, required: true, in: :query
request.add_arg :access_token, required: true, in: :query request.add_arg :access_token, required: true, in: :query
end end
...@@ -26,14 +26,14 @@ module Dingtalk ...@@ -26,14 +26,14 @@ module Dingtalk
h[:redirect_uri] = redirect_uri h[:redirect_uri] = redirect_uri
end end
"#{Dingtalk::RequestUrl::CONNECT_QR_REDIRECT}?#{qs.to_query}" "#{DingtalkSdk::RequestUrl::CONNECT_QR_REDIRECT}?#{qs.to_query}"
end end
# 钉钉内免登录第三方网站 个人信息获取 # 钉钉内免登录第三方网站 个人信息获取
# {https://ding-doc.dingtalk.com/doc#/serverapi2/etaarr} # {https://ding-doc.dingtalk.com/doc#/serverapi2/etaarr}
# @!method get_3rd_login_free_user_profile(accessKey:, timestamp:, signature:, tmp_auth_code:) # @!method get_3rd_login_free_user_profile(accessKey:, timestamp:, signature:, tmp_auth_code:)
# @return [Hash] # @return [Hash]
add_request :get_3rd_login_free_user_profile, :post, Dingtalk::RequestUrl::GET_USER_INFO_SNS do |request| add_request :get_3rd_login_free_user_profile, :post, DingtalkSdk::RequestUrl::GET_USER_INFO_SNS do |request|
request.format = :json request.format = :json
request.add_arg :accessKey, required: true, in: :query request.add_arg :accessKey, required: true, in: :query
......
...@@ -3,9 +3,10 @@ require "httparty" ...@@ -3,9 +3,10 @@ require "httparty"
require "active_support" require "active_support"
require "active_support/core_ext/hash/keys" require "active_support/core_ext/hash/keys"
require "active_support/core_ext/hash/deep_merge" require "active_support/core_ext/hash/deep_merge"
require "dingtalk"
module Dingtalk require "dingtalk_sdk"
module DingtalkSdk
module Core module Core
class RequestBuilder class RequestBuilder
attr_reader :query_args, :body_args, :query_const, :body_const, :required_args attr_reader :query_args, :body_args, :query_const, :body_const, :required_args
...@@ -64,7 +65,7 @@ module Dingtalk ...@@ -64,7 +65,7 @@ module Dingtalk
when :query when :query
@query_args << arg_name @query_args << arg_name
else else
raise Dingtalk::Error.new("unknown argument '#{arg_name}' position") raise DingtalkSdk::Error.new("unknown argument '#{arg_name}' position")
end end
@required_args << arg_name if option[:required] @required_args << arg_name if option[:required]
...@@ -79,7 +80,7 @@ module Dingtalk ...@@ -79,7 +80,7 @@ module Dingtalk
when :query when :query
@query_const.merge! const_hash @query_const.merge! const_hash
else else
raise Dingtalk::Error.new("unknown argument '#{arg_name}' position") raise DingtalkSdk::Error.new("unknown argument '#{arg_name}' position")
end end
end end
end end
...@@ -91,7 +92,7 @@ module Dingtalk ...@@ -91,7 +92,7 @@ module Dingtalk
define_method request_name do |method_args = {}| define_method request_name do |method_args = {}|
request_options = {}.tap do |h| request_options = {}.tap do |h|
builder.required_args.each do |arg| builder.required_args.each do |arg|
raise Dingtalk::Error.new("missing required argument '#{arg}' when invoke request #{request_name}") \ raise DingtalkSdk::Error.new("missing required argument '#{arg}' when invoke request #{request_name}") \
if method_args[arg].nil? if method_args[arg].nil?
end end
...@@ -125,7 +126,7 @@ module Dingtalk ...@@ -125,7 +126,7 @@ module Dingtalk
symbolized_response = JSON.parse(response.body).deep_symbolize_keys symbolized_response = JSON.parse(response.body).deep_symbolize_keys
if symbolized_response[:errcode] != 0 if symbolized_response[:errcode] != 0
raise Dingtalk::RequestFailedError.new( raise DingtalkSdk::RequestFailedError.new(
code: symbolized_response[:errcode], code: symbolized_response[:errcode],
message: symbolized_response[:errmsg], message: symbolized_response[:errmsg],
) )
......
require 'dingtalk/core' require 'dingtalk_sdk/core'
require 'dingtalk/request_url' require 'dingtalk_sdk/request_url'
module Dingtalk module DingtalkSdk
module CorpConversation module CorpConversation
extend Dingtalk::Core extend DingtalkSdk::Core
# 发送工作通知消息 # 发送工作通知消息
# {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy} # {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy}
# @!method send_corp_conversation(userid_list:, dept_id_list:, to_all_user:, msg:, access_token:) # @!method send_corp_conversation(userid_list:, dept_id_list:, to_all_user:, msg:, access_token:)
# @return [Hash] # @return [Hash]
add_request :send_corp_conversation, :post, Dingtalk::RequestUrl::SEND_CORP_CONVERSATION do |request| add_request :send_corp_conversation, :post, DingtalkSdk::RequestUrl::SEND_CORP_CONVERSATION do |request|
request.add_arg :userid_list, in: :body request.add_arg :userid_list, in: :body
request.add_arg :dept_id_list, in: :body request.add_arg :dept_id_list, in: :body
request.add_arg :to_all_user, in: :body request.add_arg :to_all_user, in: :body
...@@ -24,7 +24,7 @@ module Dingtalk ...@@ -24,7 +24,7 @@ module Dingtalk
# {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy/e2262dad} # {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy/e2262dad}
# @!method get_corp_conversation_progress(task_id:, access_token:) # @!method get_corp_conversation_progress(task_id:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_corp_conversation_progress, :post, Dingtalk::RequestUrl::CORP_CONVERSATION_SEND_PROGRESS do |request| add_request :get_corp_conversation_progress, :post, DingtalkSdk::RequestUrl::CORP_CONVERSATION_SEND_PROGRESS do |request|
request.add_arg :task_id, in: :body, required: true request.add_arg :task_id, in: :body, required: true
request.add_arg :access_token, in: :query, required: true request.add_arg :access_token, in: :query, required: true
...@@ -36,7 +36,7 @@ module Dingtalk ...@@ -36,7 +36,7 @@ module Dingtalk
# {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy/a5920210} # {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy/a5920210}
# @!method get_corp_conversation_result(task_id:, access_token:) # @!method get_corp_conversation_result(task_id:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_corp_conversation_result, :post, Dingtalk::RequestUrl::CORP_CONVERSATION_SEND_RESULT do |request| add_request :get_corp_conversation_result, :post, DingtalkSdk::RequestUrl::CORP_CONVERSATION_SEND_RESULT do |request|
request.add_arg :task_id, in: :body, required: true request.add_arg :task_id, in: :body, required: true
request.add_arg :access_token, in: :query, required: true request.add_arg :access_token, in: :query, required: true
...@@ -48,7 +48,7 @@ module Dingtalk ...@@ -48,7 +48,7 @@ module Dingtalk
# {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy/hYyV8} # {https://ding-doc.dingtalk.com/doc#/serverapi2/pgoxpy/hYyV8}
# @!method recall_corp_conversation(msg_task_id:, access_token:) # @!method recall_corp_conversation(msg_task_id:, access_token:)
# @return [Hash] # @return [Hash]
add_request :recall_corp_conversation, :post, Dingtalk::RequestUrl::RECALL_CORP_CONVERSATION do |request| add_request :recall_corp_conversation, :post, DingtalkSdk::RequestUrl::RECALL_CORP_CONVERSATION do |request|
request.add_arg :msg_task_id, in: :body, required: true request.add_arg :msg_task_id, in: :body, required: true
request.add_arg :access_token, in: :query, required: true request.add_arg :access_token, in: :query, required: true
......
require "dingtalk/core" require "dingtalk_sdk/core"
require "dingtalk/request_url" require "dingtalk_sdk/request_url"
module Dingtalk module DingtalkSdk
module Department module Department
extend Dingtalk::Core extend DingtalkSdk::Core
# 获取部门列表 # 获取部门列表
# {https://ding-doc.dingtalk.com/doc#/serverapi2/dubakq/e6e1604e} # {https://ding-doc.dingtalk.com/doc#/serverapi2/dubakq/e6e1604e}
# @!method get_department_list(id:, fetch_child:, access_token:) # @!method get_department_list(id:, fetch_child:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_department_list, :get, Dingtalk::RequestUrl::GET_DEPARTMENT_LIST do |request| add_request :get_department_list, :get, DingtalkSdk::RequestUrl::GET_DEPARTMENT_LIST do |request|
request.add_arg :id, in: :query # 父部门id(如果不传,默认部门为根部门,根部门ID为1) request.add_arg :id, in: :query # 父部门id(如果不传,默认部门为根部门,根部门ID为1)
request.add_arg :fetch_child, in: :query # 是否递归部门的全部子部门 request.add_arg :fetch_child, in: :query # 是否递归部门的全部子部门
request.add_arg :access_token, required: true, in: :query request.add_arg :access_token, required: true, in: :query
...@@ -19,7 +19,7 @@ module Dingtalk ...@@ -19,7 +19,7 @@ module Dingtalk
# {https://ding-doc.dingtalk.com/doc#/serverapi2/dubakq/5bf960de} # {https://ding-doc.dingtalk.com/doc#/serverapi2/dubakq/5bf960de}
# @!method get_department_profile(id:, access_token:) # @!method get_department_profile(id:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_department_profile, :get, Dingtalk::RequestUrl::GET_DEPARTMENT_PROFILE do |request| add_request :get_department_profile, :get, DingtalkSdk::RequestUrl::GET_DEPARTMENT_PROFILE do |request|
request.add_arg :id, in: :query, required: true request.add_arg :id, in: :query, required: true
request.add_arg :access_token, in: :query, required: true request.add_arg :access_token, in: :query, required: true
end end
......
# frozen_string_literal: true # frozen_string_literal: true
module Dingtalk module DingtalkSdk
module RequestUrl module RequestUrl
# 获取 AccessToken # 获取 AccessToken
ACCESS_TOKEN = "https://oapi.dingtalk.com/gettoken" ACCESS_TOKEN = "https://oapi.dingtalk.com/gettoken"
......
require 'dingtalk/core' require 'dingtalk_sdk/core'
require 'dingtalk/request_url' require 'dingtalk_sdk/request_url'
module Dingtalk module DingtalkSdk
module Storage module Storage
extend Dingtalk::Core extend DingtalkSdk::Core
# 上传媒体文件 # 上传媒体文件
# {https://ding-doc.dingtalk.com/doc#/serverapi2/bcmg0i/08d5a73b} # {https://ding-doc.dingtalk.com/doc#/serverapi2/bcmg0i/08d5a73b}
# @!method upload_media(type:, access_token:, media:) # @!method upload_media(type:, access_token:, media:)
# @return [Hash] # @return [Hash]
add_request :upload_media, :post, Dingtalk::RequestUrl::UPLOAD_MEDIA_FILE do |request| add_request :upload_media, :post, DingtalkSdk::RequestUrl::UPLOAD_MEDIA_FILE do |request|
request.add_arg :type, in: query, required: true request.add_arg :type, in: query, required: true
request.add_arg :access_token, in: :query, required: true request.add_arg :access_token, in: :query, required: true
request.add_arg :media, in: :body, required: true request.add_arg :media, in: :body, required: true
......
require "dingtalk/core" require "dingtalk_sdk/core"
require "dingtalk/request_url" require "dingtalk_sdk/request_url"
module Dingtalk module DingtalkSdk
module User module User
extend Dingtalk::Core extend DingtalkSdk::Core
# 获取用户详情 # 获取用户详情
# {https://ding-doc.dingtalk.com/doc#/serverapi2/ege851/AaRQe} # {https://ding-doc.dingtalk.com/doc#/serverapi2/ege851/AaRQe}
# @!method get_user_profile(userid:, access_token:) # @!method get_user_profile(userid:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_user_profile, :get, Dingtalk::RequestUrl::GET_USER_PROFILE do |request| add_request :get_user_profile, :get, DingtalkSdk::RequestUrl::GET_USER_PROFILE do |request|
request.add_arg :userid, required: true, in: :query request.add_arg :userid, required: true, in: :query
request.add_arg :access_token, required: true, in: :query request.add_arg :access_token, required: true, in: :query
end end
...@@ -18,7 +18,7 @@ module Dingtalk ...@@ -18,7 +18,7 @@ module Dingtalk
# {https://ding-doc.dingtalk.com/doc#/serverapi2/ege851/602f4b15} # {https://ding-doc.dingtalk.com/doc#/serverapi2/ege851/602f4b15}
# @!method get_userid_by_unionid(unionid:, access_token:) # @!method get_userid_by_unionid(unionid:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_userid_by_unionid, :get, Dingtalk::RequestUrl::GET_USERID_FROM_UNIONID do |request| add_request :get_userid_by_unionid, :get, DingtalkSdk::RequestUrl::GET_USERID_FROM_UNIONID do |request|
request.add_arg :unionid, required: true, in: :query request.add_arg :unionid, required: true, in: :query
request.add_arg :access_token, required: true, in: :query request.add_arg :access_token, required: true, in: :query
end end
...@@ -27,7 +27,7 @@ module Dingtalk ...@@ -27,7 +27,7 @@ module Dingtalk
# {https://ding-doc.dingtalk.com/doc#/serverapi2/ege851/soV11} # {https://ding-doc.dingtalk.com/doc#/serverapi2/ege851/soV11}
# @!method get_userid_by_mobile(mobile:, access_token:) # @!method get_userid_by_mobile(mobile:, access_token:)
# @return [Hash] # @return [Hash]
add_request :get_userid_by_mobile, :get, Dingtalk::RequestUrl::GET_USERID_FROM_MOBILE do |request| add_request :get_userid_by_mobile, :get, DingtalkSdk::RequestUrl::GET_USERID_FROM_MOBILE do |request|
request.add_arg :mobile, required: true, in: :query request.add_arg :mobile, required: true, in: :query
request.add_arg :access_token, required: true, in: :query request.add_arg :access_token, required: true, in: :query
end end
......
module Dingtalk module DingtalkSdk
VERSION = "0.1.0" VERSION = "0.1.0"
end end
require "json" require "json"
require "dingtalk" require "dingtalk_sdk"
require "dingtalk/access_token" require "dingtalk_sdk/access_token"
RSpec.describe Dingtalk::AccessToken do RSpec.describe DingtalkSdk::AccessToken do
before :each do before :each do
@agent_id = "mocked_agent_id" @agent_id = "mocked_agent_id"
@app_key = "mocked_app_key" @app_key = "mocked_app_key"
@app_secret = "mocked_app_secret" @app_secret = "mocked_app_secret"
@mocked_access_token = "mocked_access_token" @mocked_access_token = "mocked_access_token"
@dingtalk_request = Dingtalk::Request.new( @dingtalk_request = DingtalkSdk::Request.new(
agent_id: @agent_id, agent_id: @agent_id,
app_key: @app_key, app_key: @app_key,
app_secret: @app_secret, app_secret: @app_secret,
......
...@@ -3,10 +3,10 @@ require "uri" ...@@ -3,10 +3,10 @@ require "uri"
require "json" require "json"
require "securerandom" require "securerandom"
require "dingtalk" require "dingtalk_sdk"
require "dingtalk/auth" require "dingtalk_sdk/auth"
RSpec.describe Dingtalk::Auth do RSpec.describe DingtalkSdk::Auth do
before :each do before :each do
@agent_id = "mocked_agent_id" @agent_id = "mocked_agent_id"
@app_key = "mocked_app_key" @app_key = "mocked_app_key"
...@@ -17,9 +17,9 @@ RSpec.describe Dingtalk::Auth do ...@@ -17,9 +17,9 @@ RSpec.describe Dingtalk::Auth do
@int_login_code = "mocked_int_login_code" @int_login_code = "mocked_int_login_code"
@timestamp = (Time.now.localtime("+08:00").to_f * 1000).to_i @timestamp = (Time.now.localtime("+08:00").to_f * 1000).to_i
@signature = Dingtalk.login_free_signature(@app_secret, timestamp: @timestamp) @signature = DingtalkSdk.login_free_signature(@app_secret, timestamp: @timestamp)
@dingtalk_request = Dingtalk::Request.new( @dingtalk_request = DingtalkSdk::Request.new(
agent_id: @agent_id, agent_id: @agent_id,
app_key: @app_key, app_key: @app_key,
app_secret: @app_secret, app_secret: @app_secret,
......
RSpec.describe Dingtalk do RSpec.describe DingtalkSdk do
it "has a version number" do it "has a version number" do
expect(Dingtalk::VERSION).not_to be nil expect(DingtalkSdk::VERSION).not_to be nil
end end
# 测试样本见 https://ding-doc.dingtalk.com/doc#/faquestions/hxs5v9 底部 # 测试样本见 https://ding-doc.dingtalk.com/doc#/faquestions/hxs5v9 底部
...@@ -8,9 +8,9 @@ RSpec.describe Dingtalk do ...@@ -8,9 +8,9 @@ RSpec.describe Dingtalk do
timestamp = 1546084445901 timestamp = 1546084445901
app_secret = "testappSecret" app_secret = "testappSecret"
signature = Dingtalk.login_free_signature(app_secret, timestamp: timestamp) signature = DingtalkSdk.login_free_signature(app_secret, timestamp: timestamp)
expect(signature).to be_a(Dingtalk::Signature) expect(signature).to be_a(DingtalkSdk::Signature)
expect(signature.url_encoded).to eq("HCbG3xNE3vzhO%2Bu7qCUL1jS5hsu2n5r2cFhnTrtyDAE%3D") expect(signature.url_encoded).to eq("HCbG3xNE3vzhO%2Bu7qCUL1jS5hsu2n5r2cFhnTrtyDAE%3D")
end end
end end
require "pry" require "pry"
require "bundler/setup" require "bundler/setup"
require "webmock/rspec" require "webmock/rspec"
require "dingtalk" require "dingtalk_sdk"
RSpec.configure do |config| RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure # Enable flags like --only-failures and --next-failure
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment