Skip to content

Commit

Permalink
feat(analytics): Add analytics resources (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad authored Nov 21, 2023
1 parent d1f8d71 commit fe3e1bd
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lago-ruby-client (0.50.0.pre.beta)
lago-ruby-client (0.51.0.pre.beta)
jwt
openssl

Expand Down
4 changes: 4 additions & 0 deletions lib/lago-ruby-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
require 'lago/api/resources/customer'
require 'lago/api/resources/event'
require 'lago/api/resources/fee'
require 'lago/api/resources/gross_revenue'
require 'lago/api/resources/group'
require 'lago/api/resources/invoice'
require 'lago/api/resources/invoiced_usage'
require 'lago/api/resources/mrr'
require 'lago/api/resources/organization'
require 'lago/api/resources/outstanding_invoice'
require 'lago/api/resources/plan'
require 'lago/api/resources/subscription'
require 'lago/api/resources/tax'
Expand Down
17 changes: 17 additions & 0 deletions lib/lago/api/resources/gross_revenue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Lago
module Api
module Resources
class GrossRevenue < Base
def api_resource
'analytics/gross_revenue'
end

def root_name
'gross_revenue'
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/lago/api/resources/invoiced_usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Lago
module Api
module Resources
class InvoicedUsage < Base
def api_resource
'analytics/invoiced_usage'
end

def root_name
'invoiced_usage'
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/lago/api/resources/mrr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Lago
module Api
module Resources
class Mrr < Base
def api_resource
'analytics/mrr'
end

def root_name
'mrr'
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/lago/api/resources/outstanding_invoice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Lago
module Api
module Resources
class OutstandingInvoice < Base
def api_resource
'analytics/outstanding_invoices'
end

def root_name
'outstanding_invoice'
end
end
end
end
end
14 changes: 14 additions & 0 deletions spec/fixtures/api/gross_revenue_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"gross_revenues": [
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"currency": "EUR"
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"currency": "USD"
}
]
}
16 changes: 16 additions & 0 deletions spec/fixtures/api/invoiced_usage_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"invoiced_usages": [
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"payment_status": "pending",
"currency": "EUR"
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"payment_status": "pending",
"currency": "USD"
}
]
}
14 changes: 14 additions & 0 deletions spec/fixtures/api/mrr_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"mrrs": [
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"currency": "EUR"
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"currency": "USD"
}
]
}
18 changes: 18 additions & 0 deletions spec/fixtures/api/outstanding_invoice_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"outstanding_invoices": [
{
"month": "2023-11-01T00:00:00.000Z",
"payment_status": "pending",
"invoices_count": 10,
"amount_cents": 100,
"currency": "EUR"
},
{
"month": "2023-12-01T00:00:00.000Z",
"payment_status": "pending",
"invoices_count": 7,
"amount_cents": 200,
"currency": "USD"
}
]
}
60 changes: 60 additions & 0 deletions spec/lago/api/resources/gross_revenue_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Lago::Api::Resources::GrossRevenue do
subject(:resource) { described_class.new(client) }

let(:client) { Lago::Api::Client.new }

let(:error_response) do
{
'status' => 422,
'error' => 'Unprocessable Entity',
'message' => 'Validation error on the record',
}.to_json
end

describe '#get_all' do
let(:gross_revenues_response) { load_fixture('gross_revenue_index') }

context 'when there is no options' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/gross_revenue')
.to_return(body: gross_revenues_response, status: 200)
end

it 'returns gross revenue' do
response = resource.get_all

expect(response['gross_revenues'].first['currency']).to eq('EUR')
expect(response['gross_revenues'].first['amount_cents']).to eq(100)
end
end

context 'when options are present' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/gross_revenue?currency=EUR')
.to_return(body: gross_revenues_response, status: 200)
end

it 'returns gross revenue' do
response = resource.get_all({ currency: 'EUR' })

expect(response['gross_revenues'].first['currency']).to eq('EUR')
expect(response['gross_revenues'].first['amount_cents']).to eq(100)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/gross_revenue')
.to_return(body: error_response, status: 422)
end

it 'raises an error' do
expect { resource.get_all }.to raise_error Lago::Api::HttpError
end
end
end
end
60 changes: 60 additions & 0 deletions spec/lago/api/resources/invoiced_usage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Lago::Api::Resources::InvoicedUsage do
subject(:resource) { described_class.new(client) }

let(:client) { Lago::Api::Client.new }

let(:error_response) do
{
'status' => 422,
'error' => 'Unprocessable Entity',
'message' => 'Validation error on the record',
}.to_json
end

describe '#get_all' do
let(:invoiced_usages_response) { load_fixture('invoiced_usage_index') }

context 'when there is no options' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/invoiced_usage')
.to_return(body: invoiced_usages_response, status: 200)
end

it 'returns gross revenue' do
response = resource.get_all

expect(response['invoiced_usages'].first['currency']).to eq('EUR')
expect(response['invoiced_usages'].first['amount_cents']).to eq(100)
end
end

context 'when options are present' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/invoiced_usage?currency=EUR')
.to_return(body: invoiced_usages_response, status: 200)
end

it 'returns gross revenue' do
response = resource.get_all({ currency: 'EUR' })

expect(response['invoiced_usages'].first['currency']).to eq('EUR')
expect(response['invoiced_usages'].first['amount_cents']).to eq(100)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/invoiced_usage')
.to_return(body: error_response, status: 422)
end

it 'raises an error' do
expect { resource.get_all }.to raise_error Lago::Api::HttpError
end
end
end
end
60 changes: 60 additions & 0 deletions spec/lago/api/resources/mrr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Lago::Api::Resources::Mrr do
subject(:resource) { described_class.new(client) }

let(:client) { Lago::Api::Client.new }

let(:error_response) do
{
'status' => 422,
'error' => 'Unprocessable Entity',
'message' => 'Validation error on the record',
}.to_json
end

describe '#get_all' do
let(:mrrs_response) { load_fixture('mrr_index') }

context 'when there is no options' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/mrr')
.to_return(body: mrrs_response, status: 200)
end

it 'returns gross revenue' do
response = resource.get_all

expect(response['mrrs'].first['currency']).to eq('EUR')
expect(response['mrrs'].first['amount_cents']).to eq(100)
end
end

context 'when options are present' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/mrr?currency=EUR')
.to_return(body: mrrs_response, status: 200)
end

it 'returns gross revenue' do
response = resource.get_all({ currency: 'EUR' })

expect(response['mrrs'].first['currency']).to eq('EUR')
expect(response['mrrs'].first['amount_cents']).to eq(100)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/mrr')
.to_return(body: error_response, status: 422)
end

it 'raises an error' do
expect { resource.get_all }.to raise_error Lago::Api::HttpError
end
end
end
end
Loading

0 comments on commit fe3e1bd

Please sign in to comment.