easy_weibo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9bb2c8cf7bed11a3a9c6c3bb4dae6e37e9b3ed2e5750cc35a27f8a15a11ada3a
4
+ data.tar.gz: c8f038576af0cbd93c73a6e36a324e0f9a9d99e684765b6085c6432622e891b4
5
+ SHA512:
6
+ metadata.gz: d2a6c3b26eb2e882cca777e2e1b92155c5012b4ba4d72f904a20e7dddcc8b826cd8178c282ca3a0cb24f181fd46528e813694a944054f5b5c934397a697d7d5a
7
+ data.tar.gz: 4f18234cb0c62d9326c6bb973705884da19b0960d1d9f34537218a4f0cb80ce31a09e77f81b60ffda4f7bd5a436188439aba9f751a13fef10ec2fe343381b5a0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-07-12
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in easy_weibo.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 12.0"
9
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ easy_weibo (0.1.0)
5
+ activesupport
6
+ http-form_data
7
+ httpx
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.1.4)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ zeitwerk (~> 2.3)
18
+ coderay (1.1.3)
19
+ concurrent-ruby (1.1.9)
20
+ diff-lcs (1.4.4)
21
+ http-2-next (0.4.3)
22
+ http-form_data (2.3.0)
23
+ httpx (0.15.3)
24
+ http-2-next (>= 0.4.1)
25
+ timers
26
+ i18n (1.8.10)
27
+ concurrent-ruby (~> 1.0)
28
+ method_source (1.0.0)
29
+ minitest (5.14.4)
30
+ pry (0.14.1)
31
+ coderay (~> 1.1)
32
+ method_source (~> 1.0)
33
+ rake (12.3.3)
34
+ rspec (3.10.0)
35
+ rspec-core (~> 3.10.0)
36
+ rspec-expectations (~> 3.10.0)
37
+ rspec-mocks (~> 3.10.0)
38
+ rspec-core (3.10.1)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-expectations (3.10.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-mocks (3.10.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-support (3.10.2)
47
+ timers (4.3.3)
48
+ tzinfo (2.0.4)
49
+ concurrent-ruby (~> 1.0)
50
+ zeitwerk (2.4.2)
51
+
52
+ PLATFORMS
53
+ ruby
54
+
55
+ DEPENDENCIES
56
+ bundler
57
+ easy_weibo!
58
+ pry
59
+ rake (~> 12.0)
60
+ rspec (~> 3.0)
61
+
62
+ BUNDLED WITH
63
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 42up
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # EasyWeibo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/easy_weibo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'easy_weibo'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install easy_weibo
22
+
23
+ ## Usage
24
+ ```
25
+ EasyWeibo.configure do |config|
26
+ config.app_key = ""
27
+ config.app_secret = ""
28
+ config.redirect_uri = "https://api.weibo.com/oauth2/default.html"
29
+ end
30
+
31
+ @client = EasyWeibo::Client.new
32
+ puts @client.authorize_url
33
+ @client.code = "code"
34
+ @client.token = "token"
35
+ @client.statuses_share("foobar", "https://web.com", "#{EasyWeibo.root}/test.jpg")
36
+ ```
37
+
38
+ ## Todo
39
+ + 异常处理
40
+
41
+ ```
42
+ # {"error":"invalid_grant","error_code":21325,"request":"/oauth2/access_token","error_uri":"/oauth2/access_token","error_description":"invalid authorization code:"}
43
+ ```
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_weibo.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+
10
+ task :console do
11
+ exec "pry -r easy_weibo -I ./lib"
12
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "easy_weibo"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/easy_weibo/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "easy_weibo"
7
+ spec.version = EasyWeibo::VERSION
8
+ spec.authors = ["42up"]
9
+ spec.email = ["foobar@v2up.com"]
10
+
11
+ spec.summary = "An unofficial easy weibo sdk"
12
+ spec.description = "An unofficial easy weibo sdk"
13
+ spec.homepage = "https://github.com/42up/easy_weibo"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = spec.homepage
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "httpx"
33
+ spec.add_dependency "activesupport"
34
+ spec.add_dependency "http-form_data"
35
+
36
+ spec.add_development_dependency "bundler"
37
+ spec.add_development_dependency "rake"
38
+ spec.add_development_dependency "rspec"
39
+ spec.add_development_dependency "pry"
40
+ end
data/lib/easy_weibo.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "easy_weibo/version"
4
+ require "easy_weibo/client"
5
+ require "easy_weibo/configuration"
6
+
7
+ require "httpx"
8
+
9
+ module EasyWeibo
10
+ class Error < StandardError; end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ class << self
17
+ attr_writer :configuration
18
+
19
+ def configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def configure
24
+ yield(configuration)
25
+ end
26
+
27
+ def app_key
28
+ configuration.app_key
29
+ end
30
+
31
+ def app_secret
32
+ configuration.app_secret
33
+ end
34
+
35
+ def redirect_uri
36
+ configuration.redirect_uri
37
+ end
38
+
39
+ def root
40
+ File.dirname __dir__
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,65 @@
1
+ require "httpx"
2
+ require "http/form_data"
3
+ require "json"
4
+ require "active_support"
5
+
6
+ module EasyWeibo
7
+ class Client
8
+ OAUTH2_AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize"
9
+ OAUTH2_ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token"
10
+ STATUSES_SHARE_URL = "https://api.weibo.com/2/statuses/share.json"
11
+
12
+ attr_writer :token, :code
13
+
14
+ def initialize
15
+ @code = nil
16
+ @token = nil
17
+ end
18
+
19
+ # 构造授权地址,获取code
20
+ # https://open.weibo.com/wiki/Oauth2/authorize
21
+ def authorize_url
22
+ "#{OAUTH2_AUTHORIZE_URL}?redirect_uri=#{EasyWeibo.redirect_uri}&client_id=#{EasyWeibo.app_key}&display=wap"
23
+ end
24
+
25
+ # 获取token
26
+ # https://open.weibo.com/wiki/Oauth2/access_token
27
+ def access_token
28
+ raise "code is nil" if @code.blank?
29
+
30
+ payload = {
31
+ client_id: EasyWeibo.app_key,
32
+ client_secret: EasyWeibo.app_secret,
33
+ grant_type: "authorization_code",
34
+ code: @code,
35
+ redirect_uri: EasyWeibo.redirect_uri,
36
+ }
37
+
38
+ resp = HTTPX.post(OAUTH2_ACCESS_TOKEN_URL, params: payload)
39
+
40
+ r = ::JSON.parse(resp.body, quirks_mode: true)
41
+ yield r if block_given?
42
+ r
43
+ end
44
+
45
+ # 获取access_token
46
+ def token
47
+ @token ||= access_token["access_token"]
48
+ end
49
+
50
+ # 发布微博
51
+ def statuses_share(text, url, pic = nil)
52
+ # TODO: 抛出异常 必须做URLencode,内容不超过140个汉字
53
+ status = "#{text} #{url}"
54
+
55
+ payload = { status: status }
56
+ payload[:pic] = pic.is_a?(String) ? HTTP::FormData::File.new(pic) : pic unless pic.blank?
57
+
58
+ resp = HTTPX.plugin(:multipart).post(STATUSES_SHARE_URL, params: { access_token: token }, form: payload)
59
+
60
+ r = ::JSON.parse(resp.body, quirks_mode: true)
61
+ yield r if block_given?
62
+ r
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,11 @@
1
+ module EasyWeibo
2
+ class Configuration
3
+ attr_accessor :app_key, :app_secret, :redirect_uri
4
+
5
+ def initialize
6
+ @app_key = ""
7
+ @app_secret = ""
8
+ @redirect_uri = ""
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyWeibo
4
+ VERSION = "0.1.0"
5
+ end
data/test.jpg ADDED
Binary file
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_weibo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 42up
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpx
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: http-form_data
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: An unofficial easy weibo sdk
112
+ email:
113
+ - foobar@v2up.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - CHANGELOG.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - easy_weibo.gemspec
130
+ - lib/easy_weibo.rb
131
+ - lib/easy_weibo/client.rb
132
+ - lib/easy_weibo/configuration.rb
133
+ - lib/easy_weibo/version.rb
134
+ - test.jpg
135
+ homepage: https://github.com/42up/easy_weibo
136
+ licenses:
137
+ - MIT
138
+ metadata:
139
+ homepage_uri: https://github.com/42up/easy_weibo
140
+ source_code_uri: https://github.com/42up/easy_weibo
141
+ changelog_uri: https://github.com/42up/easy_weibo
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 2.3.0
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubygems_version: 3.1.4
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: An unofficial easy weibo sdk
161
+ test_files: []