From 3f63e33283cdadee27c4bf6727bb1a5380adb3c0 Mon Sep 17 00:00:00 2001 From: Edwin Rozario Date: Fri, 26 Jan 2018 10:26:01 +1300 Subject: [PATCH 1/5] Created version file and updated gemspec --- firebase.gemspec | 51 +++++++++++++++++++---------------------- lib/firebase/version.rb | 3 +++ 2 files changed, 26 insertions(+), 28 deletions(-) create mode 100644 lib/firebase/version.rb diff --git a/firebase.gemspec b/firebase.gemspec index 15130b9..d614dcb 100644 --- a/firebase.gemspec +++ b/firebase.gemspec @@ -1,31 +1,26 @@ -Gem::Specification.new do |s| - s.name = "firebase" - s.version = "0.2.7" +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'firebase/version' - s.require_paths = ["lib"] - s.authors = ["Oscar Del Ben"] - s.date = "2015-11-26" - s.description = "Firebase wrapper for Ruby" - s.email = "info@oscardelben.com" - s.extra_rdoc_files = [ - "CHANGELOG.md", - "LICENSE.txt", - "README.md" - ] - s.files = [ - "lib/firebase.rb", - "lib/firebase/response.rb", - "lib/firebase/server_value.rb" - ] - s.homepage = "http://github.com/oscardelben/firebase-ruby" - s.licenses = ["MIT"] - s.summary = "Firebase wrapper for Ruby" +Gem::Specification.new do |spec| + spec.name = 'firebase' + spec.version = Firebase::VERSION - s.add_runtime_dependency 'httpclient', '>= 2.5.3' - s.add_runtime_dependency 'json' - s.add_runtime_dependency 'googleauth' - s.add_development_dependency 'rake' - s.add_development_dependency 'rdoc' - s.add_development_dependency 'rspec' -end + spec.require_paths = ['lib'] + spec.authors = ['Oscar Del Ben'] + spec.date = '2015-11-26' + spec.description = 'Firebase wrapper for Ruby' + spec.email = 'info@oscardelben.com' + spec.extra_rdoc_files = %w[CHANGELOG.md LICENSE.txt README.md] + spec.files = `git ls-files lib/`.split($/) + spec.homepage = 'http://github.com/oscardelben/firebase-ruby' + spec.licenses = ['MIT'] + spec.summary = 'Firebase wrapper for Ruby' + spec.add_runtime_dependency 'httpclient', '>= 2.5.3' + spec.add_runtime_dependency 'json' + spec.add_runtime_dependency 'googleauth' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'rdoc' + spec.add_development_dependency 'rspec' +end diff --git a/lib/firebase/version.rb b/lib/firebase/version.rb new file mode 100644 index 0000000..230d54e --- /dev/null +++ b/lib/firebase/version.rb @@ -0,0 +1,3 @@ +module Firebase + VERSION = '0.2.7'.freeze +end \ No newline at end of file From 28e0e63959034811f028492efe8f0e92a270ac6b Mon Sep 17 00:00:00 2001 From: Edwin Rozario Date: Mon, 29 Jan 2018 11:24:27 +1300 Subject: [PATCH 2/5] - Moved client to a different file - Added a request class for firebase - Fixed specs for the above changes --- firebase.gemspec | 2 +- lib/firebase.rb | 84 +---------------------------------------- lib/firebase/client.rb | 43 +++++++++++++++++++++ lib/firebase/request.rb | 51 +++++++++++++++++++++++++ lib/firebase/version.rb | 2 +- spec/firebase_spec.rb | 26 +++++++------ spec/spec_helper.rb | 1 + 7 files changed, 113 insertions(+), 96 deletions(-) create mode 100644 lib/firebase/client.rb create mode 100644 lib/firebase/request.rb diff --git a/firebase.gemspec b/firebase.gemspec index d614dcb..484ed20 100644 --- a/firebase.gemspec +++ b/firebase.gemspec @@ -8,7 +8,6 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.authors = ['Oscar Del Ben'] - spec.date = '2015-11-26' spec.description = 'Firebase wrapper for Ruby' spec.email = 'info@oscardelben.com' spec.extra_rdoc_files = %w[CHANGELOG.md LICENSE.txt README.md] @@ -23,4 +22,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake' spec.add_development_dependency 'rdoc' spec.add_development_dependency 'rspec' + spec.add_development_dependency 'pry' end diff --git a/lib/firebase.rb b/lib/firebase.rb index 17b1db6..e264bca 100644 --- a/lib/firebase.rb +++ b/lib/firebase.rb @@ -1,85 +1,5 @@ require 'firebase/response' +require 'firebase/request' require 'firebase/server_value' -require 'googleauth' -require 'httpclient' -require 'json' -require 'uri' +require 'firebase/client' -module Firebase - class Client - attr_reader :auth, :request - - def initialize(base_uri, auth=nil) - if base_uri !~ URI::regexp(%w(https)) - raise ArgumentError.new('base_uri must be a valid https uri') - end - base_uri += '/' unless base_uri.end_with?('/') - default_header = { - 'Content-Type' => 'application/json' - } - if auth && valid_json?(auth) - # Using Admin SDK private key - scopes = %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email) - credentials = Google::Auth::DefaultCredentials.make_creds(json_key_io: StringIO.new(auth), scope: scopes) - default_header = credentials.apply(default_header) - else - # Using deprecated Database Secret - @auth = auth - end - @request = HTTPClient.new({ - :base_url => base_uri, - :default_header => default_header - }) - end - - # Writes and returns the data - # Firebase.set('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } - def set(path, data, query={}) - process :put, path, data, query - end - - # Returns the data at path - def get(path, query={}) - process :get, path, nil, query - end - - # Writes the data, returns the key name of the data added - # Firebase.push('users', { 'age' => 18}) => {"name":"-INOQPH-aV_psbk3ZXEX"} - def push(path, data, query={}) - process :post, path, data, query - end - - # Deletes the data at path and returs true - def delete(path, query={}) - process :delete, path, nil, query - end - - # Write the data at path but does not delete ommited children. Returns the data - # Firebase.update('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } - def update(path, data, query={}) - process :patch, path, data, query - end - - private - - def process(verb, path, data=nil, query={}) - if path[0] == '/' - raise(ArgumentError.new("Invalid path: #{path}. Path must be relative")) - end - Firebase::Response.new @request.request(verb, "#{path}.json", { - :body => (data && data.to_json), - :query => (@auth ? { :auth => @auth }.merge(query) : query), - :follow_redirect => true - }) - end - - def valid_json?(json) - begin - JSON.parse(json) - return true - rescue JSON::ParserError - return false - end - end - end -end diff --git a/lib/firebase/client.rb b/lib/firebase/client.rb new file mode 100644 index 0000000..1b93eea --- /dev/null +++ b/lib/firebase/client.rb @@ -0,0 +1,43 @@ +module Firebase + class Client + attr_reader :auth, :request + + def initialize(base_uri, auth = nil) + @request = Request.new(uri: base_uri, + auth: auth) + end + + # Writes and returns the data + # Firebase.set('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } + def set(path, data, query = {}) + request.execute(method: :put, path: path, + data: data, query: query) + end + + # Returns the data at path + def get(path, query = {}) + request.execute(method: :get, path: path, + query: query) + end + + # Writes the data, returns the key name of the data added + # Firebase.push('users', { 'age' => 18}) => {"name":"-INOQPH-aV_psbk3ZXEX"} + def push(path, data, query = {}) + request.execute(method: :post, path: path, + data: data, query: query) + end + + # Deletes the data at path and returs true + def delete(path, query = {}) + request.execute(method: :delete, path: path, + query: query) + end + + # Write the data at path but does not delete ommited children. Returns the data + # Firebase.update('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } + def update(path, data, query = {}) + request.execute(method: :patch, path: path, + data: data, query: query) + end + end +end \ No newline at end of file diff --git a/lib/firebase/request.rb b/lib/firebase/request.rb new file mode 100644 index 0000000..e637f70 --- /dev/null +++ b/lib/firebase/request.rb @@ -0,0 +1,51 @@ +require 'uri' +require 'httpclient' +require 'googleauth' +require 'json' + +module Firebase + class Request + attr_reader :http_client, :auth + + def initialize(uri:, auth:, headers: { 'Content-Type': 'application/json' }) + raise ArgumentError.new('base_uri must be a valid https uri') if uri !~ URI::regexp(%w(https)) + uri += '/' unless uri.end_with?('/') + + if valid_auth?(auth) + # Using Admin SDK private key + scopes = %w(https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email) + credentials = Google::Auth::DefaultCredentials.make_creds(json_key_io: StringIO.new(auth), scope: scopes) + headers = credentials.apply(headers) + else + # Using deprecated Database Secret + @auth = auth + end + + @http_client = HTTPClient.new({ base_url: uri, + default_header: headers }) + end + + def execute(method:, path:, data: nil, query: {}) + raise ArgumentError.new("Invalid path: #{path}. Path must be relative") if path.start_with? '/' + + params = { body: (data && data.to_json), + query: (@auth ? { auth: @auth }.merge(query) : query), + follow_redirect: true } + + Firebase::Response.new(http_client.request(method, "#{path}.json", params)) + end + + private + + def valid_auth?(auth) + return false unless auth + + JSON.parse(auth) + return true + rescue TypeError + return false + rescue JSON::ParserError + return false + end + end +end \ No newline at end of file diff --git a/lib/firebase/version.rb b/lib/firebase/version.rb index 230d54e..a1826dc 100644 --- a/lib/firebase/version.rb +++ b/lib/firebase/version.rb @@ -1,3 +1,3 @@ module Firebase VERSION = '0.2.7'.freeze -end \ No newline at end of file +end diff --git a/spec/firebase_spec.rb b/spec/firebase_spec.rb index 5a2d2b7..d4a3354 100644 --- a/spec/firebase_spec.rb +++ b/spec/firebase_spec.rb @@ -26,14 +26,14 @@ describe "set" do it "writes and returns the data" do - expect(@firebase).to receive(:process).with(:put, 'users/info', data, {}) + expect(@firebase.request).to receive(:execute).with(method: :put, path: 'users/info', data: data, query: {}) @firebase.set('users/info', data) end end describe "get" do it "returns the data" do - expect(@firebase).to receive(:process).with(:get, 'users/info', nil, {}) + expect(@firebase.request).to receive(:execute).with(method: :get, path: 'users/info', query: {}) @firebase.get('users/info') end @@ -42,7 +42,7 @@ :orderBy => '"$key"', :startAt => '"A1"' } - expect(@firebase).to receive(:process).with(:get, 'users/info', nil, params) + expect(@firebase.request).to receive(:execute).with(method: :get, path: 'users/info', query: params) @firebase.get('users/info', params) end @@ -104,21 +104,21 @@ describe "push" do it "writes the data" do - expect(@firebase).to receive(:process).with(:post, 'users', data, {}) + expect(@firebase.request).to receive(:execute).with(method: :post, path: 'users', data: data, query: {}) @firebase.push('users', data) end end describe "delete" do it "returns true" do - expect(@firebase).to receive(:process).with(:delete, 'users/info', nil, {}) + expect(@firebase.request).to receive(:execute).with(method: :delete, path: 'users/info', query: {}) @firebase.delete('users/info') end end describe "update" do it "updates and returns the data" do - expect(@firebase).to receive(:process).with(:patch, 'users/info', data, {}) + expect(@firebase.request).to receive(:execute).with(method: :patch, path: 'users/info', data: data, query: {}) @firebase.update('users/info', data) end end @@ -126,27 +126,29 @@ describe "http processing" do it "sends custom auth query" do firebase = Firebase::Client.new('https://test.firebaseio.com', 'secret') - expect(firebase.request).to receive(:request).with(:get, "todos.json", { + expect(firebase.request.http_client).to receive(:request).with(:get, 'todos.json', { :body => nil, :query => {:auth => "secret", :foo => 'bar'}, :follow_redirect => true }) + firebase.get('todos', :foo => 'bar') end it "sets custom auth header" do private_key = '{ "private_key": true }' credentials = double() - expect(credentials).to receive(:apply).with({ 'Content-Type' => 'application/json' }).and_return({ :authorization => 'Bearer abcdef', 'Content-Type' => 'application/json' }) + expect(credentials).to receive(:apply).with({ 'Content-Type': 'application/json' }).and_return({ authorization: 'Bearer abcdef', 'Content-Type': 'application/json' }) expect(StringIO).to receive(:new).with(private_key).and_return('StringIO private key') expect(Google::Auth::DefaultCredentials).to receive(:make_creds).with(json_key_io: 'StringIO private key', scope: instance_of(Array)).and_return(credentials) expect(HTTPClient).to receive(:new).with({ - :base_url => 'https://test.firebaseio.com/', - :default_header => { - :authorization => 'Bearer abcdef', - 'Content-Type' => 'application/json' + base_url: 'https://test.firebaseio.com/', + default_header: { + authorization: 'Bearer abcdef', + 'Content-Type': 'application/json' } }) + Firebase::Client.new('https://test.firebaseio.com/', private_key) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dfe48e7..4940603 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'rspec' require 'firebase' +require 'pry' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. From 8f73dec714e26f5423ee88d5ba1935b06979f8f9 Mon Sep 17 00:00:00 2001 From: Edwin Rozario Date: Mon, 29 Jan 2018 15:01:19 +1300 Subject: [PATCH 3/5] - Aliased Client methods - Defined Firebase::Database class to Replace CLient later - Rubocop fixes --- lib/firebase.rb | 6 ++++ lib/firebase/client.rb | 22 +++++++++++---- lib/firebase/request.rb | 14 ++++++---- lib/firebase/response.rb | 5 +++- lib/firebase/server_value.rb | 4 ++- lib/firebase/version.rb | 4 ++- spec/firebase_spec.rb | 54 ++++++++++++++++++++++++++++++++---- spec/spec_helper.rb | 4 +-- 8 files changed, 90 insertions(+), 23 deletions(-) diff --git a/lib/firebase.rb b/lib/firebase.rb index e264bca..f7c90b8 100644 --- a/lib/firebase.rb +++ b/lib/firebase.rb @@ -1,5 +1,11 @@ +# frozen_string_literal: true + require 'firebase/response' require 'firebase/request' require 'firebase/server_value' require 'firebase/client' +# Suggestion to slowly replace Client with Database +# So that we can have Firebase::Storeage, Firebase::RealtineDatabase +# in the future +Firebase::Database = Firebase::Client diff --git a/lib/firebase/client.rb b/lib/firebase/client.rb index 1b93eea..b47ae69 100644 --- a/lib/firebase/client.rb +++ b/lib/firebase/client.rb @@ -1,10 +1,13 @@ +# frozen_string_literal: true + module Firebase + # All Database interactions implemented here class Client - attr_reader :auth, :request + attr_reader :request - def initialize(base_uri, auth = nil) + def initialize(base_uri, auth = nil) @request = Request.new(uri: base_uri, - auth: auth) + auth: auth) end # Writes and returns the data @@ -33,11 +36,18 @@ def delete(path, query = {}) query: query) end - # Write the data at path but does not delete ommited children. Returns the data - # Firebase.update('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } + # Write the data at path but does not delete ommited + # children. Returns the data + # Firebase.update('users/info', + # { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } def update(path, data, query = {}) request.execute(method: :patch, path: path, data: data, query: query) end + + # Aliasing methods to match usual Ruby/Rails http methods + alias post push + alias put set + alias destroy delete end -end \ No newline at end of file +end diff --git a/lib/firebase/request.rb b/lib/firebase/request.rb index e637f70..4154ff1 100644 --- a/lib/firebase/request.rb +++ b/lib/firebase/request.rb @@ -1,14 +1,17 @@ +# frozen_string_literal: true + require 'uri' require 'httpclient' require 'googleauth' require 'json' module Firebase + # API requests to firebase API implemented here class Request attr_reader :http_client, :auth def initialize(uri:, auth:, headers: { 'Content-Type': 'application/json' }) - raise ArgumentError.new('base_uri must be a valid https uri') if uri !~ URI::regexp(%w(https)) + raise ArgumentError.new('base_uri must be a valid https uri') if uri !~ URI.regexp(%w(https)) uri += '/' unless uri.end_with?('/') if valid_auth?(auth) @@ -21,8 +24,8 @@ def initialize(uri:, auth:, headers: { 'Content-Type': 'application/json' }) @auth = auth end - @http_client = HTTPClient.new({ base_url: uri, - default_header: headers }) + @http_client = HTTPClient.new(base_url: uri, + default_header: headers) end def execute(method:, path:, data: nil, query: {}) @@ -32,7 +35,8 @@ def execute(method:, path:, data: nil, query: {}) query: (@auth ? { auth: @auth }.merge(query) : query), follow_redirect: true } - Firebase::Response.new(http_client.request(method, "#{path}.json", params)) + http_call = http_client.request(method, "#{path}.json", params) + Firebase::Response.new(http_call) end private @@ -48,4 +52,4 @@ def valid_auth?(auth) return false end end -end \ No newline at end of file +end diff --git a/lib/firebase/response.rb b/lib/firebase/response.rb index 55e5f3c..72174bb 100644 --- a/lib/firebase/response.rb +++ b/lib/firebase/response.rb @@ -1,4 +1,7 @@ +# frozen_string_literal: true + module Firebase + # Firebase response class class Response attr_accessor :response @@ -7,7 +10,7 @@ def initialize(response) end def body - JSON.parse(response.body, :quirks_mode => true) + JSON.parse(response.body, quirks_mode: true) end def raw_body diff --git a/lib/firebase/server_value.rb b/lib/firebase/server_value.rb index 02604ed..68522bc 100644 --- a/lib/firebase/server_value.rb +++ b/lib/firebase/server_value.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Firebase class ServerValue - TIMESTAMP = { ".sv" => 'timestamp' } + TIMESTAMP = { ".sv" => 'timestamp' }.freeze end end diff --git a/lib/firebase/version.rb b/lib/firebase/version.rb index a1826dc..c83e7ae 100644 --- a/lib/firebase/version.rb +++ b/lib/firebase/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Firebase - VERSION = '0.2.7'.freeze + VERSION = '0.2.7' end diff --git a/spec/firebase_spec.rb b/spec/firebase_spec.rb index d4a3354..dcf8e02 100644 --- a/spec/firebase_spec.rb +++ b/spec/firebase_spec.rb @@ -24,11 +24,43 @@ @firebase = Firebase::Client.new('https://test.firebaseio.com') end - describe "set" do - it "writes and returns the data" do + describe 'initialize request' do + it 'returns firebase request object' do + expect(@firebase.request).to be_a(Firebase::Request) + end + + it 'returns nil auth' do + expect(@firebase.request.auth).to be nil + end + + it 'returns string auth token' do + auth_firebase = Firebase::Client.new('https://test.firebaseio.com', 'fakefirebasetoken') + expect(auth_firebase.request.auth).to eq 'fakefirebasetoken' + end + + it 'returns http_client object' do + expect(@firebase.request.http_client).to be_a HTTPClient + end + + it 'returns default header' do + expect(@firebase.request.http_client.default_header).to eq({ 'Content-Type': 'application/json'}) + end + + it 'returns base_url' do + expect(@firebase.request.http_client.base_url).to eq 'https://test.firebaseio.com/' + end + end + + describe 'set' do + it 'writes and returns the data' do expect(@firebase.request).to receive(:execute).with(method: :put, path: 'users/info', data: data, query: {}) @firebase.set('users/info', data) end + + it 'writes and returns the data' do + expect(@firebase.request).to receive(:execute).with(method: :put, path: 'users/info', data: data, query: {}) + @firebase.put('users/info', data) + end end describe "get" do @@ -102,18 +134,28 @@ end end - describe "push" do - it "writes the data" do + describe 'push/post' do + it 'writes the data' do expect(@firebase.request).to receive(:execute).with(method: :post, path: 'users', data: data, query: {}) @firebase.push('users', data) end + + it 'writes the data' do + expect(@firebase.request).to receive(:execute).with(method: :post, path: 'users', data: data, query: {}) + @firebase.post('users', data) + end end - describe "delete" do - it "returns true" do + describe 'delete/destroy' do + it 'returns true' do expect(@firebase.request).to receive(:execute).with(method: :delete, path: 'users/info', query: {}) @firebase.delete('users/info') end + + it 'returns true' do + expect(@firebase.request).to receive(:execute).with(method: :delete, path: 'users/info', query: {}) + @firebase.destroy('users/info') + end end describe "update" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4940603..db3909f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,4 @@ # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} -RSpec.configure do |config| - -end +RSpec.configure do |config|; end \ No newline at end of file From f148a2aadab6b6ef13262d2f776ca4760c171243 Mon Sep 17 00:00:00 2001 From: Edwin Rozario Date: Mon, 29 Jan 2018 15:06:23 +1300 Subject: [PATCH 4/5] README updates --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee90f25..8838bf1 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ gem install firebase ```ruby base_uri = 'https://.firebaseio.com/' +firebase = Firebase::Database.new(base_uri) + +or + firebase = Firebase::Client.new(base_uri) response = firebase.push("todos", { :name => 'Pick the milk', :'.priority' => 1 }) @@ -74,10 +78,20 @@ firebase.update('', { So far, supported methods are: ```ruby -set(path, data, query_options) get(path, query_options) + +set(path, data, query_options) +or +put(path, data, query_options) + push(path, data, query_options) +or +post(path, data, query_options) + delete(path, query_options) +or +destroy(path, query_options) + update(path, data, query_options) ``` From c17bd5aeba32a1056ca935d5aa3c7d87859f3093 Mon Sep 17 00:00:00 2001 From: Edwin Rozario Date: Mon, 29 Jan 2018 18:38:35 +1300 Subject: [PATCH 5/5] Why : Ruby 1.9 dosent support hash params for a method & string to symbol keys What : Reverting these --- CHANGELOG.md | 2 +- lib/firebase/client.rb | 18 ++++++------------ lib/firebase/request.rb | 4 ++-- spec/firebase_spec.rb | 29 ++++++++++++++--------------- spec/spec_helper.rb | 2 +- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46a4ef4..d4df3ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changelog ## 0.3.8 -* Major version change as new class added handle request. +* Major version change as new class added to handle request. * Classes separated in different files. * Syntax for Hashes changed from hashrockets to symbol. * Alias for request methods defined to match HTTP methods diff --git a/lib/firebase/client.rb b/lib/firebase/client.rb index b47ae69..4bc0d0a 100644 --- a/lib/firebase/client.rb +++ b/lib/firebase/client.rb @@ -6,34 +6,29 @@ class Client attr_reader :request def initialize(base_uri, auth = nil) - @request = Request.new(uri: base_uri, - auth: auth) + @request = Request.new(base_uri, auth) end # Writes and returns the data # Firebase.set('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } def set(path, data, query = {}) - request.execute(method: :put, path: path, - data: data, query: query) + request.execute(:put, path, data, query) end # Returns the data at path def get(path, query = {}) - request.execute(method: :get, path: path, - query: query) + request.execute(:get, path, nil, query) end # Writes the data, returns the key name of the data added # Firebase.push('users', { 'age' => 18}) => {"name":"-INOQPH-aV_psbk3ZXEX"} def push(path, data, query = {}) - request.execute(method: :post, path: path, - data: data, query: query) + request.execute(:post, path, data, query) end # Deletes the data at path and returs true def delete(path, query = {}) - request.execute(method: :delete, path: path, - query: query) + request.execute(:delete, path, nil, query) end # Write the data at path but does not delete ommited @@ -41,8 +36,7 @@ def delete(path, query = {}) # Firebase.update('users/info', # { 'name' => 'Oscar' }) => { 'name' => 'Oscar' } def update(path, data, query = {}) - request.execute(method: :patch, path: path, - data: data, query: query) + request.execute(:patch, path, data, query) end # Aliasing methods to match usual Ruby/Rails http methods diff --git a/lib/firebase/request.rb b/lib/firebase/request.rb index 0b6094c..fc22425 100644 --- a/lib/firebase/request.rb +++ b/lib/firebase/request.rb @@ -10,7 +10,7 @@ module Firebase class Request attr_reader :http_client, :auth - def initialize(uri:, auth:, headers: { 'Content-Type': 'application/json' }) + def initialize(uri, auth, headers = { 'Content-Type' => 'application/json' }) raise ArgumentError.new('base_uri must be a valid https uri') if uri !~ URI.regexp(%w(https)) uri += '/' unless uri.end_with?('/') @@ -32,7 +32,7 @@ def initialize(uri:, auth:, headers: { 'Content-Type': 'application/json' }) end end - def execute(method:, path:, data: nil, query: {}) + def execute(method, path, data = nil, query = {}) raise ArgumentError.new("Invalid path: #{path}. Path must be relative") if path.start_with? '/' if @expires_at && Time.now > @expires_at diff --git a/spec/firebase_spec.rb b/spec/firebase_spec.rb index 4e11d93..9a1c30b 100644 --- a/spec/firebase_spec.rb +++ b/spec/firebase_spec.rb @@ -43,7 +43,7 @@ end it 'returns default header' do - expect(@firebase.request.http_client.default_header).to eq({ 'Content-Type': 'application/json'}) + expect(@firebase.request.http_client.default_header).to eq('Content-Type' => 'application/json') end it 'returns base_url' do @@ -53,19 +53,19 @@ describe 'set' do it 'writes and returns the data' do - expect(@firebase.request).to receive(:execute).with(method: :put, path: 'users/info', data: data, query: {}) + expect(@firebase.request).to receive(:execute).with(:put, 'users/info', data, {}) @firebase.set('users/info', data) end it 'writes and returns the data' do - expect(@firebase.request).to receive(:execute).with(method: :put, path: 'users/info', data: data, query: {}) + expect(@firebase.request).to receive(:execute).with(:put, 'users/info', data, {}) @firebase.put('users/info', data) end end describe "get" do it "returns the data" do - expect(@firebase.request).to receive(:execute).with(method: :get, path: 'users/info', query: {}) + expect(@firebase.request).to receive(:execute).with(:get, 'users/info', nil, {}) @firebase.get('users/info') end @@ -74,7 +74,7 @@ :orderBy => '"$key"', :startAt => '"A1"' } - expect(@firebase.request).to receive(:execute).with(method: :get, path: 'users/info', query: params) + expect(@firebase.request).to receive(:execute).with(:get, 'users/info', nil, params) @firebase.get('users/info', params) end @@ -136,31 +136,31 @@ describe 'push/post' do it 'writes the data' do - expect(@firebase.request).to receive(:execute).with(method: :post, path: 'users', data: data, query: {}) + expect(@firebase.request).to receive(:execute).with(:post, 'users', data, {}) @firebase.push('users', data) end it 'writes the data' do - expect(@firebase.request).to receive(:execute).with(method: :post, path: 'users', data: data, query: {}) + expect(@firebase.request).to receive(:execute).with(:post, 'users', data, {}) @firebase.post('users', data) end end describe 'delete/destroy' do it 'returns true' do - expect(@firebase.request).to receive(:execute).with(method: :delete, path: 'users/info', query: {}) + expect(@firebase.request).to receive(:execute).with(:delete, 'users/info', nil, {}) @firebase.delete('users/info') end it 'returns true' do - expect(@firebase.request).to receive(:execute).with(method: :delete, path: 'users/info', query: {}) + expect(@firebase.request).to receive(:execute).with(:delete, 'users/info', nil, {}) @firebase.destroy('users/info') end end describe "update" do it "updates and returns the data" do - expect(@firebase.request).to receive(:execute).with(method: :patch, path: 'users/info', data: data, query: {}) + expect(@firebase.request).to receive(:execute).with(:patch, 'users/info', data, {}) @firebase.update('users/info', data) end end @@ -198,7 +198,7 @@ it "sets custom auth header" do client = Firebase::Client.new('https://test.firebaseio.com/', '{ "private_key": true }') - expect(client.request.http_client.default_header).to eql('Content-Type': 'application/json', authorization: 'Bearer 1') + expect(client.request.http_client.default_header).to eql('Content-Type' => 'application/json', authorization: 'Bearer 1') end it "handles token expiry" do @@ -209,10 +209,9 @@ client.get 'dummy' - expect(client.request.http_client.default_header).to eql({ - 'Content-Type': 'application/json', - authorization: 'Bearer 2' - }) + expect(client.request.http_client.default_header).to eql( + 'Content-Type' => 'application/json', + authorization: 'Bearer 2') end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index db3909f..ef7ffb4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,4 +8,4 @@ # in ./support/ and its subdirectories. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} -RSpec.configure do |config|; end \ No newline at end of file +RSpec.configure do |config|; end