Skip to content

Commit

Permalink
Add Whoami struct to replace Hash response
Browse files Browse the repository at this point in the history
Closes GH-110
  • Loading branch information
weppos committed Jun 10, 2016
1 parent 9860da1 commit 48e4868
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
7 changes: 2 additions & 5 deletions lib/dnsimple/client/identity.rb
Expand Up @@ -7,15 +7,12 @@ module Identity
# @see https://developer.dnsimple.com/v2/identity/#whoami
#
# @param [Hash] options
# @return [Dnsimple::Response<Hash>]
# @return [Dnsimple::Response<Dnsimple::Struct::Whoami>]
# @raise [Dnsimple::RequestError]
def whoami(options = {})
response = client.get(Client.versioned("/whoami"), options)

data = response["data"]
account = data["account"] ? Struct::Account.new(data["account"]) : nil
user = data["user"] ? Struct::User.new(data["user"]) : nil
Response.new(response, account: account, user: user)
Dnsimple::Response.new(response, Struct::Whoami.new(response["data"]))
end


Expand Down
1 change: 1 addition & 0 deletions lib/dnsimple/struct.rb
Expand Up @@ -29,3 +29,4 @@ def initialize(attributes = {})
require_relative 'struct/whois_privacy'
require_relative 'struct/zone'
require_relative 'struct/webhook'
require_relative 'struct/whoami'
30 changes: 30 additions & 0 deletions lib/dnsimple/struct/whoami.rb
@@ -0,0 +1,30 @@
module Dnsimple
module Struct

class Whoami < Base
# @return [Account] The account, if present.
attr_accessor :account

# @return [String] The user, if present.
attr_accessor :user


# Converts account to a Struct::Account and sets it.
#
# @param [Hash, nil] account
# @return [void]
def account=(account)
@account = account ? Struct::Account.new(account) : account
end

# Converts user to a Struct::User and sets it.
#
# @param [Hash, nil] user
# @return [void]
def user=(user)
@user = user ? Struct::User.new(user) : user
end
end

end
end
12 changes: 6 additions & 6 deletions spec/dnsimple/client/identity_spec.rb
Expand Up @@ -18,12 +18,12 @@
with(headers: { 'Accept' => 'application/json' })
end

it "returns a response" do
it "returns the whoami" do
response = subject.whoami
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Hash)
expect(result).to be_a(Dnsimple::Struct::Whoami)
end

context "when authenticated as account" do
Expand All @@ -34,8 +34,8 @@

it "sets the account" do
result = subject.whoami.data
expect(result[:account]).to be_a(Dnsimple::Struct::Account)
expect(result[:user]).to be_nil
expect(result.account).to be_a(Dnsimple::Struct::Account)
expect(result.user).to be_nil
end
end

Expand All @@ -47,8 +47,8 @@

it "sets the user" do
result = subject.whoami.data
expect(result[:account]).to be_nil
expect(result[:user]).to be_a(Dnsimple::Struct::User)
expect(result.account).to be_nil
expect(result.user).to be_a(Dnsimple::Struct::User)
end
end
end
Expand Down

0 comments on commit 48e4868

Please sign in to comment.