Skip to content

Commit

Permalink
Merge pull request #166 from joeyw/implement-public-key-get-and-update
Browse files Browse the repository at this point in the history
implement .key and .key_update
  • Loading branch information
pengwynn committed Oct 24, 2012
2 parents 09b5b99 + 06b16b3 commit 8373dcb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
47 changes: 43 additions & 4 deletions lib/octokit/client/users.rb
@@ -1,9 +1,5 @@
module Octokit
class Client
#@todo Add support for getting a single public key by id.
# http://developer.github.com/v3/users/keys/#get-a-single-public-key
#@todo Add support for updating a public key.
# http://developer.github.com/v3/users/keys/#update-a-public-key
module Users

# Search for user.
Expand Down Expand Up @@ -161,6 +157,33 @@ def watched(user=login, options={})
get("users/#{user}/watched", options, 3)
end

# Get a public key.
#
# Note, when using dot notation to retrieve the values, ruby will return
# the hash key for the public keys value instead of the actual value, use
# symbol or key string to retrieve the value. See example.
#
# Requires authenticated client.
#
# @param key_id [Integer] Key to retreive.
# @param [Hashie::Mash] Hash representing the key.
# @see http://developer.github.com/v3/users/keys/#get-a-single-public-key
# @example
# @client.key(1)
# @example Retrieve public key contents
# public_key = @client.key(1)
# public_key.key
# # => Error
#
# public_key[:key]
# # => "ssh-rsa AAA..."
#
# public_key['key']
# # => "ssh-rsa AAA..."
def key(key_id, options={})
get("user/keys/#{key_id}", options, 3)
end

# Get list of public keys for user.
#
# Requires authenticated client.
Expand Down Expand Up @@ -189,6 +212,22 @@ def add_key(title, key, options={})
post("user/keys", options.merge({:title => title, :key => key}), 3)
end

# Update a public key
#
# Requires authenticated client
#
# @param key_id [Integer] Id of key to update.
# @param options [Hash] Hash containing attributes to update.
# @option options [String] :title
# @option options [String] :key
# @return [Hashie::Mash] Hash representing the updated public key.
# @see http://developer.github.com/v3/users/keys/#update-a-public-key
# @example
# @client.update_key(1, :title => 'new title', :key => "ssh-rsa BBB")
def update_key(key_id, options={})
patch("/user/keys/#{key_id}", options)
end

# Remove a public key from user account.
#
# Requires authenticated client.
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/v3/public_key_update.json
@@ -0,0 +1,5 @@
{
"id": 103205,
"key": "ssh-rsa BBBB...",
"title": "updated title"
}
29 changes: 29 additions & 0 deletions spec/octokit/client/users_spec.rb
Expand Up @@ -242,6 +242,18 @@

end

describe ".key" do

it "returns a public key" do
stub_get("https://api.github.com/user/keys/103205").
to_return(:body => fixture('v3/public_key.json'))
public_key = @client.key(103205)
expect(public_key.id).to eq(103205)
expect(public_key[:key]).to include("ssh-dss AAAAB")
end

end

describe ".keys" do

it "returns public keys" do
Expand All @@ -266,6 +278,23 @@

end

describe ".update_key" do

it "updates a public key" do
updated_key = {
:title => "updated title",
:key => "ssh-rsa BBBB..."
}
stub_patch("https://api.github.com/user/keys/1").
with(updated_key).
to_return(:body => fixture("v3/public_key_update.json"))
public_key = @client.update_key(1, updated_key)
expect(public_key[:title]).to eq(updated_key[:title])
expect(public_key[:key]).to eq(updated_key[:key])
end

end

describe ".remove_key" do

it "removes a public key" do
Expand Down

0 comments on commit 8373dcb

Please sign in to comment.