imwukong 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f839e78ed7eb750f06fe3b23a20e9d2b2038b77
4
- data.tar.gz: 2cb0f34fa327598e8e5f0da64b1829cf605b7bdf
3
+ metadata.gz: 646a6144178082387c93319db9fe27f04f89733c
4
+ data.tar.gz: a09298d354196e1afdbcbc3b0bf30445e4ab2f24
5
5
  SHA512:
6
- metadata.gz: fb6fff8bdd2c0c775db24fdfbbcd018c5616ab3fe29c8b87fa6a241edd4cdaaf42cf5ce5078b237001485e5e1541686933c0f9d4b2ed3594b55465b5c48f54f2
7
- data.tar.gz: 4c197d257bc8ea4217c268dfdf7600503885c43b61387bf6f4cd7c74fd346de7ae459fbfb44a9fed31e015d9e80cf9aaeadacf81f88eb7100297d860afb5043d
6
+ metadata.gz: 64528f4879b345a808a8432bdbfaf40c72a613b5a5213899b37b77f4a51ef10b9b1d51b0d5b181be2d8c31598239808ed1131bd650fc8b8ada6dc5257a64d738
7
+ data.tar.gz: f49e656fb60f748696029f76d6e5f730eff76955ac0e2984da9c02c731417735582c9b1839a2b00dea01e3f1a2e5836ee796fee217bf03da32727a45d2163794
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- imwukong (0.1.4)
4
+ imwukong (0.1.5)
5
5
  activesupport (~> 4.2.0)
6
- httparty (~> 0.13.0)
6
+ httpclient (~> 2.7.0.1)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
@@ -15,13 +15,10 @@ GEM
15
15
  thread_safe (~> 0.3, >= 0.3.4)
16
16
  tzinfo (~> 1.1)
17
17
  diff-lcs (1.2.5)
18
- httparty (0.13.3)
19
- json (~> 1.8)
20
- multi_xml (>= 0.5.2)
18
+ httpclient (2.7.0.1)
21
19
  i18n (0.7.0)
22
20
  json (1.8.3)
23
- minitest (5.8.2)
24
- multi_xml (0.5.5)
21
+ minitest (5.8.3)
25
22
  rake (10.4.2)
26
23
  rspec (3.3.0)
27
24
  rspec-core (~> 3.3.0)
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  Add this line to your application's Gemfile:
7
7
 
8
8
  ```ruby
9
- gem 'imwukong', github: 'dannyxu2015/imwukong', branch: 'master'
9
+ gem 'imwukong'
10
10
  ```
11
11
 
12
12
  And then execute:
data/imwukong.gemspec CHANGED
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 1.10"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec"
33
- spec.add_dependency "httparty", "~> 0.13.0"
33
+ spec.add_dependency "httpclient", "~> 2.7.0.1"
34
34
  spec.add_dependency "activesupport", "~> 4.2.0"
35
35
  end
data/lib/imwukong/api.rb CHANGED
@@ -5,6 +5,14 @@ module Imwukong
5
5
  STATUS_FOLLOW_BY = 1 # 被关注
6
6
  STATUS_BI_FOLLOW = 2 # 双向关系
7
7
 
