lib/simple_twitter/client.rb in simple_twitter-2.0.0 vs lib/simple_twitter/client.rb in simple_twitter-2.1.0
- old
+ new
@@ -1,13 +1,13 @@
module SimpleTwitter
# Twitter API Client
class Client
- # @param bearer_token [String] This requires for API v2
- # @param api_key [String] This requires for API v1.1
- # @param api_secret_key [String] This requires for API v1.1
- # @param access_token [String] This requires for API v1.1
- # @param access_token_secret [String] This requires for API v1.1
+ # @param bearer_token [String] This requires for OAuth 2
+ # @param api_key [String] This requires for OAuth 1.0a
+ # @param api_secret_key [String] This requires for OAuth 1.0a
+ # @param access_token [String] This requires for OAuth 1.0a
+ # @param access_token_secret [String] This requires for OAuth 1.0a
def initialize(bearer_token: nil,
api_key: nil,
api_secret_key: nil,
access_token: nil,
access_token_secret: nil)
@@ -21,89 +21,112 @@
token_secret: access_token_secret,
}
end
end
- # @!method get(url, params: {}, json: {})
+ # @!method get(url, params: {}, json: {}, form: {})
# Call Twitter API with GET method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [Hash] parsed json data
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
- # @!method get_raw(url, params: {}, json: {})
+ # @!method get_raw(url, params: {}, json: {}, form: {})
# Call Twitter API with GET method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [HTTP::Response]
- # @!method post(url, params: {}, json: {})
+ # @!method post(url, params: {}, json: {}, form: {})
# Call Twitter API with POST method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [Hash] parsed json data
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
- # @!method post_raw(url, params: {}, json: {})
+ # @!method post_raw(url, params: {}, json: {}, form: {})
# Call Twitter API with POST method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [HTTP::Response]
- # @!method put(url, params: {}, json: {})
+ # @!method put(url, params: {}, json: {}, form: {})
# Call Twitter API with PUT method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [Hash] parsed json data
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
- # @!method put_raw(url, params: {}, json: {})
+ # @!method put_raw(url, params: {}, json: {}, form: {})
# Call Twitter API with PUT method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [HTTP::Response]
- # @!method delete(url, params: {}, json: {})
+ # @!method delete(url, params: {}, json: {}, form: {})
# Call Twitter API with DELETE method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [Hash] parsed json data
# @raise [SimpleTwitter::ClientError] Twitter API returned 4xx error
# @raise [SimpleTwitter::ServerError] Twitter API returned 5xx error
- # @!method delete_raw(url, params: {}, json: {})
+ # @!method delete_raw(url, params: {}, json: {}, form: {})
# Call Twitter API with DELETE method
# @param url [String]
# @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
# @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
# @return [HTTP::Response]
%i[get post put delete].each do |m|
class_eval <<~EOD
- def #{m}(url, params: {}, json: {})
- res = #{m}_raw(url, params: params, json: json)
+ 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: {})
- args = { params: params }
- args[:json] = json unless json.empty?
+ def #{m}_raw(url, params: {}, json: {}, form: {})
+ args = create_http_args(params: params, json: json, form: form)
http(:#{m}, url, params).#{m}(url, args)
end
EOD
end
private
+
+ # @param params [Hash] Send this arg as a query string. (e.g. `?name1=value1&name2=value2`)
+ # @param json [Hash] Send this arg as JSON request body with `Content-Type: application/json` header
+ # @param form [Hash] Send this arg as form-data request body with `Content-Type: multipart/form-data` header
+ # @return [Hash<Symbol, Object>]
+ 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
# @param method [Symbol]
# @param url [String]
# @param params [Hash<Symbol, String>]
# @return [HTTP::Client]