Skip to content

Commit

Permalink
Refactored POSTs to use a common method
Browse files Browse the repository at this point in the history
POSTs to twitter now use a single method, post(url,body,headers), which
also JSON parses the response.
  • Loading branch information
namelessjon authored and moomerman committed Dec 29, 2009
1 parent b0208a9 commit d13e081
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 32 deletions.
7 changes: 3 additions & 4 deletions lib/twitter_oauth/account.rb
Expand Up @@ -22,21 +22,20 @@ def rate_limit_status
# Returns extended user info object.
def update_profile_background_image(image, tile = false)
body, headers = http_multipart_data({:image => image, :tile => tile})
oauth_response = access_token.post('/account/update_profile_background_image.json', body, headers)
post('/account/update_profile_background_image.json', body, headers)
end

# Updates profile avatar image. Takes a File object which should be an image.
# Returns extended user info object.
def update_profile_image(image)
body, headers = http_multipart_data({:image => image})
oauth_response = access_token.post('/account/update_profile_image.json', body, headers)
post('/account/update_profile_image.json', body, headers)
end

# colors hash must contain at least one or more of the following keys :profile_background_color, :profile_text_color, :profile_link_color, :profile_sidebar_fill_color, :profile_sidebar_border_color
# returns extended user info object.
def update_profile_colors(colors)
oauth_response = access_token.post('/account/update_profile_colors.json', colors)
JSON.parse(oauth_response.body)
post('/account/update_profile_colors.json', colors)
end

end
Expand Down
8 changes: 3 additions & 5 deletions lib/twitter_oauth/blocks.rb
Expand Up @@ -3,15 +3,13 @@ class Client

# unblock this user.
def block(id)
oauth_response = access_token.post("/blocks/create/#{id}.json")
JSON.parse(oauth_response.body)
post("/blocks/create/#{id}.json")
end

# block this user.
def unblock(id)
oauth_response = access_token.post("/blocks/destroy/#{id}.json")
JSON.parse(oauth_response.body)
post("/blocks/destroy/#{id}.json")
end

end
end
end
5 changes: 5 additions & 0 deletions lib/twitter_oauth/client.rb
Expand Up @@ -35,6 +35,11 @@ def get(url)
oauth_response = access_token.get(url)
JSON.parse(oauth_response.body)
end

def post(url, body = '', headers = {})
oauth_response = access_token.post(url, body, headers)
JSON.parse(oauth_response.body)
end

def show(username)
get("/users/show/#{username}.json")
Expand Down
6 changes: 2 additions & 4 deletions lib/twitter_oauth/direct_messages.rb
Expand Up @@ -13,14 +13,12 @@ def sent_messages

# Sends a new direct message to the specified user from the authenticating user.
def message(user, text)
oauth_response = access_token.post('/direct_messages/new.json', :user => user, :text => text)
JSON.parse(oauth_response.body)
post('/direct_messages/new.json', :user => user, :text => text)
end

# Destroys the direct message specified in the required ID parameter.
def message_destroy(id)
oauth_response = access_token.post("/direct_messages/destroy/#{id}.json")
JSON.parse(oauth_response.body)
post("/direct_messages/destroy/#{id}.json")
end

end
Expand Down
6 changes: 2 additions & 4 deletions lib/twitter_oauth/favorites.rb
Expand Up @@ -6,13 +6,11 @@ def favorites(page=1)
end

def favorite
oauth_response = access_token.post("/favorites/create/#{id}.json")
JSON.parse(oauth_response.body)
post("/favorites/create/#{id}.json")
end

def unfavorite
oauth_response = access_token.post("/favorites/destroy/#{id}.json")
JSON.parse(oauth_response.body)
post("/favorites/destroy/#{id}.json")
end

end
Expand Down
6 changes: 2 additions & 4 deletions lib/twitter_oauth/friendships.rb
Expand Up @@ -13,14 +13,12 @@ def followers_ids(options={})

# friend this user.
def friend(id)
oauth_response = access_token.post("/friendships/create/#{id}.json")
JSON.parse(oauth_response.body)
post("/friendships/create/#{id}.json")
end

# unfriend.
def unfriend(id)
oauth_response = access_token.post("/friendships/destroy/#{id}.json")
JSON.parse(oauth_response.body)
post("/friendships/destroy/#{id}.json")
end

# exists?.
Expand Down
8 changes: 3 additions & 5 deletions lib/twitter_oauth/notifications.rb
Expand Up @@ -3,15 +3,13 @@ class Client

# follow this user.
def follow(id)
oauth_response = access_token.post("/notifications/follow/#{id}.json")
JSON.parse(oauth_response.body)
post("/notifications/follow/#{id}.json")
end

# unfollow.
def leave(id)
oauth_response = access_token.post("/notifications/leave/#{id}.json")
JSON.parse(oauth_response.body)
post("/notifications/leave/#{id}.json")
end

end
end
end
9 changes: 3 additions & 6 deletions lib/twitter_oauth/status.rb
Expand Up @@ -8,20 +8,17 @@ def status(id)

# Updates the authenticating user's status.
def update(message, options={})
oauth_response = access_token.post('/statuses/update.json', options.merge(:status => message))
JSON.parse(oauth_response.body)
post('/statuses/update.json', options.merge(:status => message))
end

# Destroys the status specified by the required ID parameter
def status_destroy(id)
oauth_response = access_token.post("/statuses/destroy/#{id}.json")
JSON.parse(oauth_response.body)
post("/statuses/destroy/#{id}.json")
end

# Retweets the tweet specified by the id parameter. Returns the original tweet with retweet details embedded.
def retweet(id)
oauth_response = access_token.post("/statuses/retweet/#{id}.json")
JSON.parse(oauth_response.body)
post("/statuses/retweet/#{id}.json")
end

end
Expand Down

0 comments on commit d13e081

Please sign in to comment.