From b51b6d0361665d561633c466e7f5a0d7acfcf513 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sun, 27 May 2018 20:02:37 -0700 Subject: [PATCH] baremetrics: Simplify response generation. --- .../bots/baremetrics/baremetrics.py | 91 ++++++++++--------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py index f7d648b..b9af587 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py @@ -110,12 +110,13 @@ class BaremetricsHandler(object): account_data = account_response.json() account_data = account_data['account'] - response = '**Your account information:** \n' - response += 'Id: {id}\n'.format(id=account_data['id']) - response += 'Company: {company}\n'.format(company=account_data['company']) - response += 'Default Currency: {currency_name}'.format(currency_name=account_data['default_currency']['name']) + template = ['**Your account information:** ', + 'Id: {id}', + 'Company: {company}', + 'Default Currency: {currency}'] - return response + return "\n".join(template).format(currency=account_data['default_currency']['name'], + **account_data) def get_sources(self) -> str: url = 'https://api.baremetrics.com/v1/sources' @@ -126,9 +127,9 @@ class BaremetricsHandler(object): response = '**Listing sources:** \n' for index, source in enumerate(sources_data): - response += '{}.ID: {}\nProvider: {}\nProvider ID: {}\n\n'.format(index + 1, source['id'], - source['provider'], - source['provider_id']) + response += ('{_count}.ID: {id}\n' + 'Provider: {provider}\n' + 'Provider ID: {provider_id}\n\n').format(_count=index + 1, **source) return response @@ -139,18 +140,19 @@ class BaremetricsHandler(object): plans_data = plans_response.json() plans_data = plans_data['plans'] - template = '{}.Name: {}\nActive: {}\nInterval: {}\nInterval Count: {}\nAmounts: \n' - response = '**Listing plans:** \n' + template = '\n'.join(['{_count}.Name: {name}', + 'Active: {active}', + 'Interval: {interval}', + 'Interval Count: {interval_count}', + 'Amounts: ']) + response = ['**Listing plans:** '] for index, plan in enumerate(plans_data): - response += template.format(index + 1, plan['name'], plan['active'], plan['interval'], - plan['interval_count']) + response += ([template.format(_count=index + 1, **plan)] + + [' - {amount} {currency}'.format(**amount) + for amount in plan['amounts']] + + ['']) - for amount in plan['amounts']: - response += ' - {} {}\n'.format(amount['amount'], amount['currency']) - - response += '\n' - - return response + return '\n'.join(response + ['']) def get_customers(self, source_id: str) -> str: url = 'https://api.baremetrics.com/v1/{}/customers'.format(source_id) @@ -159,18 +161,22 @@ class BaremetricsHandler(object): customers_data = customers_response.json() customers_data = customers_data['customers'] - template = '{}.Name: {}\nDisplay Name: {}\nOID: {}\nActive: {}\nEmail: {}\nNotes: {}\nCurrent Plans: \n' - response = '**Listing customers:** \n' + # FIXME BUG here? mismatch of name and display name? + template = '\n'.join(['{_count}.Name: {display_name}', + 'Display Name: {name}', + 'OID: {oid}', + 'Active: {is_active}', + 'Email: {email}', + 'Notes: {notes}', + 'Current Plans: ']) + response = ['**Listing customers:** '] for index, customer in enumerate(customers_data): - response += template.format(index + 1, customer['display_name'], customer['name'], customer['oid'], - customer['is_active'], customer['email'], customer['notes']) + response += ([template.format(_count=index + 1, **customer)] + + [' - {name}'.format(**plan) + for plan in customer['current_plans']] + + ['']) - for plan in customer['current_plans']: - response += ' - {}\n'.format(plan['name']) - - response += '\n' - - return response + return '\n'.join(response + ['']) def get_subscriptions(self, source_id: str) -> str: url = 'https://api.baremetrics.com/v1/{}/subscriptions'.format(source_id) @@ -179,21 +185,24 @@ class BaremetricsHandler(object): subscriptions_data = subscriptions_response.json() subscriptions_data = subscriptions_data['subscriptions'] - template = '{}.Customer Name: {}\nCustomer Display Name: {}\nCustomer OID: {}\nCustomer Email: {}\n' \ - 'Active: {}\nPlan Name: {}\nPlan Amounts: \n' - response = '**Listing subscriptions:** \n' + template = '\n'.join(['{_count}.Customer Name: {name}', + 'Customer Display Name: {display_name}', + 'Customer OID: {oid}', + 'Customer Email: {email}', + 'Active: {_active}', + 'Plan Name: {_plan_name}', + 'Plan Amounts:' ]) + response = ['**Listing subscriptions:** '] for index, subscription in enumerate(subscriptions_data): - response += template.format(index + 1, subscription['customer']['name'], - subscription['customer']['display_name'], - subscription['customer']['oid'], subscription['customer']['email'], - subscription['active'], subscription['plan']['name']) + response += ([template.format(_count=index + 1, + _active=subscription['active'], + _plan_name=subscription['plan']['name'], + **subscription['customer'])] + + [' - {amount} {symbol}'.format(**amount) + for amount in subscription['plan']['amounts']] + + ['']) - for amount in subscription['plan']['amounts']: - response += ' - {} {}\n'.format(amount['amount'], amount['symbol']) - - response += '\n' - - return response + return '\n'.join(response + ['']) def create_plan(self, parameters: List[str]) -> str: data_header = {