Class: Termtter::RubytterProxy

Inherits:
Object
  • Object
show all
Includes:
Hookable
Defined in:
lib/plugins/basic.rb,
lib/plugins/mongo.rb,
lib/plugins/whale.rb,
lib/plugins/list_switch.rb,
lib/termtter/rubytter_proxy.rb

Defined Under Namespace

Classes: FrequentAccessError, LimitManager

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hookable

included

Constructor Details

#initialize(*args) ⇒ RubytterProxy

Returns a new instance of RubytterProxy.



13
14
15
16
17
# File 'lib/plugins/basic.rb', line 13

def initialize(access_token, twitter_option)
  user_name = config.plugins.basic.user_name
  password = config.plugins.basic.password
  @rubytter = Rubytter.new(user_name, password, twitter_option)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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
# File 'lib/termtter/rubytter_proxy.rb', line 20

def method_missing(method, *args, &block)
  return super if !@rubytter.respond_to?(method)
  result = nil
  begin
    modified_args = args
    hooks = self.class.get_hooks("pre_#{method}")
    hooks.each do |hook|
      modified_args = hook.call(*modified_args)
    end

    from = Time.now if Termtter::Client.logger.debug?
    Termtter::Client.logger.debug {
      "rubytter_proxy: #{method}(#{modified_args.inspect[1...-1]})"
    }
    result = call_rubytter_or_use_cache(method, *modified_args, &block)
    Termtter::Client.logger.debug {
      "rubytter_proxy: #{method}(#{modified_args.inspect[1...-1]}), " +
        "%.2fsec" % (Time.now - from)
    }

    self.class.call_hooks("post_#{method}", *args)
  rescue HookCanceled
  rescue TimeoutError => e
    Termtter::Client.logger.debug {
      "rubytter_proxy: #{method}(#{modified_args.inspect[1...-1]}) " +
        "#{e.message} #{'%.2fsec' % (Time.now - from)}"
    }
    raise e
  rescue => e
    Termtter::Client.logger.debug {
      "rubytter_proxy: #{method}(#{modified_args.inspect[1...-1]}) #{e.message}"
    }
    raise e
  end
  result
end

Instance Attribute Details

#rubytterObject (readonly)

Returns the value of attribute rubytter.



13
14
15
# File 'lib/termtter/rubytter_proxy.rb', line 13

def rubytter
  @rubytter
end

#safe_modeObject

Returns the value of attribute safe_mode.



112
113
114
# File 'lib/termtter/rubytter_proxy.rb', line 112

def safe_mode
  @safe_mode
end

Instance Method Details

#access_tokenObject

XXX: these methods should be in oauth_rubytter



171
172
173
# File 'lib/termtter/rubytter_proxy.rb', line 171

def access_token
  @rubytter.instance_variable_get(:@access_token)
end

#cached_status(status_id) ⇒ Object



104
105
106
107
108
# File 'lib/plugins/mongo.rb', line 104

def cached_status(status_id)
  status = Termtter::Client.memory_cache.get(['status', status_id].join('-'))
  status ||= Termtter::Client.mongo_db.collection('status').find_one({'id' => status_id.to_i})
  Termtter::ActiveRubytter.new(status) if status
end

#cached_user(screen_name_or_id) ⇒ Object



84
85
86
87
88
89
# File 'lib/termtter/rubytter_proxy.rb', line 84

def cached_user(screen_name_or_id)
  user =
    Termtter::Client.memory_cache.get(
      ['user', Termtter::Client.normalize_as_user_name(screen_name_or_id.to_s)].join('-'))
  ActiveRubytter.new(user) if user
end

#call_rubytter_or_use_cache(method, *args, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/termtter/rubytter_proxy.rb', line 57

def call_rubytter_or_use_cache(method, *args, &block)
  case method
  when :show
    status = cached_status(args[0])
    unless status
      status = call_rubytter(method, *args, &block)
      store_status_cache(status)
    end
    status
  when :user
    user = cached_user(args[0])
    unless user
      user = call_rubytter(method, *args, &block)
      store_user_cache(user)
    end
    user
  when :home_timeline, :user_timeline, :friends_timeline, :search
    statuses = call_rubytter(method, *args, &block)
    statuses.each do |status|
      store_status_cache(status)
    end
    statuses
  else
    call_rubytter(method, *args, &block)
  end
end

#call_rubytter_with_list_switch(method, *args, &block) ⇒ Object Also known as: call_rubytter



22
23
24
# File 'lib/plugins/list_switch.rb', line 22

def call_rubytter_with_list_switch(method, *args, &block)
  Termtter::Plugins::ListSwitch.call_rubytter(self, method, *args, &block)
end

#call_rubytter_without_list_switchObject



21
# File 'lib/plugins/list_switch.rb', line 21

alias_method :call_rubytter_without_list_switch, :call_rubytter

#consumer_tokenObject



175
176
177
# File 'lib/termtter/rubytter_proxy.rb', line 175

def consumer_token
  access_token.consumer
end

#current_limitObject



122
123
124
# File 'lib/termtter/rubytter_proxy.rb', line 122

def current_limit
  @limit_manager ||= LimitManager.new(@rubytter)
end

#error_html_message_origObject



4
# File 'lib/plugins/whale.rb', line 4

alias_method :error_html_message_orig, :error_html_message

#error_html_message_whale(e) ⇒ Object Also known as: error_html_message



6
7
8
9
10
11
12
# File 'lib/plugins/whale.rb', line 6

def error_html_message_whale(e)
  if %r'Twitter / Over capacity' =~ e.message
    WHALE
  else
    error_html_message_orig(e)
  end
end

#safeObject



113
114
115
116
117
118
119
120
# File 'lib/termtter/rubytter_proxy.rb', line 113

def safe
  new_instance = self.class.new(@rubytter)
  new_instance.safe_mode = true
  self.instance_variables.each do |v|
    new_instance.instance_variable_set(v, self.instance_variable_get(v))
  end
  new_instance
end

#store_status_cache(status) ⇒ Object



97
98
99
100
101
# File 'lib/termtter/rubytter_proxy.rb', line 97

def store_status_cache(status)
  Termtter::Client.memory_cache.set(
    ['status', status.id].join('-'), status.to_hash, 3600 * 24 * 14)
  store_user_cache(status.user)
end

#store_user_cache(user) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/termtter/rubytter_proxy.rb', line 103

def store_user_cache(user)
  Termtter::Client.memory_cache.set(
    ['user', user.id.to_i].join('-'),
    user.to_hash, 3600 * 24)
  Termtter::Client.memory_cache.set(
    ['user', Termtter::Client.normalize_as_user_name(user.screen_name)].join('-'),
    user.to_hash, 3600 * 24)
end