Skip to content

Commit

Permalink
Merge pull request #6 from poption/release/v.1.0.0
Browse files Browse the repository at this point in the history
Added string helper methods and request body is set to be formatted as json
  • Loading branch information
Da9elKH authored Jul 31, 2020
2 parents e07c5af + dbf0d6e commit 3ed6014
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 35 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Whereby Ruby HTTP API Wrapper

Just a simple Whereby HTTP API wrapper written for Ruby projects.
This gem is a very simple [Whereby](https://whereby.com) HTTP API wrapper written for Ruby/Rails projects, to easily create video meetings and conferences.

## Installation

Expand All @@ -20,16 +20,18 @@ Or install it yourself as:

## Usage

If using Rails put the following into a initializer. If just Ruby, run this before trying to use the API.
If using Rails put the following into an initializer. If you use plain Ruby, run this code before trying to use the API.

```ruby
require 'whereby'
Whereby.configure do |config|
config.api_key = 'YOUR_KEY_HERE'
config.api_key = "Your api key!"
end
```
By default, this gem try to read the `WHEREBY_API_KEY` environment variable and use that as api key. The above statement is therefore equivalent to setting this environment variable.

### Instantiate an API object.

### Instantiate an API object

```ruby
api = Whereby::Api.new
Expand All @@ -42,8 +44,12 @@ api.meeting(id) # GET /v1/meetings/:id
api.create_meeting(options) # POST /v1/meetings
api.delete_meeting(id) # DELETE /v1/meetings/:id
```
Read more about the different endpoints and options at https://whereby.dev/http-api/

Read more about the different endpoints at https://whereby.dev/http-api/
### Example creating a meeting
```ruby
api.create_meeting(room_name_prefix: '/example', room_mode: "normal", start_date: "2020-08-01T00:00:00Z", end_date: "2020-08-01T15:00:00Z", fields: ["hostRoomUrl"])
```

## Contributing

Expand Down
19 changes: 18 additions & 1 deletion lib/whereby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module Whereby
class << self
attr_accessor :configuration
attr_writer :configuration
end

def self.api_key
Expand All @@ -20,6 +20,23 @@ def self.configuration
def self.configure
yield(configuration)
end
end

class String
def whereby_underscore
gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
.tr('-', '_')
.downcase
end

def whereby_camelize
split('_').collect(&:capitalize).join
end

def whereby_lower_camelize
res = whereby_camelize
res[0].downcase + res[1..-1]
end
end
50 changes: 25 additions & 25 deletions lib/whereby/api.rb
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
require 'httparty'
require 'json'

module Whereby
class WherebyError < StandardError; end

class Api
API_VERSION = 'v1'
BASE_URL = "https://api.whereby.dev/#{API_VERSION}"
API_VERSION = 'v1'.freeze
BASE_URL = "https://api.whereby.dev/#{API_VERSION}".freeze

# GET /v1/meetings/:id
def meeting(id)
Meeting.new(whereby_request(:get, "meetings/#{id.to_s}"))
Meeting.new(whereby_request(:get, "meetings/#{id}"))
end

# POST /v1/meetings/
def create_meeting(options={})
Meeting.new(whereby_request(:post, "meetings", options))
def create_meeting(**options)
Meeting.new(whereby_request(:post, 'meetings', options))
end

# DELETE /v1/meetings/:id
def delete_meeting(id)
whereby_request(:delete, "meetings/#{id.to_s}")
whereby_request(:delete, "meetings/#{id}")
end

private
def whereby_request(method, url, options={})
url = "#{BASE_URL}/#{url}"

# Convert hash keys into camel case
options = Hash[options.map { |k,v| [k.to_s.camelize, v]}]

http_attrs = {headers: headers}
http_attrs.merge!(options)

# TODO: Create case/when for special cases for options

result = HTTParty.send(method, url, http_attrs)
def whereby_request(method, url, options = {})
url = "#{BASE_URL}/#{url}"
result = HTTParty.send(method, url, { headers: headers, body: body(options) })

if [200, 201].include? result.code
JSON.parse(result.body) # Create / Get
JSON.parse(result.body)
elsif result.code == 204
true # Delete
true
else
raise WherebyError.new error(result.code)
end
end

def error(code)
return "Access token is missing or invalid" if code == 401
return "The requested resource doesn't exist" if code == 404
"Major fuck-up."
def body(options)
Hash[options.map { |k, v| [k.to_s.whereby_lower_camelize, v] }].to_json
end

def headers
{
"Content-type" => "application/json",
"Authorization" => "Bearer #{Whereby.api_key}"
'Content-type' => 'application/json',
'Authorization' => "Bearer #{Whereby.api_key}"
}
end

def error(code)
return 'Access token is missing or invalid' if code == 401
return 'The requested resource does not exist' if code == 404

"The API resulted in an unknown status code: #{code}"
end

end
end
2 changes: 1 addition & 1 deletion lib/whereby/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Configuration
attr_accessor :api_key

def initialize
@api_key = nil
@api_key = ENV["WHEREBY_API_KEY"]
end
end
end
2 changes: 1 addition & 1 deletion lib/whereby/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Whereby
class Element
def initialize(attrs = {})
# Only set attributes if the method exists
attrs.each{ |k,v| send("#{k.underscore}=", v) if respond_to?("#{k.underscore}") }
attrs.each{ |k,v| send("#{k.whereby_underscore}=", v) if respond_to?(k.whereby_underscore.to_s) }
end

def to_h
Expand Down
2 changes: 1 addition & 1 deletion lib/whereby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Whereby
VERSION = "0.1.4"
VERSION = "1.0.0"
end
2 changes: 1 addition & 1 deletion whereby-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.email = ["[email protected]"]

spec.summary = "A Ruby Whereby Wrapper"
spec.description = "Easily use Whereby's HTTP API using this gem"
spec.description = "Easily use Whereby's HTTP API to create video rooms and conferences"
spec.homepage = "https://github.com/poption/whereby-ruby"
spec.license = "MIT"

Expand Down

0 comments on commit 3ed6014

Please sign in to comment.