Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zhiren #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/wework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'active_support/all'
#require 'active_support/core_ext/object/blank'

require 'wework/cipher'

Dir["#{File.dirname(__FILE__)}/wework/*.rb"].each do |path|
require path
end
Expand Down
21 changes: 21 additions & 0 deletions lib/wework/api/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def get_oauth_userinfo code
get 'user/getuserinfo', params: {code: code}
end

def get_session_with_jscode(js_code, grant_type='authorization_code')
get 'miniprogram/jscode2session', params: {js_code: js_code, grant_type: grant_type}
end

def get_jssign_package url
timestamp = Time.now.to_i
noncestr = SecureRandom.hex(8)
Expand Down Expand Up @@ -83,6 +87,23 @@ def news_message_send user_ids, department_ids, news=[]
message_send user_ids, department_ids, {news: {articles: news}, msgtype: 'news'}
end

def get_checkin_data start_time, end_time, userid_list=[], checkin_type=3
# https://work.weixin.qq.com/api/doc#11196
post 'checkin/getcheckindata', {
opencheckindatatype: checkin_type,
starttime: start_time,
endtime: end_time,
useridlist: userid_list,
}
end

def get_approval_data start_time, end_time, next_spnum=nil
# https://work.weixin.qq.com/api/doc#11228
payload = {starttime: start_time, endtime: end_time}
payload[:next_spnum] = next_spnum unless next_spnum.nil?
post 'corp/getapprovaldata', payload
end

private

def agent_id
Expand Down
7 changes: 4 additions & 3 deletions lib/wework/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Api
class Base
include Wework::Cipher

attr_reader :corp_id, :app_id, :app_secret
attr_reader :corp_id, :app_id, :app_secret, :proxy
attr_accessor :options

delegate :access_token, to: :token_store
Expand All @@ -19,11 +19,12 @@ def initialize(corp_id, app_id, app_secret, options={})
@corp_id = corp_id
@app_id = app_id
@app_secret = app_secret
@proxy = options.delete(:proxy)
@options = options
end

def request
@request ||= Wework::Request.new(API_ENDPOINT, false)
@request ||= Wework::Request.new(API_ENDPOINT, false, proxy: proxy)
end

def valid?
Expand Down Expand Up @@ -81,4 +82,4 @@ def jsticket_store
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/wework/api/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Wework
module Api
class Contact < Base

def initialize(corp_id, corp_secret)
super(corp_id, CONTACT_AGENT_ID, corp_secret)
def initialize(corp_id, corp_secret, proxy: nil)
super(corp_id, CONTACT_AGENT_ID, corp_secret, { proxy: proxy })
end

def user_create data={}
Expand Down Expand Up @@ -78,4 +78,4 @@ def batch_params media_id, callback_url, token, encodingaeskey
end
end
end
end
end
9 changes: 5 additions & 4 deletions lib/wework/engine.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module Wework
class Engine

attr_reader :corp_id, :corp_secret, :app_id, :app_secret
attr_reader :corp_id, :corp_secret, :app_id, :app_secret, :proxy

def initialize(options={})
@corp_id = options[:corp_id]
@corp_secret = options[:corp_secret]
@app_id = options[:app_id]
@app_secret = options[:app_secret]
@proxy = options[:proxy]
end

def contact
@contact ||= Wework::Api::Contact.new(corp_id, corp_secret) if contact?
@contact ||= Wework::Api::Contact.new(corp_id, corp_secret, proxy: proxy) if contact?
end

def agent
@agent ||= Wework::Api::Agent.new(corp_id, app_id, app_secret) if agent?
@agent ||= Wework::Api::Agent.new(corp_id, app_id, app_secret, { proxy: proxy }) if agent?
end

private
Expand All @@ -28,4 +29,4 @@ def contact?
corp_id.present? && corp_secret.present?
end
end
end
end
9 changes: 6 additions & 3 deletions lib/wework/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ module Wework
class Request
attr_reader :base, :ssl_context, :httprb

def initialize(base, skip_verify_ssl)
# params proxy should be { address: 'ip address', port: 8888, username: 'username', password: 'password' }
def initialize(base, skip_verify_ssl, proxy: nil)
@base = base
@httprb = HTTP.timeout(**Wework.http_timeout_options)
@httprb = @httprb.via(*proxy.values) if proxy.is_a?(Hash)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.ssl_version = :TLSv1_client
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE if skip_verify_ssl
Expand Down Expand Up @@ -72,7 +74,8 @@ def parse_response(response, as)
data = file

when :json
data = JSON.parse response.body.to_s.gsub(/[\u0000-\u001f]+/, '')
res = response.body.to_s.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
data = JSON.parse res.gsub(/[\u0000-\u001f]+/, '')
else
data = response.body
end
Expand Down Expand Up @@ -103,4 +106,4 @@ def success?
errcode == SUCCESS_CODE
end
end
end
end
4 changes: 2 additions & 2 deletions wework.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency 'http', '>= 1.0.4', '< 3'
spec.add_dependency 'http', '>= 1.0.4'
spec.add_dependency 'activesupport', '~> 5.0'
spec.add_dependency 'redis', '~>3.2'
spec.add_dependency 'redis', '~>3.3'

spec.add_development_dependency "bundler", "~> 1.13"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down