8
+ # api description
9
+ # method_group: string, 接口分组名
10
+ # method_pluralize: boolean, 接口名是否为复数形式, 默认为false
11
+ # method_name: 接口名
12
+ # prefix: url前缀, 默认是'v1/im',
13
+ # url: 接口url
14
+ # args: 参数列表, symbol array
15
+ # default: 可选参数的默认值, 调用时可以不用传
8
16
  API_LIST = [
9
17
  # 用户
10
18
  {
@@ -46,7 +54,12 @@ module Imwukong
46
54
  method_name: 'create',
47
55
  method_pluralize: false,
48
56
  http_method: :post,
49
- url: 'create'
57
+ url: 'create',
58
+ args: [:openId, :type, :icon, :title, :members],
59
+ default: {
60
+ icon: '',
61
+ title: ''
62
+ }
50
63
  },
51
64
  {
52
65
  # 查询聊天会话的概要信息
@@ -452,6 +465,7 @@ module Imwukong
452
465
  method_name = "wk_#{method_group}_#{api[:method_name]}"
453
466
  fail "Method #{method_name} already defined" if respond_to?(method_name)
454
467
  define_method method_name do |params|
468
+ params.merge!(api[:default]||{})
455
469
  check_params(params, api[:args]||[])
456
470
  self.send "wk_#{api[:http_method]}", method_group, api[:url], api[:prefix]||'v1/im', params
457
471
  end
data/lib/imwukong/base.rb CHANGED
@@ -1,63 +1,48 @@
1
- require 'httparty'
1
+ require 'httpclient'
2
2
  require 'digest'
3
+ require 'json'
3
4
 
4
5
  module Imwukong
5
6
  class Base
6
- include HTTParty
7
- # include Api::User
8
- # include Api::Conversation
9
- # include Api::Message
10
- # include Api::Follow
11
- # include Api::Upload
12
- # include Api::Push
13
7
 
14
- DEFAULT_OPTIONS = { api_version: 'v1' }
15
8
  DEV_HOST = 'https://sandbox-wkapi.laiwang.com'.freeze
16
9
  PRODUCTION_HOST = "https://wkapi.laiwang.com"
17
10
 
18
11
  def initialize(app_domain = Imwukong.config[:domain],
19
12
  app_token = Imwukong.config[:server][:app_token])
20
13
  fail 'app domain/token is invalid' unless app_domain.present? && app_token.present?
21
- @app_domain = app_domain.strip
22
- @app_token = app_token.strip
23
- @options = DEFAULT_OPTIONS
14
+ @app_domain = app_domain.strip
15
+ @app_token = app_token.strip
16
+ @http_client = HTTPClient.new
24
17
  end
25
18
 
26
19
  def wk_post(type, method, prefix='v1/im', params)
27
- options = {
28
- body: params.to_json,
29
- headers: wukong_header
30
- }
31
- prefix ||= 'v1/im'
32
- result = self.class.send(:post, get_request_url(type, method, prefix), options) rescue nil
20
+ prefix ||= 'v1/im'
21
+ result = @http_client.post(get_request_url(type, method, prefix), params.to_json, wukong_header) rescue nil
33
22
  handle_response(result)
34
23
  end
35
24
 
36
25
  def wk_get(type, method, prefix='v1/im', params)
37
- options = {
38
- query: params,
39
- headers: wukong_header
40
- }
41
- prefix ||= 'v1/im'
42
- result = self.class.send(:get, get_request_url(type, method, prefix), options) rescue nil
26
+ prefix ||= 'v1/im'
27
+ result = @http_client.get(get_request_url(type, method, prefix), params, wukong_header) rescue nil
43
28
  handle_response(result)
44
29
  end
45
30
 
46
- # def safe_json_parse(data)
47
- # warn data.inspect
48
- # JSON.parse(data)
49
- # # rescue TypeError which will cause 'no implicit conversion of HTTParty::Response into String'
50
- # rescue JSON::ParserError, TypeError
51
- # {}
52
- # end
53
-
54
31
  private
55
32
 
33
+ def safe_json_parse(data)
34
+ JSON.parse(data)
35
+ # rescue TypeError which will cause 'no implicit conversion of HTTParty::Response into String'
36
+ rescue JSON::ParserError, TypeError
37
+ warn data.inspect
38
+ {}
39
+ end
40
+
56
41
  def handle_response(result)
42
+ # warn result.inspect
57
43
  fail 'No respond from server' if result.nil?
58
- warn ">>> Code: #{result.code}, " + result.inspect
59
- r = result.parsed_response
60
- fail result.inspect unless result.code == 200
44
+ fail "Request fail, return code:#{result.code}" unless result.ok?
45
+ r = safe_json_parse(result.body)
61
46
  fail "#{r['errorCode']}, #{r['errorMessage']}" if r['success'] == false
62
47
  r['data']
63
48
  end
@@ -67,7 +52,7 @@ module Imwukong
67
52
  end
68
53
 
69
54
  def get_request_url(type, method, prefix='v1/im')
70
- warn "#{wukong_host}/#{prefix}/#{type}/#{method}"
55
+ # warn "#{wukong_host}/#{prefix}/#{type}/#{method}"
71
56
  "#{wukong_host}/#{prefix}/#{type}/#{method}"
72
57
  end
73
58
 
@@ -76,7 +61,6 @@ module Imwukong
76
61
  @nonce = rand(100000..200000).to_s
77
62
  array = [token, @timestamp, @nonce].sort
78
63
  sign = Digest::SHA1.hexdigest(array.join)
79
- warn "Sign: #{sign}"
80
64
  sign
81
65
  end
82
66
 
@@ -1,3 +1,3 @@
1
1
  module Imwukong
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imwukong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Xu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-10 00:00:00.000000000 Z
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,19 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: httparty
56
+ name: httpclient
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.13.0
61
+ version: 2.7.0.1
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.13.0
68
+ version: 2.7.0.1
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
71
  requirement: !ruby/object:Gem::Requirement