Skip to content

Commit

Permalink
Allow retrieval of accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Feb 5, 2014
1 parent 16e7339 commit d47bb0f
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/xednese.rb
Expand Up @@ -10,6 +10,8 @@
require_relative 'xednese/requests/messages'

require_relative 'xednese/responses/parser'
require_relative 'xednese/responses/account'
require_relative 'xednese/responses/accounts'
require_relative 'xednese/responses/message_dispatcher_headers'
require_relative 'xednese/responses/message_header'
require_relative 'xednese/responses/message_headers'
Expand Down
14 changes: 14 additions & 0 deletions lib/xednese/accounts.rb
Expand Up @@ -7,5 +7,19 @@ def initialize(credentials)
def reference
@credentials.account_reference
end

def each(&block)
Client.get(@credentials, 'v1.0/accounts') {|status, data|
Responses::Accounts.deserialise(data).accounts
}.each(&block)
end

include Enumerable

def get(id)
Client.get(@credentials, "v1.0/accounts/#{id}") do |status, data|
Responses::Account.deserialise(data)
end
end
end
end
25 changes: 25 additions & 0 deletions lib/xednese/responses/account.rb
@@ -0,0 +1,25 @@
class Esendex
module Responses
class Account
extend Serialisable

root 'account'

attribute :id, 'id'
attribute :uri, 'uri'

element :reference, 'reference'
element :label, 'label'
element :address, 'address'
element :type, 'type'
element :messages_remaining, 'messagesremaining', Parser.for(&:to_i)
element :expires_on, 'expireson'
element :role, 'role'
element :settings, Class.new {
extend Serialisable
root 'settings'
attribute :uri, 'uri'
}
end
end
end
10 changes: 10 additions & 0 deletions lib/xednese/responses/accounts.rb
@@ -0,0 +1,10 @@
class Esendex
module Responses
class Accounts
extend Serialisable

root 'accounts'
elements :accounts, Account
end
end
end
55 changes: 55 additions & 0 deletions spec/xednese/accounts_spec.rb
Expand Up @@ -9,4 +9,59 @@
subject.reference.must_equal credentials.account_reference
end
end

describe '#each' do
let(:data) { mock }
let(:account_list) { [mock, mock] }
let(:accounts) { stub(accounts: account_list) }

before {
Esendex::Client
.expects(:get)
.with(credentials, 'v1.0/accounts')
.yields(200, data)
.returns(account_list)

Esendex::Responses::Accounts
.expects(:deserialise)
.with(data)
.returns(accounts)

accounts
.expects(:accounts)
.returns(account_list)
}

it 'retrieves all accounts' do
returned_accounts = []
subject.each do |account|
returned_accounts << account
end

returned_accounts.must_equal account_list
end
end

describe '#get' do
let(:id) { "heyohid" }
let(:data) { mock }
let(:account) { mock }

before {
Esendex::Client
.expects(:get)
.with(credentials, "v1.0/accounts/#{id}")
.yields(200, data)
.returns(account)

Esendex::Responses::Account
.expects(:deserialise)
.with(data)
.returns(account)
}

it 'retrieves the specified accounts' do
subject.get(id).must_equal account
end
end
end
48 changes: 48 additions & 0 deletions spec/xednese/responses/account_spec.rb
@@ -0,0 +1,48 @@
require_relative '../../helper'

describe Esendex::Responses::Account do
describe '.deserialise' do

let(:id) { "A00F0218-510D-423D-A74E-5A65342FE070" }
let(:uri) { "http://api.esendex.com/v1.0/accounts/A00F0218-510D-423D-A74E-5A65342FE070" }
let(:reference) { "EX0000000" }
let(:label) { "label" }
let(:address) { "447700900654" }
let(:type) { "Professional" }
let(:messagesremaining) { 2000 }
let(:expireson) { "2999-02-25T00:00:00" }
let(:role) { "PowerUser" }
let(:settings_uri) { "http://api.esendex.com/v1.0/accounts/A00F0218-510D-423D-A74E-5A65342FE070/settings" }

let(:xml) {
<<EOS
<?xml version="1.0" encoding="utf-8"?>
<account id="#{id}" uri="#{uri}" xmlns="http://api.esendex.com/ns/">
<reference>#{reference}</reference>
<label>#{label}</label>
<address>#{address}</address>
<type>#{type}</type>
<messagesremaining>#{messagesremaining}</messagesremaining>
<expireson>#{expireson}</expireson>
<role>#{role}</role>
<settings uri="#{settings_uri}" />
</account>
EOS
}

it 'deserialises xml into an Account instance' do
account = Esendex::Responses::Account.deserialise(xml)

account.id.must_equal id
account.uri.must_equal uri
account.reference.must_equal reference
account.label.must_equal label
account.address.must_equal address
account.type.must_equal type
account.messages_remaining.must_equal messagesremaining
account.expires_on.must_equal expireson
account.role.must_equal role
account.settings.uri.must_equal settings_uri
end
end
end
51 changes: 51 additions & 0 deletions spec/xednese/responses/accounts_spec.rb
@@ -0,0 +1,51 @@
require_relative '../../helper'

describe Esendex::Responses::Accounts do
describe '.deserialise' do

let(:id) { "A00F0218-510D-423D-A74E-5A65342FE070" }
let(:uri) { "http://api.esendex.com/v1.0/accounts/A00F0218-510D-423D-A74E-5A65342FE070" }
let(:reference) { "EX0000000" }
let(:label) { "label" }
let(:address) { "447700900654" }
let(:type) { "Professional" }
let(:messagesremaining) { 2000 }
let(:expireson) { "2999-02-25T00:00:00" }
let(:role) { "PowerUser" }
let(:settings_uri) { "http://api.esendex.com/v1.0/accounts/A00F0218-510D-423D-A74E-5A65342FE070/settings" }

let(:xml) {
<<EOS
<?xml version="1.0" encoding="utf-8"?>
<accounts xmlns="http://api.esendex.com/ns/">
<account id="#{id}" uri="#{uri}">
<reference>#{reference}</reference>
<label>#{label}</label>
<address>#{address}</address>
<type>#{type}</type>
<messagesremaining>#{messagesremaining}</messagesremaining>
<expireson>#{expireson}</expireson>
<role>#{role}</role>
<settings uri="#{settings_uri}" />
</account>
</accounts>
EOS
}

it 'deserialises xml into an Accounts instance' do
accounts = Esendex::Responses::Accounts.deserialise(xml)
account = accounts.accounts.first

account.id.must_equal id
account.uri.must_equal uri
account.reference.must_equal reference
account.label.must_equal label
account.address.must_equal address
account.type.must_equal type
account.messages_remaining.must_equal messagesremaining
account.expires_on.must_equal expireson
account.role.must_equal role
account.settings.uri.must_equal settings_uri
end
end
end

0 comments on commit d47bb0f

Please sign in to comment.