Class: Rubytter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytter.rb,
lib/rubytter/connection.rb

Direct Known Subclasses

OAuthRubytter

Defined Under Namespace

Classes: APIError, Connection

Constant Summary collapse

VERSION =
'0.9.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login = nil, password = nil, options = {}) ⇒ Rubytter

Returns a new instance of Rubytter.



25
26
27
28
29
30
31
32
33
# File 'lib/rubytter.rb', line 25

def initialize( = nil, password = nil, options = {})
  @login = 
  @password = password
  @host = options[:host] || 'twitter.com'
  @header = {'User-Agent' => "Rubytter/#{VERSION} (http://github.com/jugyo/rubytter)"}
  @header.merge!(options[:header]) if options[:header]
  @app_name = options[:app_name]
  @connection = Connection.new(options)
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



23
24
25
# File 'lib/rubytter.rb', line 23

def header
  @header
end

#hostObject

Returns the value of attribute host.



23
24
25
# File 'lib/rubytter.rb', line 23

def host
  @host
end

#loginObject (readonly)

Returns the value of attribute login.



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

def 
  @login
end

Class Method Details

.api_settingsObject



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
# File 'lib/rubytter.rb', line 35

def self.api_settings
  # method name             path for API                    http method
  "
    update_status           /statuses/update                post
    remove_status           /statuses/destroy/%s            delete
    public_timeline         /statuses/public_timeline
    friends_timeline        /statuses/friends_timeline
    replies                 /statuses/replies
    user_timeline           /statuses/user_timeline/%s
    show                    /statuses/show/%s
    friends                 /statuses/friends/%s
    followers               /statuses/followers/%s
    user                    /users/show/%s
    direct_messages         /direct_messages
    sent_direct_messages    /direct_messages/sent
    send_direct_message     /direct_messages/new            post
    remove_direct_message   /direct_messages/destroy/%s     delete
    follow                  /friendships/create/%s          post
    leave                   /friendships/destroy/%s         delete
    friendship_exists       /friendships/exists
    followers_ids           /followers/ids/%s
    friends_ids             /friends/ids/%s
    favorites               /favorites
    favorite                /favorites/create/%s            post
    remove_favorite         /favorites/destroy/%s           delete
    verify_credentials      /account/verify_credentials     get
    end_session             /account/end_session            post
    update_delivery_device  /account/update_delivery_device post
    update_profile_colors   /account/update_profile_colors  post
    limit_status            /account/rate_limit_status
    update_profile          /account/update_profile         post
    enable_notification     /notifications/follow/%s        post
    disable_notification    /notifications/leave/%s         post
    block                   /blocks/create/%s               post
    unblock                 /blocks/destroy/%s              delete
  ".strip.split("\n").map{|line| line.strip.split(/\s+/)}
end

.search_result_to_hash(json) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rubytter.rb', line 134

def self.search_result_to_hash(json)
  {
    'id' => json['id'],
    'text' => json['text'],
    'source' => json['source'],
    'created_at' => json['created_at'],
    'in_reply_to_user_id' => json['to_usre_id'],
    'in_reply_to_screen_name' => json['to_usre'],
    'in_reply_to_status_id' => nil,
    'user' => {
      'id' => json['from_user_id'],
      'name' => nil,
      'screen_name' => json['from_user'],
      'profile_image_url' => json['profile_image_url']
    }
  }
end

.structize(data) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rubytter.rb', line 175

def self.structize(data)
  case data
  when Array
    data.map{|i| structize(i)}
  when Hash
    class << data
      def id
        self[:id]
      end

      def method_missing(name, *args)
        self[name]
      end

      def symbolize_keys!
        each do |key, value|
          self[(key.to_sym rescue key) || key] = value
        end

        self
      end
    end

    data.keys.each do |k|
      case k
      when String, Symbol # String しかまず来ないだろうからこの判定はいらない気もするなぁ
        data[k] = structize(data[k])
      else
        data.delete(k)
      end
    end

    data.symbolize_keys!
  else
    case data
    when String
      CGI.unescapeHTML(data) # ここで unescapeHTML すべきか悩むところではある
    else
      data
    end
  end
end

.to_param_str(hash) ⇒ Object

Raises:

  • (ArgumentError)


218
219
220
221
# File 'lib/rubytter.rb', line 218

def self.to_param_str(hash)
  raise ArgumentError, 'Argument must be a Hash object' unless hash.is_a?(Hash)
  hash.to_a.map{|i| i[0].to_s + '=' + CGI.escape(i[1].to_s) }.join('&')
end

Instance Method Details

#__update_statusObject



91
# File 'lib/rubytter.rb', line 91

alias_method :__update_status, :update_status

#create_request(req, basic_auth = true) ⇒ Object



169
170
171
172
173
# File 'lib/rubytter.rb', line 169

def create_request(req, basic_auth = true)
  @header.each {|k, v| req.add_field(k, v) }
  req.basic_auth(@login, @password) if basic_auth
  req
end

#direct_message(user, text, params = {}) ⇒ Object



101
102
103
# File 'lib/rubytter.rb', line 101

def direct_message(user, text, params = {})
  send_direct_message(params.merge({:user => user, :text => text}))
end

#get(path, params = {}) ⇒ Object



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

def get(path, params = {})
  path += '.json'
  param_str = '?' + self.class.to_param_str(params)
  path = path + param_str unless param_str.empty?
  req = create_request(Net::HTTP::Get.new(path))
  self.class.structize(http_request(@host, req))
end

#http_request(host, req, param_str = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rubytter.rb', line 152

def http_request(host, req, param_str = nil)
  res = @connection.start(host) do |http|
    if param_str
      http.request(req, param_str)
    else
      http.request(req)
    end
  end
  json_data = JSON.parse(res.body)
  case res.code
  when "200"
    json_data
  else
    raise APIError.new(json_data['error'], res)
  end
end

#post(path, params = {}) ⇒ Object Also known as: delete



113
114
115
116
117
118
# File 'lib/rubytter.rb', line 113

def post(path, params = {})
  path += '.json'
  param_str = self.class.to_param_str(params)
  req = create_request(Net::HTTP::Post.new(path))
  self.class.structize(http_request(@host, req, param_str))
end

#search(query, params = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rubytter.rb', line 121

def search(query, params = {})
  path = '/search.json'
  param_str = '?' + self.class.to_param_str(params.merge({:q => query}))
  path = path + param_str unless param_str.empty?
  req = create_request(Net::HTTP::Get.new(path), false)
  json_data = http_request("search.#{@host}", req)
  self.class.structize(
    json_data['results'].map do |result|
      self.class.search_result_to_hash(result)
    end
  )
end

#update(status, params = {}) ⇒ Object



97
98
99
# File 'lib/rubytter.rb', line 97

def update(status, params = {})
  update_status(params.merge({:status => status}))
end

#update_status(params = {}) ⇒ Object



92
93
94
95
# File 'lib/rubytter.rb', line 92

def update_status(params = {})
  params[:source] = @app_name if @app_name
  __update_status(params)
end