Class: Rubytter

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

Defined Under Namespace

Classes: APIError, Connection

Constant Summary collapse

APP_NAME =
'Rubytter'
VERSION =
'0.4.7'
HOMEPAGE =
'http://github.com/jugyo/rubytter'

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.



19
20
21
22
23
24
25
# File 'lib/rubytter.rb', line 19

def initialize( = nil, password = nil, options = {})
  @login = 
  @password = password
  @host = options[:host] || 'twitter.com'
  @header = options[:header] || {'User-Agent' => "#{APP_NAME}/#{VERSION} (#{HOMEPAGE})"}
  @connection = Connection.new(options)
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



17
18
19
# File 'lib/rubytter.rb', line 17

def header
  @header
end

#hostObject

Returns the value of attribute host.



17
18
19
# File 'lib/rubytter.rb', line 17

def host
  @host
end

#loginObject (readonly)

Returns the value of attribute login.



16
17
18
# File 'lib/rubytter.rb', line 16

def 
  @login
end

Class Method Details

.api_settingsObject



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

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

Instance Method Details

#create_request(req, basic_auth = true) ⇒ Object



133
134
135
136
137
# File 'lib/rubytter.rb', line 133

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



87
88
89
# File 'lib/rubytter.rb', line 87

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

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



99
100
101
102
103
104
105
# File 'lib/rubytter.rb', line 99

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

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubytter.rb', line 116

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
  struct = json_to_struct(JSON.parse(res.body))
  case res.code
  when "200"
    struct
  else
    raise APIError, struct.error
  end
end

#json_to_struct(json) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rubytter.rb', line 139

def json_to_struct(json)
  case json
  when Array
    json.map{|i| json_to_struct(i)}
  when Hash
    struct_values = {}
    json.each do |k, v|
      case k
      when String, Symbol
        struct_values[k.to_sym] = json_to_struct(v)
      end
    end
    unless struct_values.empty?
      Struct.new(*struct_values.keys).new(*struct_values.values)
    else
      nil
    end
  else
    json
  end
end

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



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

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

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



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

def search(query, params = {})
  path = '/search.json'
  param_str = '?' + to_param_str(params.merge({:q => query}))
  path = path + param_str unless param_str.empty?
  req = create_request(Net::HTTP::Get.new(path), false)
  http_request("search.#{@host}", req)
end

#to_param_str(hash) ⇒ Object

Raises:

  • (ArgumentError)


161
162
163
164
# File 'lib/rubytter.rb', line 161

def 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

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



83
84
85
# File 'lib/rubytter.rb', line 83

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