Class: SimpleTwitter::Client

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

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



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

#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

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

Returns:

  • (HTTP::Response)

See Also:



113
114
115
116
117
118
119
120
121
122
123
124
125
# 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)
      http(:#{m}, url, params).#{m}(url, args)
    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

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

Returns:

  • (HTTP::Response)

See Also:



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

#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

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

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

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

Returns:

  • (HTTP::Response)

See Also:



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