Skip to content

Loading…

implement .key and .key_update #166

Merged
merged 1 commit into from

2 participants

@joeyw
octokit member

No description provided.

@pengwynn pengwynn merged commit 8373dcb into octokit:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 23, 2012
  1. @joeyw

    implement .key and .key_update

    joeyw committed
Showing with 77 additions and 4 deletions.
  1. +43 −4 lib/octokit/client/users.rb
  2. +5 −0 spec/fixtures/v3/public_key_update.json
  3. +29 −0 spec/octokit/client/users_spec.rb
View
47 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.
@@ -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.
@@ -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.
View
5 spec/fixtures/v3/public_key_update.json
@@ -0,0 +1,5 @@
+{
+ "id": 103205,
+ "key": "ssh-rsa BBBB...",
+ "title": "updated title"
+}
View
29 spec/octokit/client/users_spec.rb
@@ -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
@@ -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
Something went wrong with that request. Please try again.