Skip to content
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

Add support for writing to read-only namespaces #2

Merged
merged 3 commits into from Feb 2, 2013
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,15 @@ response.body # => { 'name' => "-INOQPH-aV_psbk3ZXEX" }
response.raw_body # => '{"name":"-INOQPH-aV_psbk3ZXEX"}'
```

If you have a read-only namespace, set your secret key as follows:
```ruby
Firebase.base_uri = 'http://gamma.firebase.com/youruser'
Firebase.key = 'yoursecretkey'

response = Firebase.push("todos", { :name => 'Pick the milk', :priority => 1 })
```


So far, supported methods are:

```ruby
Expand Down
6 changes: 5 additions & 1 deletion lib/firebase.rb
Expand Up @@ -4,13 +4,17 @@ module Firebase
autoload :Request, 'firebase/request'

class << self
attr_accessor :base_uri
attr_accessor :base_uri, :key

def base_uri=(other)
other = other + "/" if other[-1] != "/"
@base_uri = other
end

def key=(key)
@key = key
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attr_accessor should autogenerate this method for you.


# Writes and returns the data
# Firebase.set('users/info', { 'name' => 'Oscar' }) => { 'name' => 'Oscar' }
def set(path, data)
Expand Down
15 changes: 11 additions & 4 deletions lib/firebase/request.rb
@@ -1,5 +1,6 @@
require 'typhoeus'
require 'json'
require 'uri'

module Firebase
class Request
Expand All @@ -22,15 +23,21 @@ def delete(path)
process(:delete, path)
end

def build_url(path)
host = Firebase.base_uri
path = "#{path}.json"
query_string = Firebase.key ? "?key=#{Firebase.key}" : ""
url = URI.join(Firebase.base_uri, path, query_string)

url.to_s
end

private

def process(method, path, options={})
raise "Please set Firebase.base_uri before making requests" unless Firebase.base_uri

url = URI.join(Firebase.base_uri, path)
url = [url, '.json'].compact.join

request = Typhoeus::Request.new(url.to_s,
request = Typhoeus::Request.new(build_url(path),
:body => options[:body],
:method => method)
hydra = Typhoeus::Hydra.new
Expand Down
28 changes: 28 additions & 0 deletions spec/firebase_request_spec.rb
@@ -0,0 +1,28 @@
require 'spec_helper'

describe "Firebase Request" do

describe "url_builder" do
it "should build the correct url when passed no path" do
Firebase.base_uri = 'https://test.firebaseio.com'
Firebase::Request.build_url(nil).should == 'https://test.firebaseio.com/.json'
end
end

describe "url_builder" do
it "should build the correct url when passed a path" do
Firebase.base_uri = 'https://test.firebaseio.com'

Firebase::Request.build_url('users/eugene').should == 'https://test.firebaseio.com/users/eugene.json'
end
end

describe "url_builder" do
it "should include a key in the query string, if configured" do
Firebase.base_uri = 'https://test.firebaseio.com'
Firebase.key = 'secretkey'

Firebase::Request.build_url('users/eugene').should == 'https://test.firebaseio.com/users/eugene.json?key=secretkey'
end
end
end