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



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

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.



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

def rubytter
  @rubytter
end

#safe_modeObject

Returns the value of attribute safe_mode.



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

def safe_mode
  @safe_mode
end

Instance Method Details

#access_tokenObject

XXX: these methods should be in oauth_rubytter



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

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



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

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



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

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



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

def consumer_token
  access_token.consumer
end

#current_limitObject



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

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



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

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



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

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



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

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