Class: Atig::BasicTwitter

Inherits:
Object
  • Object
show all
Defined in:
lib/atig/basic_twitter.rb

Overview

from tig.rb

Direct Known Subclasses

SearchTwitter, Twitter

Defined Under Namespace

Classes: APIFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, api_base) ⇒ BasicTwitter

Returns a new instance of BasicTwitter.



15
16
17
18
19
20
21
22
# File 'lib/atig/basic_twitter.rb', line 15

def initialize(context, api_base)
  @log   = context.log
  @opts  = context.opts
  @api_base = api_base
  @http  = Atig::Http.new @log

  @limit = @remain = 150
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



11
12
13
# File 'lib/atig/basic_twitter.rb', line 11

def limit
  @limit
end

#remainObject (readonly)

Returns the value of attribute remain.



11
12
13
# File 'lib/atig/basic_twitter.rb', line 11

def remain
  @remain
end

#resetObject (readonly)

Returns the value of attribute reset.



11
12
13
# File 'lib/atig/basic_twitter.rb', line 11

def reset
  @reset
end

Class Method Details

.http_methods(*methods) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/atig/basic_twitter.rb', line 98

def self.http_methods(*methods)
  methods.each do |m|
    self.module_eval <<END
      def #{m}(path, query = {}, opts = {})
        opts.update( :method => :#{m})
        api path, query, opts
      end
END
  end
end

Instance Method Details

#api(path, query = {}, opts = {}) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/atig/basic_twitter.rb', line 24

def api(path, query = {}, opts = {})
  path.sub!(%r{\A/+}, "")

  uri = api_base
  uri.path += path
  uri.path += ".json" if path != "users/username_available"
  uri.query = query.to_query_str unless query.empty?

  header      = {}

  begin
    ret = request(uri, opts)
  rescue OpenSSL::SSL::SSLError => e
    @log.error e.inspect
    raise e.inspect
  end

  if ret["X-RateLimit-Limit"] then
    hourly_limit = ret["X-RateLimit-Limit"].to_i
    unless hourly_limit.zero?
      if @limit != hourly_limit
        msg = "The rate limit per hour was changed: #{@limit} to #{hourly_limit}"
        @log.info msg
        @limit = hourly_limit
      end
    end
  end

  if ret["X-RateLimit-Remaining"] then
    @remain = ret["X-RateLimit-Remaining"].to_i
    @log.debug "IP based limit: #{@remain}"
  end

  if ret["X-RateLimit-Reset"] then
    @reset = ret["X-RateLimit-Reset"].to_i
    @log.debug "RateLimit Reset: #{@reset}"
  end

  case ret
  when Net::HTTPOK # 200
    # Avoid Twitter's invalid JSON
    json = ret.body.strip.sub(/\A(?:false|true)\z/, "[\\&]")

    res = JSON.parse(json)
    if res.is_a?(Hash) && res["error"] # and not res["response"]
      if @error != res["error"]
        @error = res["error"]
        log @error
      end
      raise APIFailed, res["error"]
    end

    TwitterStruct.make(res)
  when Net::HTTPNoContent,  # 204
    Net::HTTPNotModified # 304
    []
  when Net::HTTPBadRequest # 400: exceeded the rate limitation
    if ret.key?("X-RateLimit-Reset")
      @log.info "waiting for rate limit reset"
      s = ret["X-RateLimit-Reset"].to_i - Time.now.to_i
      if s > 0
        sleep (s > 60 * 10) ? 60 * 10 : s # 10 分に一回はとってくるように
      end
    end
    raise APIFailed, "#{ret.code}: #{ret.message}"
  when Net::HTTPUnauthorized # 401
    raise APIFailed, "#{ret.code}: #{ret.message}"
  else
    raise APIFailed, "Server Returned #{ret.code} #{ret.message}"
  end
rescue Errno::ETIMEDOUT, JSON::ParserError, IOError, Timeout::Error, Errno::ECONNRESET => e
  raise APIFailed, e.inspect
end