Skip to content

Commit

Permalink
Merge pull request #48 from FoamFactory/jwir3/#45-multiple-apis
Browse files Browse the repository at this point in the history
[r=jwir3]
  • Loading branch information
jwir3 authored Oct 17, 2022
2 parents ee6d37a + ce0ebe2 commit 0a907a9
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 267 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# bubblez
[![Build Status](https://travis-ci.org/FoamFactory/bubbles.svg?branch=master)](https://travis-ci.org/FoamFactory/bubbles)
[![Build Status](https://github.com/FoamFactory/bubblez/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/FoamFactory/bubblez/actions/workflows/test.yml)

A gem for easily defining client REST interfaces in ruby

Expand Down Expand Up @@ -38,33 +38,36 @@ In `config/initializers/bubblez.rb`, add the following:
require 'bubblez'

Bubblez.configure do |config|
config.endpoints = [
{
:method => :get,
:location => :version,
:authenticated => false,
:api_key_required => false
}
]

config.environments = [{
:scheme => 'http',
:host => '0.0.0.0',
:port => '1234'
}]
config.add_api(name: 'MyApi',
environments: [{
:scheme => 'http',
:host => '0.0.0.0',
:port => '1234'
}],
endpoints: [
{
:method => :get,
:location => :version,
:authenticated => false,
:api_key_required => false
}
])
end
```

The `config.endpoints` section is where you configure which endpoints you want to support. The `config.environments`
defines the environments, or remote configurations, for accessing the endpoint on specific remote destinations.
The `config` object is the root of the `bubblez` configuration. It contains one or more API configurations, each added using the `add_api` method. Each API configuration must have a unique name.

The `endpoints` parameter of the method is where you configure which endpoints you want to support.
The `environments` parameter of the method defines the environments, or remote configurations, for accessing the
endpoints on specific remote destinations.

Now, you can use this endpoint with:
```ruby
require 'bubblez'
...

def version
resources = Bubbles::Resources.new
resources = Bubblez::Resources.new 'Default'

# The following will make a GET request to
# http://0.0.0.0:1234/version and return the result.
Expand All @@ -74,5 +77,6 @@ def version
end
```


## Detailed Documentation
For more examples and detailed documentation, please see [the Bubblez GitHub page](http://foamfactory.github.io/bubblez).
7 changes: 6 additions & 1 deletion lib/bubblez.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@

module Bubblez
class Resources < RestClientResources
def initialize(api_key='')
def initialize(name, api_key='')
super

@config = Bubblez.configuration[name]
@package_name = Bubblez::VersionInformation.package_name
@version_name = Bubblez::VersionInformation.version_name
@version_code = Bubblez::VersionInformation.version_code
end

def config
@config
end

def get_version_info
{
:name => @package_name,
Expand Down
72 changes: 61 additions & 11 deletions lib/bubblez/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,84 @@ class << self
#
# @example In app/config/initializers/bubblez.rb
# Bubblez.configure do |config|
# config.endpoints = [
# {
# :type => :get,
# :location => :version,
# :authenticated => false,
# :api_key_required => false
# }
# ]
# config.add_api('someApiName', endpoints: [
# {
# :type => :get,
# :location => :version,
# :authenticated => false,
# :api_key_required => false
# }
# ])
# end
def self.configure
yield(configuration)
end

def self.configuration
@configuration ||= Configuration.new

@configuration
end

class Configuration
def initialize
@apis = []
end

def num_apis
@apis.length
end

def [] (key = nil)
if key.nil? && @apis.length == 1
return @apis[0]
end

unless @apis.include? key
self.add_api(name: key)
end

if key.is_a? Integer and key < @apis.length
return @apis[key]
end

if key.is_a? String
return @apis.detect {|e| e.name == key}
end

nil
end

def add_api(name: 'Default', environments: nil, endpoints: nil)
api_config = ApiConfiguration.new name
unless environments == nil
api_config.environments = environments
end

unless endpoints == nil
api_config.endpoints = endpoints
end

@apis.push(api_config)
end
end

##
# The configuration of the Bubblez rest client.
# The configuration of a single API within the Bubblez rest client.
#
# Use this class if you want to retrieve configuration values set during initialization.
#
class Configuration
def initialize
class ApiConfiguration
def initialize(name = '')
@name = name
@environments = Hash.new
@endpoints = Hash.new
end

def name
@name
end

##
# Retrieve the {RestEnvironment} object defined as part of this Configuration having a specified name.
#
Expand Down
4 changes: 3 additions & 1 deletion lib/bubblez/rest_client_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

module Bubblez
class RestClientResources
## @TODO_jwir3: This isn't ideal. We should choose an API using the Resources.new, and then simply be able to
# retrieve environments from there.
def environment(env_name = nil)
Bubblez.configuration.environment(env_name)
config.environment(env_name)
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/bubblez/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.package_name
end

def self.version_name
'1.0.1'
'2.0.0'
end

def self.version_code
Expand Down
65 changes: 35 additions & 30 deletions spec/bubblez/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,49 @@

describe 'Bubblez Configuration' do
context '#endpoints' do
context 'when we specify an environment with two endpoints' do
context 'when we specify an api with a single environment having two endpoints' do
before do
Bubblez.configure do |config|
config.environments = [
{
scheme: 'http',
host: '127.0.0.1',
port: '9002'
}
]

config.endpoints = [
{
method: :get,
location: 'categories',
name: 'get_categories',
return_type: :body_as_object,
authenticated: false,
api_key_required: true,
headers: {
'X-RapidAPI-Host': @host
}
},
{
method: :post,
location: :students,
authenticated: true,
name: 'create_student',
return_type: :body_as_object
}
]
config.add_api(name: 'BoredApi',
environments: [
{
scheme: 'http',
host: '127.0.0.1',
port: '9002'
}
],
endpoints: [
{
method: :get,
location: 'categories',
name: 'get_categories',
return_type: :body_as_object,
authenticated: false,
api_key_required: true,
headers: {
'X-RapidAPI-Host': @host
}
},
{
method: :post,
location: :students,
authenticated: true,
name: 'create_student',
return_type: :body_as_object
}
]
)
end
end

it 'should return that two endpoints were defined' do
config = Bubblez.configuration
expect(config).to_not be_nil
expect(config.endpoints.length).to eq(2)
expect(config.num_apis).to eq(1)

boredApi = config['BoredApi']
expect(config[0]).to_not be_nil
expect(boredApi).to_not be_nil
end
end
end
Expand Down
Loading

0 comments on commit 0a907a9

Please sign in to comment.