Class: SimpleTwitter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_twitter/client.rb,
sig/simple_twitter/client.rbs

Overview

Twitter API Client

Instance Method Summary collapse

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.

Examples:

Initialize with OAuth 2

client = SimpleTwitter::Client.new(bearer_token: "bearer_token")

Initialize with OAuth 1.0a

client = SimpleTwitter::Client.new(
  api_key: "api_key",
  api_secret_key: "api_secret_key",
  access_token: "access_token",
  access_token_secret: "access_token_secret",
)

Parameters:

  • bearer_token (String) (defaults to: nil)

    This requires for OAuth 2

  • api_key (String) (defaults to: nil)

    API Key (a.k.a Consumer Key). This requires for OAuth 1.0a

  • api_secret_key (String) (defaults to: nil)

    API Secret Key (a.k.a Consumer Secret). This requires for OAuth 1.0a

  • access_token (String) (defaults to: nil)

    Access Token. This requires for OAuth 1.0a

  • access_token_secret (String) (defaults to: nil)

    Access Token Secret. This requires for OAuth 1.0a

  • bearer_token: (string, nil) (defaults to: nil)
  • api_key: (string, nil) (defaults to: nil)
  • api_secret_key: (string, nil) (defaults to: nil)
  • access_token: (string, nil) (defaults to: nil)
  • access_token_secret: (string, nil) (defaults to: nil)


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

Parameters:

  • method (Symbol)
  • url (String)
  • params (Hash<Symbol, String>)

Returns:

  • (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>

Parameters:

  • params (Hash)

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash)

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash)

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped])
  • json: (Hash[Symbol, untyped])
  • form: (Hash[Symbol, untyped])

Returns:

  • (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

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash)

    parsed json data

Raises:



# File 'lib/simple_twitter/client.rb', line 94

#delete_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response

Call Twitter API with DELETE method

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (HTTP::Response)

See Also:



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

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash)

    parsed json data

Raises:



# File 'lib/simple_twitter/client.rb', line 37

#get_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response

Call Twitter API with GET method

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (HTTP::Response)

See Also:



# File 'lib/simple_twitter/client.rb', line 47

#http(method, url, params) ⇒ HTTP::Client

Parameters:

  • method (Symbol)
  • url (String)
  • params (Hash<Symbol, String>)

Returns:

  • (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

Returns:

  • (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.

Parameters:

  • res (HTTP::Response)

Returns:

  • (Hash)

    parsed json data

Raises:

See Also:



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

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash)

    parsed json data

Raises:



# File 'lib/simple_twitter/client.rb', line 56

#post_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response

Call Twitter API with POST method

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (HTTP::Response)

See Also:



# File 'lib/simple_twitter/client.rb', line 66

#put(url, params: {}, json: {}, form: {}) ⇒ Hash

Call Twitter API with PUT method

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash)

    parsed json data

Raises:



# File 'lib/simple_twitter/client.rb', line 75

#put_raw(url, params: {}, json: {}, form: {}) ⇒ HTTP::Response

Call Twitter API with PUT method

Parameters:

  • url (String)
  • params (Hash) (defaults to: {})

    Send this arg as a query string. (e.g. ?name1=value1&name2=value2)

  • json (Hash) (defaults to: {})

    Send this arg as JSON request body with Content-Type: application/json header

  • form (Hash) (defaults to: {})

    Send this arg as form-data request body with Content-Type: multipart/form-data header

  • params: (Hash[Symbol, untyped]) (defaults to: {})
  • json: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (HTTP::Response)

See Also:



# File 'lib/simple_twitter/client.rb', line 85