Class: SimpleTwitter::Client
- Inherits:
-
Object
- Object
- SimpleTwitter::Client
- Defined in:
- lib/simple_twitter/client.rb,
sig/simple_twitter/client.rbs
Overview
Twitter API Client
Instance Method Summary collapse
- #auth_header(method, url, params) ⇒ String
- #create_http_args(params:, json:, form:) ⇒ Hash<Symbol, Object>
-
#delete(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with DELETE method.
-
#delete_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with DELETE method.
-
#get(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with GET method.
-
#get_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with GET method.
- #http(method, url, params) ⇒ HTTP::Client
- #http_v6_or_later? ⇒ Boolean
-
#initialize(bearer_token: nil, api_key: nil, api_secret_key: nil, access_token: nil, access_token_secret: nil) ⇒ Client
constructor
A new instance of Client.
-
#parse_response(res) ⇒ Hash
Parsed json data.
-
#post(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with POST method.
-
#post_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with POST method.
-
#put(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with PUT method.
-
#put_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with PUT method.
Constructor Details
#initialize(bearer_token: nil, api_key: nil, api_secret_key: nil, access_token: nil, access_token_secret: nil) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/simple_twitter/client.rb', line 20 def initialize(bearer_token: nil, api_key: nil, api_secret_key: nil, access_token: nil, access_token_secret: nil) if bearer_token @bearer_token = bearer_token else @oauth_params = { consumer_key: api_key, consumer_secret: api_secret_key, token: access_token, token_secret: access_token_secret, } end end |
Instance Method Details
#auth_header(method, url, params) ⇒ String
167 168 169 170 171 172 173 |
# File 'lib/simple_twitter/client.rb', line 167 def auth_header(method, url, params) if @bearer_token "Bearer #{@bearer_token}" else SimpleOAuth::Header.new(method, url, params, @oauth_params).to_s end end |
#create_http_args(params:, json:, form:) ⇒ Hash<Symbol, Object>
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/simple_twitter/client.rb', line 138 def create_http_args(params:, json:, form:) args = { params: params, headers: { "User-Agent" => "simple_twitter v#{SimpleTwitter::VERSION} (https://github.com/yhara/simple_twitter)", }, } args[:json] = json unless json.empty? args[:form] = form unless form.empty? args end |
#delete(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with DELETE method
|
|
# File 'lib/simple_twitter/client.rb', line 94
|
#delete_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with DELETE method
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/simple_twitter/client.rb', line 113 %i[get post put delete].each do |m| class_eval <<~EOD, __FILE__, __LINE__ + 1 def #{m}(url, params: {}, json: {}, form: {}) res = #{m}_raw(url, params: params, json: json, form: form) parse_response(res) end def #{m}_raw(url, params: {}, json: {}, form: {}) args = create_http_args(params: params, json: json, form: form) if http_v6_or_later? http(:#{m}, url, params).#{m}(url, **args) else http(:#{m}, url, params).#{m}(url, args) end end EOD end |
#get(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with GET method
|
|
# File 'lib/simple_twitter/client.rb', line 37
|
#get_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with GET method
|
|
# File 'lib/simple_twitter/client.rb', line 47
|
#http(method, url, params) ⇒ HTTP::Client
159 160 161 |
# File 'lib/simple_twitter/client.rb', line 159 def http(method, url, params) HTTP.auth(auth_header(method, url, params)) end |
#http_v6_or_later? ⇒ Boolean
151 152 153 |
# File 'lib/simple_twitter/client.rb', line 151 def http_v6_or_later? Gem::Version.new(HTTP::VERSION) >= Gem::Version.new("6.0.0") end |
#parse_response(res) ⇒ Hash
Returns parsed json data.
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/simple_twitter/client.rb', line 180 def parse_response(res) case res.code.to_i / 100 when 4 raise ClientError, res when 5 raise ServerError, res end JSON.parse(res.to_s, symbolize_names: true) end |
#post(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with POST method
|
|
# File 'lib/simple_twitter/client.rb', line 56
|
#post_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with POST method
|
|
# File 'lib/simple_twitter/client.rb', line 66
|
#put(url, params: {}, json: {}, form: {}) ⇒ Hash
Call Twitter API with PUT method
|
|
# File 'lib/simple_twitter/client.rb', line 75
|
#put_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response
Call Twitter API with PUT method
|
|
# File 'lib/simple_twitter/client.rb', line 85
|