Skip to content

Add/new endpoints and update Metabase version #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
metabase:
image: metabase/metabase:v0.35.4
image: metabase/metabase:v0.38.4
ports:
- 3030:3000
volumes:
Expand Down
3 changes: 2 additions & 1 deletion lib/metabase/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ class Client
include Connection
include Endpoint

def initialize(url:, username: nil, password: nil, token: nil)
def initialize(url:, username: nil, password: nil, token: nil, ssl: {})
@url = url
@username = username
@password = password
@token = token
@ssl = ssl
end
end
end
2 changes: 1 addition & 1 deletion lib/metabase/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def request(method, path, params)
end

def connection
@connection ||= Faraday.new(url: @url) do |c|
@connection ||= Faraday.new(url: @url, ssl: @ssl) do |c|
c.request :json, content_type: /\bjson$/
c.response :json, content_type: /\bjson$/
c.request :url_encoded, content_type: /x-www-form-urlencoded/
Expand Down
9 changes: 9 additions & 0 deletions lib/metabase/endpoint/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def query_card_with_metadata(card_id, **params)
def query_card(card_id, format: :json, **params)
post("/api/card/#{card_id}/query/#{format}", **params)
end

# Create a new card.
#
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicard
def new_card(**params)
post('/api/card', **params)
end
end
end
end
27 changes: 27 additions & 0 deletions lib/metabase/endpoint/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ module Collection
def collections(**params)
get('/api/collection', **params)
end

# Fetch a collection.
#
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#get-apicollectionid
def collection(collection_id, **params)
get("/api/collection/#{collection_id}", **params)
end

# Fetch a collection's items.
#
# @param params [Hash] Query string
# @return [Array<Hash>] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#get-apicollectioniditems
def collection_items(collection_id, **params)
get("/api/collection/#{collection_id}/items", **params)
end

# Create a new collection.
#
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apicollection
def new_collection(**params)
post('/api/collection', **params)
end
end
end
end
59 changes: 59 additions & 0 deletions lib/metabase/endpoint/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,65 @@ module Dashboard
def dashboards(**params)
get('/api/dashboard', **params)
end

# Fetch the dashboard.
#
# @param dashboard_id [Integer, String] Dashboard ID
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#get-apidashboardid
def dashboard(dashboard_id, **params)
get("/api/dashboard/#{dashboard_id}", **params)
end

# Create a new dashboard.
#
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apidashboard
def new_dashboard(**params)
post('/api/dashboard', **params)
end

# Copy a dashboard.
#
# @param from_dashboard_id [Integer, String] Dashboard ID
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apidashboardfrom-dashboard-idcopy
def copy_dashboard(from_dashboard_id, **params)
post("/api/dashboard/#{from_dashboard_id}/copy", **params)
end

# Add a Card to a Dashboard.
#
# @param dashboard_id [Integer, String] Dashboard ID
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#post-apidashboardidcards
def add_dashboard_card(dashboard_id, **params)
post("/api/dashboard/#{dashboard_id}/cards", **params)
end

# Update Cards on a Dashboard.
#
# @param dashboard_id [Integer, String] Dashboard ID
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#put-apidashboardidcards
def update_dashboard_cards(dashboard_id, **params)
put("/api/dashboard/#{dashboard_id}/cards", **params)
end

# Remove a DashboardCard from a Dashboard.
#
# @param dashboard_id [Integer, String] Dashboard ID
# @param params [Hash] Query string
# @return [Hash] Parsed response JSON
# @see https://github.com/metabase/metabase/blob/master/docs/api-documentation.md#delete-apidashboardidcards
def delete_dashboard_card(dashboard_id, **params)
delete("/api/dashboard/#{dashboard_id}/cards", **params)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/metabase/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Metabase
VERSION = '0.5.0'
VERSION = '0.6.0'
end
10 changes: 10 additions & 0 deletions spec/metabase/endpoint/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@
end
end
end

describe 'new_card', vcr: true do
context 'success' do
it 'create a new card' do
card = client.card(card_id)
new_card = client.new_card(**card)
expect(new_card).to be_kind_of(Hash)
end
end
end
end
17 changes: 17 additions & 0 deletions spec/metabase/endpoint/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,30 @@

RSpec.describe Metabase::Endpoint::Collection do
include_context 'login'
let(:collection_id) { 4 }

describe 'collections', vcr: true do
context 'success' do
it 'returns all collections' do
collections = client.collections
expect(collections).to be_kind_of(Array)
end

it 'returns a collection' do
collection = client.collection(collection_id)
expect(collection).to be_kind_of(Hash)
end

it "returns a collection's items" do
collection = client.collection_items(collection_id)
expect(collection).to be_kind_of(Array)
end

it "create a new collection" do
params = { name: 'New Organization', color: '#509EE3' }
collection = client.new_collection(**params)
expect(collection).to be_kind_of(Hash)
end
end
end
end
63 changes: 63 additions & 0 deletions spec/metabase/endpoint/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

RSpec.describe Metabase::Endpoint::Dashboard do
include_context 'login'
let(:dashboard_id) { 1 }
let(:card_id) { 1 }

describe 'dashboards', vcr: true do
context 'success' do
Expand All @@ -11,4 +13,65 @@
end
end
end

describe 'dashboard', vcr: true do
context 'success' do
it 'returns the dashboard' do
dashboard = client.dashboard(dashboard_id)
expect(dashboard).to be_kind_of(Hash)
end
end
end

describe 'new_dashboard', vcr: true do
context 'success' do
it 'create a new dashboard' do
dashboard = client.dashboard(dashboard_id)
new_dashboard = client.new_dashboard(**dashboard)
expect(new_dashboard).to be_kind_of(Hash)
end
end
end

describe 'copy_dashboard', vcr: true do
context 'success' do
it 'create a copy dashboard' do
dashboard = client.dashboard(dashboard_id)
copy_dashboard = client.copy_dashboard(dashboard_id, **dashboard)
expect(copy_dashboard).to be_kind_of(Hash)
end
end
end

describe 'add_dashboard_cards', vcr: true do
context 'success' do
it 'add a dashboard cards' do
card = client.dashboard(dashboard_id)
card = client.card(card_id)
add_dashboard_cards = client.add_dashboard_card(dashboard_id, **card)
expect(add_dashboard_cards).to be_kind_of(Hash)
end
end
end

describe 'update_dashboard_cards', vcr: true do
context 'success' do
it 'update a dashboard cards' do
dashboard = client.dashboard(dashboard_id)
update_dashboard_cards = client.update_dashboard_cards(dashboard_id, **dashboard)
expect(update_dashboard_cards).to be_kind_of(Hash)
end
end
end

describe 'delete_dashboard_card', vcr: true do
context 'success' do
it 'delete a dashboard card' do
dashboard = client.dashboard(dashboard_id)
params = { dashcardId: dashboard['ordered_cards'][0]['id'] }
delete_dashboard_card = client.delete_dashboard_card(dashboard_id, **params)
expect(delete_dashboard_card).to be_nil
end
end
end
end
Loading