From dc382ae0f26304b3f9dac2e173c1e28d82489277 Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 20:35:11 +0200 Subject: [PATCH 1/9] Camelize and underscore helper methods for strings i plain ruby --- lib/whereby.rb | 19 ++++++++++++++++++- lib/whereby/element.rb | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/whereby.rb b/lib/whereby.rb index 3836d25..f718cb7 100644 --- a/lib/whereby.rb +++ b/lib/whereby.rb @@ -6,7 +6,7 @@ module Whereby class << self - attr_accessor :configuration + attr_writer :configuration end def self.api_key @@ -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 \ No newline at end of file diff --git a/lib/whereby/element.rb b/lib/whereby/element.rb index 5e0f3bf..b99a0f1 100644 --- a/lib/whereby/element.rb +++ b/lib/whereby/element.rb @@ -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 From 9c5401fd3ffac5e0916ae0ad36e5339113b64e0b Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 20:35:58 +0200 Subject: [PATCH 2/9] Fixed API to send body as json --- lib/whereby/api.rb | 50 +++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/whereby/api.rb b/lib/whereby/api.rb index 1773dd9..9d47e6f 100644 --- a/lib/whereby/api.rb +++ b/lib/whereby/api.rb @@ -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 a unknown status code #{code}" + end + end end \ No newline at end of file From cc5abc525154484670b75c2d9430497fc9ca1e74 Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 20:40:13 +0200 Subject: [PATCH 3/9] Updated README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a9afff6..0db30f4 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Whereby.configure do |config| end ``` -### Instantiate an API object. +### Instantiate an API object ```ruby api = Whereby::Api.new @@ -42,8 +42,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 From 890212240fcd61532982262607d25b2d1349d25f Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 20:43:34 +0200 Subject: [PATCH 4/9] Updated README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0db30f4..ea6207f 100644 --- a/README.md +++ b/README.md @@ -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 easly create video meetings and conferences. ## Installation @@ -20,12 +20,12 @@ 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 = ENV['YOUR_KEY'] end ``` From 8806e0add50f135b190c21947d2e7bbd7f8faecf Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 20:44:57 +0200 Subject: [PATCH 5/9] Updated version and gemspec --- lib/whereby/version.rb | 2 +- whereby-ruby.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/whereby/version.rb b/lib/whereby/version.rb index ea845c4..8ae80de 100644 --- a/lib/whereby/version.rb +++ b/lib/whereby/version.rb @@ -1,3 +1,3 @@ module Whereby - VERSION = "0.1.4" + VERSION = "1.0.0" end diff --git a/whereby-ruby.gemspec b/whereby-ruby.gemspec index a277209..8d6115d 100644 --- a/whereby-ruby.gemspec +++ b/whereby-ruby.gemspec @@ -10,7 +10,7 @@ Gem::Specification.new do |spec| spec.email = ["tech@poption.com"] 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" From e6f3e59246107475aa4e298f21f48d9d2bce47c4 Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 20:52:44 +0200 Subject: [PATCH 6/9] Typo --- lib/whereby/api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/whereby/api.rb b/lib/whereby/api.rb index 9d47e6f..276b3d6 100644 --- a/lib/whereby/api.rb +++ b/lib/whereby/api.rb @@ -53,7 +53,7 @@ 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 a unknown status code #{code}" + "The API resulted in an unknown status code: #{code}" end end From ce46f1db71e6da4c3069eac8142fc9c9d49b5e54 Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 21:01:58 +0200 Subject: [PATCH 7/9] Trying to read WHEREBY_API_KEY env variable --- lib/whereby/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/whereby/configuration.rb b/lib/whereby/configuration.rb index 2f3f18f..10dc507 100644 --- a/lib/whereby/configuration.rb +++ b/lib/whereby/configuration.rb @@ -3,7 +3,7 @@ class Configuration attr_accessor :api_key def initialize - @api_key = nil + @api_key = ENV["WHEREBY_API_KEY"] end end end \ No newline at end of file From db54634a1adac4364aba89a7ee3875d16ed9bfd0 Mon Sep 17 00:00:00 2001 From: Daniel Kittilsen Henriksen Date: Thu, 30 Jul 2020 21:02:02 +0200 Subject: [PATCH 8/9] Updated README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea6207f..0614e6d 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,11 @@ If using Rails put the following into an initializer. If you use plain Ruby, run ```ruby require 'whereby' Whereby.configure do |config| - config.api_key = ENV['YOUR_KEY'] + 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 From dbf0d6e67958c7eddc43cb71e116f6454a42cdc3 Mon Sep 17 00:00:00 2001 From: Daniel Henriksen Date: Fri, 31 Jul 2020 09:11:06 +0200 Subject: [PATCH 9/9] Typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0614e6d..cd615ce 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Whereby Ruby HTTP API Wrapper -This gem is a very simple [Whereby](https://whereby.com) HTTP API wrapper written for Ruby/Rails projects, to easly create video meetings and conferences. +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