simple_twitter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +93 -0
- data/Rakefile +2 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/simple_twitter.rb +59 -0
- data/lib/simple_twitter/version.rb +3 -0
- data/simple_twitter.gemspec +30 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2736729c95b2f5def88674bda64a6fecd397a5f422c829ab8b301ac4a36f216c
|
4
|
+
data.tar.gz: 530c600f14041e91c7476b1179bf2069f8d35076175d10089b07db642b13c3fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e216e514b9bcdfd91e600e07137c409b1bd3d84c4eb045a7988b30a29c4b4dea4bb05f39dddf309295c104a640420bd43a7cf5f7f2702b86ea05f13b2df0acda
|
7
|
+
data.tar.gz: '036388ae7887f3af5b3b625f15dd6ab7e5241502e1af4257ae1b59ed92b3082e406f9246f623d711a9791af9de851d3f0b5870badde6a4e41e4673df75308a06'
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Yutaka HARA
|
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,93 @@
|
|
1
|
+
# simple_twitter
|
2
|
+
|
3
|
+
Dead simple Twitter API client. Supports both v1 and v2
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'simple_twitter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install simple_twitter
|
20
|
+
|
21
|
+
## Example
|
22
|
+
|
23
|
+
```rb
|
24
|
+
require 'simple_twitter'
|
25
|
+
|
26
|
+
client = SimpleTwitter::Client.new(bearer_token: "...")
|
27
|
+
pp client.get("https://api.twitter.com/2/tweets",
|
28
|
+
ids: "1302127884039909376,1369885448319889409")
|
29
|
+
```
|
30
|
+
|
31
|
+
Result:
|
32
|
+
|
33
|
+
```
|
34
|
+
{:data=>
|
35
|
+
[{:id=>"1302127884039909376",
|
36
|
+
:text=>
|
37
|
+
"We conclude RubyKaigi Takeout 2020. We hope we can meet in-person, safely at RubyKaigi 2021 in Mie! Thank you all for tuning in. #rubykaigi"},
|
38
|
+
{:id=>"1369885448319889409",
|
39
|
+
:text=>
|
40
|
+
"RubyKaigi 2021 is going online again: RubyKaigi Takeout 2021 will happen this fall. https://t.co/Fv1PlvmUHh"}]}
|
41
|
+
```
|
42
|
+
|
43
|
+
As you see hash keys are converted into symbols (Note that strings as values are not converted.)
|
44
|
+
|
45
|
+
### Call API on user context
|
46
|
+
|
47
|
+
Some operations (eg. posting a tweet) needs OAuth instead of `bearer_token`.
|
48
|
+
|
49
|
+
```rb
|
50
|
+
config = (load from yaml or something)
|
51
|
+
client = SimpleTwitter::Client.new(
|
52
|
+
api_key: config[:api_key],
|
53
|
+
api_secret_key: config[:api_secret_key],
|
54
|
+
access_token: config[:access_token],
|
55
|
+
access_token_secret: config[:access_token_secret],
|
56
|
+
)
|
57
|
+
pp client.post("https://api.twitter.com/1.1/statuses/update.json",
|
58
|
+
status: "Test.")
|
59
|
+
```
|
60
|
+
|
61
|
+
You can get the access_token and access_token_secret for your own at the Twitter Developer Portal. For other users, you need to get them via OAuth (out of scope of this gem.)
|
62
|
+
|
63
|
+
### Advanced
|
64
|
+
|
65
|
+
If you want the raw json string or use streaming API, use `get_raw`, `post_raw`, etc. which returns `HTTP::Response` of the [http gem](https://github.com/httprb/http).
|
66
|
+
|
67
|
+
```rb
|
68
|
+
res = client.get_raw("https://api.twitter.com/2/tweets/sample/stream")
|
69
|
+
p res #=> #<HTTP::Response ...>
|
70
|
+
loop do
|
71
|
+
puts res.body.readpartial
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
### Hint
|
76
|
+
|
77
|
+
Some API parameters has `.` in its name (eg. `tweet.fields`.) Did you know that in Ruby you can include `.` in a hash key if quoted? :-)
|
78
|
+
|
79
|
+
```rb
|
80
|
+
tweets = @client.get("https://api.twitter.com/2/users/#{id}/tweets", {
|
81
|
+
expansions: "author_id",
|
82
|
+
max_results: 100,
|
83
|
+
"tweet.fields": "author_id,created_at,referenced_tweets,text",
|
84
|
+
})
|
85
|
+
```
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/naclyhara/simple_twitter.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
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 "simple_twitter"
|
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,59 @@
|
|
1
|
+
require 'http'
|
2
|
+
require 'simple_oauth'
|
3
|
+
|
4
|
+
module SimpleTwitter
|
5
|
+
class Client
|
6
|
+
def initialize(bearer_token: nil,
|
7
|
+
api_key: nil,
|
8
|
+
api_secret_key: nil,
|
9
|
+
access_token: nil,
|
10
|
+
access_token_secret: nil)
|
11
|
+
if bearer_token
|
12
|
+
@bearer_token = bearer_token
|
13
|
+
else
|
14
|
+
@oauth_params = {
|
15
|
+
consumer_key: api_key,
|
16
|
+
consumer_secret: api_secret_key,
|
17
|
+
token: access_token,
|
18
|
+
token_secret: access_token_secret,
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
%i[get post put delete].each do |m|
|
24
|
+
class_eval <<~EOD
|
25
|
+
# @return [Object] parsed json data
|
26
|
+
def #{m}(url, params={})
|
27
|
+
JSON.parse(#{m}_raw(url, params).to_s, symbolize_names: true)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [HTTP::Response]
|
31
|
+
def #{m}_raw(url, params={})
|
32
|
+
http(:#{m}, url, params).#{m}(url, params: params)
|
33
|
+
end
|
34
|
+
EOD
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# @param method [Symbol]
|
40
|
+
# @param url [String]
|
41
|
+
# @param params [Hash<Symbol, String>]
|
42
|
+
# @return [HTTP::Request]
|
43
|
+
def http(method, url, params)
|
44
|
+
HTTP.auth(auth_header(method, url, params))
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param method [Symbol]
|
48
|
+
# @param url [String]
|
49
|
+
# @param params [Hash<Symbol, String>]
|
50
|
+
# @return [String]
|
51
|
+
def auth_header(method, url, params)
|
52
|
+
if @bearer_token
|
53
|
+
"Bearer #{@bearer_token}"
|
54
|
+
else
|
55
|
+
SimpleOAuth::Header.new(method, url, params, @oauth_params).to_s
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative "lib/simple_twitter/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "simple_twitter"
|
5
|
+
spec.version = SimpleTwitter::VERSION
|
6
|
+
spec.authors = ["Yutaka HARA"]
|
7
|
+
spec.email = ["yutaka.hara+github@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Dead simple client for Twitter API v1/v2"
|
10
|
+
spec.description = "Dead simple client for Twitter API (supports both v1 and v2)"
|
11
|
+
spec.homepage = "https://github.com/yhara/simple_twitter"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "http", "~> 4"
|
29
|
+
spec.add_dependency "simple_oauth", "~> 0.3.1"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_twitter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yutaka HARA
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: simple_oauth
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.3.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.3.1
|
41
|
+
description: Dead simple client for Twitter API (supports both v1 and v2)
|
42
|
+
email:
|
43
|
+
- yutaka.hara+github@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CHANGELOG.md
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- lib/simple_twitter.rb
|
57
|
+
- lib/simple_twitter/version.rb
|
58
|
+
- simple_twitter.gemspec
|
59
|
+
homepage: https://github.com/yhara/simple_twitter
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata:
|
63
|
+
homepage_uri: https://github.com/yhara/simple_twitter
|
64
|
+
source_code_uri: https://github.com/yhara/simple_twitter
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 2.3.0
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.2.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Dead simple client for Twitter API v1/v2
|
84
|
+
test_files: []
|