module MikuTwitter::Connect

OAuth関連

Constants

BAD_AUTHENTICATION_DATA
BASIC_AUTHENTICATION_IS_NOT_SUPPORTED
IDENTITY_SEED
INTERNAL_ERROR
INVALID_OR_EXPIRED_TOKEN
INVALID_REQUEST
OVER_CAPACITY
PAGE_DOES_NOT_EXIST
RATE_LIMIT_EXCEEDED
TIMESTAMP_TOOLATE

Attributes

a_secret[RW]
a_token[RW]
consumer_key[RW]
consumer_secret[RW]
oauth_url[RW]

Public Class Methods

new(*a, &b) click to toggle source
Calls superclass method
# File core/lib/mikutwitter/connect.rb, line 25
def initialize(*a, &b)
  @oauth_url = 'https://twitter.com'
  super(*a, &b)
end

Public Instance Methods

access_token(url=@oauth_url) click to toggle source
# File core/lib/mikutwitter/connect.rb, line 38
def access_token(url=@oauth_url)
  OAuth::AccessToken.new(consumer(url), a_token, a_secret) end
account_identity() click to toggle source

キャッシュのキーなどで使う、アカウント毎の一意なID

# File core/lib/mikutwitter/connect.rb, line 31
def account_identity
  Digest::MD5.hexdigest([consumer_key, consumer_secret, a_token, a_secret, IDENTITY_SEED].join) end
consumer(url=oauth_url) click to toggle source
# File core/lib/mikutwitter/connect.rb, line 34
def consumer(url=oauth_url)
  OAuth::Consumer.new(consumer_key, consumer_secret,
                      :site => url) end
query_with_oauth!(method, url, options = {}) click to toggle source

OAuth経由でurlを叩く

Args

method

メソッド。:get, :post, :put, :delete の何れか

url

叩くURL

options

API引数。ただし、以下のキーは特別扱いされ、API引数からは除外される

:head

HTTPリクエストヘッダ(Hash)

Return

戻り値(HTTPResponse)

Exceptions

TimeoutError

# File core/lib/mikutwitter/connect.rb, line 55
def query_with_oauth!(method, url, options = {})
  if [:get, :delete].include? method
    path = url + get_args(options)
    res = access_token.__send__(method, path, options[:head])
  else
    path = url
    query_args = options.melt
    head = options[:head]
    query_args.delete(:head)
    res = access_token.__send__(method, path, query_args, head) end
  if res.is_a? Net::HTTPResponse
    case res.code
    when '200'
    when '401'
      begin
        errors = (JSON.parse(res.body)["errors"] rescue nil)
        errors.each { |error|
          if [INVALID_OR_EXPIRED_TOKEN].include? error["code"]
            atoken = authentication_failed_action(method, url, options, res)
            return query_with_oauth!(method, url, options) if atoken end }
      rescue Exception => e
        warn e end
    when '429'
      raise MikuTwitter::RateLimitError.new("Rate limit #{url}", nil)
    end
  end
  res
end
request_oauth_token() click to toggle source
# File core/lib/mikutwitter/connect.rb, line 41
def request_oauth_token
  consumer.get_request_token end