Skip to content
This repository was archived by the owner on May 8, 2018. It is now read-only.

Add a host option to specify which interface to listen on. #3

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run_app(level, cmd)
end

def run_server(level)
run_app(level, "server --config=development.properties --port=#{ENV['PORT'] || 8080}")
run_app(level, "server --config=development.properties #{ENV['HOST'] && '--host=' + ENV['HOST']} --port=#{ENV['PORT'] || 8080}")
end

desc "Run brcm-accounts-api in an embedded Jetty server."
Expand Down Expand Up @@ -93,4 +93,4 @@ namespace :db do
task :schema do
run_app(:silent, "schema --config=development.properties")
end
end
end
6 changes: 5 additions & 1 deletion script/server
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: server [options]"

opts.on("-H", "--host=HOST", "The host name to run on") do |hostname|
options[:hostname] = hostname
end

opts.on("-p", "--port=PORT", "The port number to run on") do |portnum|
options[:portnum] = portnum
end
Expand Down Expand Up @@ -44,5 +48,5 @@ end

brcm_dir = File.expand_path("../..", __FILE__)
Dir.chdir(brcm_dir)
exec "rake #{cmd} PORT=#{options[:portnum]}"
exec "rake #{cmd} HOST=#{options[:hostname]} PORT=#{options[:portnum]}"

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wesabe.api.accounts.analytics;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.Currency;
Expand Down Expand Up @@ -30,6 +31,8 @@ public class TxactionListBuilder {
private int limit = 0;
private Currency currency;
private CurrencyExchangeRateMap exchangeRateMap;
private BigDecimal amount;
private String query;

public TxactionList build(Collection<Txaction> txactions) {
TxactionList txactionList = new TxactionList();
Expand Down Expand Up @@ -100,6 +103,10 @@ private List<Txaction> sort(List<Txaction> txactions) {
private List<Txaction> filter(Collection<Txaction> txactions) {
List<Txaction> filteredTxactions = filterHiddenTxactions(txactions);

if (amount != null) {
filteredTxactions = filterByAmount(filteredTxactions);
}

if (!accounts.isEmpty()) {
filteredTxactions = filterByAccounts(filteredTxactions);
}
Expand All @@ -116,9 +123,24 @@ private List<Txaction> filter(Collection<Txaction> txactions) {
filteredTxactions = filterByUnedited(filteredTxactions);
}

if (query != null) {
filteredTxactions = filterByQuery(filteredTxactions);
}

return filteredTxactions;
}

private List<Txaction> filterByAmount(List<Txaction> txactions) {
return Lists.newArrayList(
Iterables.filter(txactions, new Predicate<Txaction>() {
@Override
public boolean apply(Txaction txaction) {
return txaction.getAmount().getValue().equals(amount);
}
})
);
}

private List<Txaction> filterByMerchants(List<Txaction> txactions) {
return Lists.newArrayList(
Iterables.filter(txactions, new Predicate<Txaction>() {
Expand Down Expand Up @@ -180,6 +202,52 @@ public boolean apply(Txaction txaction) {
);
}

private List<Txaction> filterByQuery(List<Txaction> txactions) {
final String lowerQuery = query.toLowerCase();

return Lists.newArrayList(
Iterables.filter(txactions, new Predicate<Txaction>() {
@Override
public boolean apply(Txaction txaction) {
final String filteredName = txaction.getFilteredName();

if (filteredName != null && filteredName.toLowerCase().contains(lowerQuery)) {
return true;
}

final String note = txaction.getNote();

if (note != null && note.toLowerCase().contains(lowerQuery)) {
return true;
}

final Merchant merchant = txaction.getMerchant();

if (merchant != null) {
final String merchantName = merchant.getName();

if (merchantName != null && merchantName.toLowerCase().contains(lowerQuery)) {
return true;
}
}

final List<TaggedAmount> taggedAmounts = txaction.getTaggedAmounts();

if (taggedAmounts != null) {
for (TaggedAmount taggedAmount : taggedAmounts) {
final Tag tag = taggedAmount.getTag();
if (tag != null && tag.toString().toLowerCase().contains(lowerQuery)) {
return true;
}
}
}

return false;
}
})
);
}

public TxactionListBuilder setMerchantNames(Collection<String> merchantNames) {
this.merchantNames = ImmutableSet.copyOf(merchantNames);
return this;
Expand Down Expand Up @@ -214,6 +282,11 @@ public TxactionListBuilder setLimit(int limit) {
this.limit = limit;
return this;
}

public TxactionListBuilder setAmount(BigDecimal amount) {
this.amount = amount;
return this;
}

public TxactionListBuilder setCurrency(Currency currency) {
this.currency = currency;
Expand All @@ -228,4 +301,9 @@ public TxactionListBuilder setCurrencyExchangeRateMap(CurrencyExchangeRateMap ex
public CurrencyExchangeRateMap getCurrencyExchangeRateMap() {
return exchangeRateMap;
}

public TxactionListBuilder setQuery(String query) {
this.query = query;
return this;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/wesabe/api/accounts/entities/Txaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public class Txaction implements Monetary, Comparable<Txaction> {
@Column(name="raw_name")
private String rawName;

@Column(name="filtered_name")
private String filteredName;

@Column
private String memo;

Expand Down Expand Up @@ -223,6 +226,10 @@ public boolean isTransfer() {
return transferTxaction != null;
}

public boolean isPairedTransfer() {
return transferTxaction != null && !transferTxaction.equals(this);
}

public TxactionStatus getStatus() {
return TxactionStatus.byValue(status);
}
Expand Down Expand Up @@ -306,6 +313,10 @@ public String getUneditedName() {
return uneditedName;
}

public String getFilteredName() {
return filteredName;
}

public Merchant getMerchant() {
return merchant;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public XmlsonObject present(Txaction txaction, Locale locale) {
private XmlsonObject presentWithTransfer(Txaction txaction, Locale locale) {
final XmlsonObject root = presentWithoutTransfer("transaction", txaction, locale);

if (txaction.isTransfer()) {
if (txaction.isPairedTransfer()) {
root.add(presentWithoutTransfer("transfer", txaction.getTransferTxaction(), locale));
} else if (txaction.isTransfer()) {
root.addProperty("transfer", true);
} else {
root.addNullProperty("transfer");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public XmlsonObject show(@Context WesabeUser user, @Context Locale locale,
@QueryParam("account") Set<UriParam> accountUris,
@QueryParam("tag") Set<String> tagUris,
@QueryParam("merchant") Set<String> merchantNames,
@QueryParam("ignore-tag") Set<Tag> ignoredTags) {
@QueryParam("ignore-tag") Set<Tag> ignoredTags,
@QueryParam("query") String query) {

final DateTime intervalStartDate = intervalType.getValue().currentInterval(startDate.getValue()).getStart();
final DateTime intervalEndDate = intervalType.getValue().currentInterval(endDate.getValue()).getEnd();
Expand All @@ -96,6 +97,7 @@ public XmlsonObject show(@Context WesabeUser user, @Context Locale locale,
builder.setUnedited(uneditedOnly.getValue());
builder.setTags(getTags(tagUris));
builder.setAccounts(accounts);
builder.setQuery(query);

if (!merchantNames.isEmpty()) {
builder.setMerchantNames(merchantNames);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wesabe.api.accounts.resources;

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -72,13 +73,16 @@ public XmlsonObject show(@Context WesabeUser user,
@QueryParam("end") ISODateParam endDate,
@QueryParam("account") Set<UriParam> accountUris,
@QueryParam("tag") Set<String> tagUris,
@QueryParam("merchant") Set<String> merchantNames) {
@QueryParam("merchant") Set<String> merchantNames,
@QueryParam("amount") BigDecimal amount,
@QueryParam("query") String query) {

final List<Account> accounts = getAccounts(user, accountUris);
final TxactionList txactions = filterTxactions(
accounts,
getTxactions(accounts, startDate, endDate),
currency, uneditedOnly, limit, offset, tagUris, merchantNames
currency, uneditedOnly, limit, offset,
tagUris, merchantNames, amount, query
);

return presenter.present(txactions, locale);
Expand All @@ -87,7 +91,7 @@ public XmlsonObject show(@Context WesabeUser user,
private TxactionList filterTxactions(List<Account> accounts,
List<Txaction> txactions, CurrencyParam currency, BooleanParam uneditedOnly,
IntegerParam limit, IntegerParam offset, Set<String> tagUris,
Set<String> merchantNames) {
Set<String> merchantNames, BigDecimal amount, String query) {
final TxactionListBuilder txactionListBuilder = builderProvider.get();
txactionListBuilder.setAccounts(accounts);
txactionListBuilder.setUnedited(uneditedOnly.getValue());
Expand All @@ -103,6 +107,12 @@ private TxactionList filterTxactions(List<Account> accounts,
if (offset != null) {
txactionListBuilder.setOffset(offset.getValue());
}
if (amount != null) {
txactionListBuilder.setAmount(amount);
}
if (query != null) {
txactionListBuilder.setQuery(query);
}

final TxactionList filteredTxactions = txactionListBuilder.build(txactions);
return filteredTxactions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.wesabe.api.accounts.entities.AccountType;
import com.wesabe.api.accounts.entities.Merchant;
import com.wesabe.api.accounts.entities.Tag;
import com.wesabe.api.accounts.entities.TaggedAmount;
import com.wesabe.api.accounts.entities.Txaction;
import com.wesabe.api.accounts.entities.TxactionList;
import com.wesabe.api.accounts.entities.TxactionStatus;
Expand Down Expand Up @@ -447,4 +448,104 @@ public void itIncludesTheLimitedTransactionsInTheTotalCount() throws Exception {
assertEquals(3, list.getTotalCount());
}
}

public static class A_Builder_With_An_Amount {
private List<Txaction> txactions;
private Account checking = Account.ofType(AccountType.CHECKING);
private Account savings = Account.ofType(AccountType.SAVINGS);
private Txaction wholeFoods = new Txaction(checking, decimal("-48.19"), jun14th);
private Txaction starbucks = new Txaction(checking, decimal("-3.00"), jun15th);
private Txaction interestEarned = new Txaction(savings, decimal("23.01"), new DateTime());
private CurrencyExchangeRateMap exchangeRates = new CurrencyExchangeRateMap();

@Before
public void setup() throws Exception {
checking.setCurrency(USD);
savings.setCurrency(USD);

inject(Account.class, checking, "accountBalances", Sets.newHashSet(new AccountBalance(checking, decimal("100.00"), new DateTime())));
inject(Account.class, savings, "accountBalances", Sets.newHashSet(new AccountBalance(savings, decimal("100.00"), new DateTime())));

starbucks.setStatus(TxactionStatus.ACTIVE);
wholeFoods.setStatus(TxactionStatus.ACTIVE);
interestEarned.setStatus(TxactionStatus.ACTIVE);

txactions = ImmutableList.of(interestEarned, starbucks, wholeFoods);
}

@Test
public void itReturnsTxactionsWithTheCorrectAmount() {
final TxactionList list = new TxactionListBuilder()
.setAmount(decimal("-3.00"))
.setCurrency(USD)
.setCurrencyExchangeRateMap(exchangeRates)
.build(txactions);
assertEquals(ImmutableList.of(starbucks), list.getTxactions());
}
}
public static class A_Builder_With_A_Query {
private List<Txaction> txactions;
private Account checking = Account.ofType(AccountType.CHECKING);
private Account savings = Account.ofType(AccountType.SAVINGS);
private Txaction wholeFoods = new Txaction(checking, decimal("-48.19"), jun14th);
private Txaction starbucks = new Txaction(checking, decimal("-3.00"), jun15th);
private Txaction interestEarned = new Txaction(savings, decimal("23.01"), new DateTime());
private CurrencyExchangeRateMap exchangeRates = new CurrencyExchangeRateMap();

@Before
public void setup() throws Exception {
checking.setCurrency(USD);
savings.setCurrency(USD);

inject(Account.class, checking, "accountBalances", Sets.newHashSet(new AccountBalance(checking, decimal("100.00"), new DateTime())));
inject(Account.class, savings, "accountBalances", Sets.newHashSet(new AccountBalance(savings, decimal("100.00"), new DateTime())));

starbucks.setStatus(TxactionStatus.ACTIVE);
wholeFoods.setStatus(TxactionStatus.ACTIVE);
interestEarned.setStatus(TxactionStatus.ACTIVE);

txactions = ImmutableList.of(interestEarned, starbucks, wholeFoods);
}

private TxactionList buildTxactionList(String query) {
final TxactionList list = new TxactionListBuilder()
.setQuery(query)
.setCurrency(USD)
.setCurrencyExchangeRateMap(exchangeRates)
.build(txactions);
return list;
}

@Test
public void itReturnsTxactionsWithFilteredNamesContainingTheQuery() throws Exception {
inject(Txaction.class, starbucks, "filteredName", "Starbucks San Francis");

final TxactionList list = buildTxactionList("Starbucks");
assertEquals(ImmutableList.of(starbucks), list.getTxactions());
}

@Test
public void itReturnsTxactionsWithMerchantNamesContainingTheQuery() throws Exception {
starbucks.setMerchant(new Merchant("Starbucks"));

final TxactionList list = buildTxactionList("Starbucks");
assertEquals(ImmutableList.of(starbucks), list.getTxactions());
}

@Test
public void itReturnsTxactionsWithTagNamesContainingTheQuery() throws Exception {
inject(Txaction.class, starbucks, "taggedAmounts", ImmutableList.of(new TaggedAmount(starbucks, new Tag("snack"), null)));

final TxactionList list = buildTxactionList("Snack");
assertEquals(ImmutableList.of(starbucks), list.getTxactions());
}

@Test
public void itReturnsTxactionsWithNotesContainingTheQuery() throws Exception {
inject(Txaction.class, starbucks, "note", "MUFFINS OH JOY");

final TxactionList list = buildTxactionList("muffins");
assertEquals(ImmutableList.of(starbucks), list.getTxactions());
}
}
}
Loading