Skip to content

Commit

Permalink
Merge pull request #86 from ResultadosDigitais/add-rdsm-account-info-…
Browse files Browse the repository at this point in the history
…endpoint

Add the RDSM Account info endpoint
  • Loading branch information
m3nd3s authored Mar 14, 2023
2 parents 172c67d + 2757fb4 commit ec77697
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## 2.9.0

### Additions

#### 1. New Account client

Usage example:

```ruby
client = RDStation::Client.new(access_token: 'access_token', refresh_token: 'refresh_token')
client.account.info
```


## 2.8.2

- Fix TooManyRequest handler
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Upgrading? Check the [migration guide](#Migration-guide) before bumping to a new
8. [Segmentations](#Segmentations)
9. [Analytics](#Analytics)
10.[LandingPages](#LandingPages)
11.[Errors](#Errors)
11.[Account](#Account)
12.[Errors](#Errors)
3. [Changelog](#Changelog)
4. [Migration guide](#Migration-guide)
1. [Upgrading from 1.2.x to 2.0.0](#Upgrading-from-1.2.x-to-2.0.0)
Expand Down Expand Up @@ -377,6 +378,16 @@ client = RDStation::Client.new(access_token: 'access_token', refresh_token: 'ref
client.landing_pages.all
```

### Account

Returns the account information.

```ruby
client = RDStation::Client.new(access_token: 'access_token', refresh_token: 'refresh_token')
client.account.info
=> {"name"=>"www.rdstation.com"}
```

### Errors

Each endpoint may raise errors accoording to the HTTP response code from RDStation:
Expand Down
1 change: 1 addition & 0 deletions lib/rdstation-ruby-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'rdstation/authentication'
require 'rdstation/authorization'
require 'rdstation/client'
require 'rdstation/account'
require 'rdstation/contacts'
require 'rdstation/events'
require 'rdstation/fields'
Expand Down
25 changes: 25 additions & 0 deletions lib/rdstation/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module RDStation
class Account
include HTTParty
include ::RDStation::RetryableRequest

def initialize(authorization:)
@authorization = authorization
end

def info
retryable_request(@authorization) do |authorization|
response = self.class.get(base_url, headers: authorization.headers)
ApiResponse.build(response)
end
end

private

def base_url(_path = '')
"#{RDStation.host}/marketing/account_info"
end
end
end
4 changes: 4 additions & 0 deletions lib/rdstation/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize(access_token:, refresh_token: nil)
)
end

def account
@account ||= RDStation::Account.new(authorization: @authorization)
end

def contacts
@contacts ||= RDStation::Contacts.new(authorization: @authorization)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rdstation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RDStation
VERSION = '2.8.2'
VERSION = '2.9.0'
end
57 changes: 57 additions & 0 deletions spec/lib/rdstation/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'

RSpec.describe RDStation::Account do
let(:client) do
described_class.new(authorization: RDStation::Authorization.new(access_token: 'access_token'))
end

let(:endpoint) { 'https://api.rd.services/marketing/account_info' }

let(:headers) do
{
'Authorization' => 'Bearer access_token',
'Content-Type' => 'application/json'
}
end

let(:error_handler) do
instance_double(RDStation::ErrorHandler, raise_error: 'mock raised errors')
end

before do
allow(RDStation::ErrorHandler).to receive(:new).and_return(error_handler)
end

describe '#info' do
it 'calls retryable_request' do
expect(client).to receive(:retryable_request)
client.info
end

context 'when the request is successful' do
before do
stub_request(:get, endpoint)
.with(headers: headers)
.to_return(status: 200, body: {name: 'www.rdstation.com'}.to_json)
end

it 'return RDSM account information' do
response = client.info
expect(response).to eq({ 'name' => 'www.rdstation.com'})
end
end

context 'when the response contains errors' do
before do
stub_request(:get, endpoint)
.with(headers: headers)
.to_return(status: 404, body: { 'errors' => ['not found'] }.to_json)
end

it 'calls raise_error on error handler' do
client.info
expect(error_handler).to have_received(:raise_error)
end
end
end
end
5 changes: 5 additions & 0 deletions spec/lib/rdstation/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
expect(RDStation::Webhooks).to receive(:new).with({ authorization: mock_authorization }).and_call_original
expect(client.webhooks).to be_instance_of RDStation::Webhooks
end

it 'returns Account endpoint' do
expect(RDStation::Account).to receive(:new).with({ authorization: mock_authorization }).and_call_original
expect(client.account).to be_instance_of RDStation::Account
end
end

context "when access_token isn't given" do
Expand Down

0 comments on commit ec77697

Please sign in to comment.