File: authentication_failed_action.rb

package info (click to toggle)
mikutter 3.8.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,544 kB
  • sloc: ruby: 20,548; sh: 99; makefile: 19
file content (74 lines) | stat: -rw-r--r-- 3,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-

require_relative "basic"
require_relative "utils"

# OAuthに失敗した時の処理
module MikuTwitter::AuthenticationFailedAction
  class << self
    extend Gem::Deprecate
    # Twitterから401が帰ってきた時に、OAuthトークンを取得するためのブロック
    # &callback を登録する。
    # ==== Args
    # [&callback(twitter, method, url, options, res)] Twitter のOAuth トークンが切れた時に呼び出される。以下の引数を取る。
    #   [twitter] 401が返されたリクエストを発行したMikuTwitterのインスタンス
    #   [method] HTTPメソッド。:get, :post, :put, :delete のいずれか
    #   [url] APIのURL
    #   [options] APIの引数
    #   [res] API問い合わせの結果(Net::HTTPResponse)
    # ==== Return
    # 登録したProcオブジェクト
    def register(&callback)
      @authentication_failed_action = callback end

    alias :regist :register
    deprecate :regist, "register", 2016, 12

    # register_authentication_failed_action で登録されたProcを返す。
    # 何も登録されていない時は、abortするProcを返す。
    # ==== Return
    # 登録されたProcオブジェクト
    def get
      @authentication_failed_action ||= lambda{ |t, m, u, o, r| warn((JSON.parse(r.body)["error"] rescue 'OAuth error')); abort } end

    def lock
      @lock ||= Mutex.new end
  end

  # OAuthトークンを最取得するためのブロックを呼び出す。
  # ==== Args
  # [method] HTTPメソッド。:get, :post, :put, :delete のいずれか
  # [url] APIのURL
  # [options] APIの引数
  # [res] API問い合わせの結果(Net::HTTPResponse)
  def authentication_failed_action(method, url, options, res)
    failed_token, failed_secret = self.a_token, self.a_secret
    MikuTwitter::AuthenticationFailedAction.lock.synchronize{
      if failed_token == self.a_token and failed_secret == self.a_secret
        result = MikuTwitter::AuthenticationFailedAction.get.call(self, method, url, options, res)
        if(result and 2 == result.size)
          self.a_token, self.a_secret = *result
          UserConfig[:twitter_token] = self.a_token
          UserConfig[:twitter_secret] = self.a_secret
          return *result end
      else
        return self.a_token, self.a_secret end } end

end

# デフォルトの認証メソッド: ターミナルでPINコードを入力させる
MikuTwitter::AuthenticationFailedAction.register{ |service, method, url, options, res|
  begin
    request_token = service.request_oauth_token
    puts "go to #{request_token.authorize_url}"
    print "Authorized number is:"
    $stdout.flush
    access_token = request_token.get_access_token(:oauth_token => request_token.token,
                                                  :oauth_verifier => STDIN.gets.chomp)
    [access_token.token, access_token.secret]
  rescue Timeout::Error, StandardError => e
    error('invalid number')
  end
}

class MikuTwitter; include MikuTwitter::AuthenticationFailedAction end