lib/protolink/protonet.rb in protolink-0.2.6 vs lib/protolink/protonet.rb in protolink-0.2.7

- old
+ new

@@ -15,37 +15,38 @@ # == Options: # * +:username+: your api users username # * +:password+: your api users password # * +:proxy+: a hash with your proxy options (e.g. {:uri => 'http://user:pass@example.com', :port => 8800}) # - def self.open(uri, username, password, proxy = nil) + def self.open(uri, username = nil, password = nil, proxy = nil) # this allows you to use the httparty class helpers (base_uri...) with multiple connections clazz = self.dup clazz.base_uri(uri) - clazz.basic_auth(username, password) + clazz.basic_auth(username, password) if username && password if proxy clazz.http_proxy(proxy[:uri], proxy[:port]) end clazz.new end # CHANNELS # Get an array of all the available channels def channels - get('/api/v1/channels.json').map do |channel| + get('/api/v1/channels').map do |channel| Channel.new(self, channel) end end # Creates and returns a new Channel with the given +name+ and optionally a +description+ def create_channel(options={}) name = options[:name] || raise(ArgumentError, "Please provide a name for the channel") description = options[:description] display_name = options[:display_name] skip_autosubscribe = options[:skip_autosubscribe] - post('/api/v1/channels', :body => { :name => name, :description => description, :display_name => display_name, :skip_autosubscribe => skip_autosubscribe } ) + global = options[:global] + post('/api/v1/channels', :body => { :name => name, :description => description, :display_name => display_name, :skip_autosubscribe => skip_autosubscribe, :global => global } ) find_channel_by_name(name) end def find_or_create_channel_by_name(name, options = {}) find_channel_by_name(name) || create_channel({:name => name}.merge(options)) @@ -57,24 +58,37 @@ Channel.new(self, response) if response end # Find a Channel by name def find_channel_by_name(name) - response = get("/api/v1/channels/#{name}") + response = get("/api/v1/channels/find_by_name/#{name}") Channel.new(self, response) if response end + def find_channel_by_uuid(uuid) + response = get("/api/v1/channels/find_by_uuid/#{uuid}") + Channel.new(self, response) if response + end + def create_rendezvous(first_user_id, second_user_id) response = post('/api/v1/rendezvous', :body => { :first_user_id => first_user_id, :second_user_id => second_user_id } ) Channel.new(self, response) if response end def find_rendezvous(first_user_id, second_user_id) response = get('/api/v1/rendezvous', :query => {:first_user_id => first_user_id, :second_user_id => second_user_id}) Channel.new(self, response) if response end + def global_channels + response = get('/api/v1/channels?global=true') + + response && response.map do |channel| + Channel.new(self, channel) + end + end + # USERS # Get an array of all the available users def users get('/api/v1/users.json').map do |user| @@ -116,14 +130,26 @@ User.new(self, response) if response end # LISTENS def create_listen(user_id, channel_id) - post('/api/v1/listens', :body => {:user_id => user_id, :channel_id => channel_id } ) + channel_query = channel_id.to_s.match("-") ? {:channel_uuid => channel_id} : {:channel_id => channel_id} + post('/api/v1/listens', :body => {:user_id => user_id}.merge(channel_query) ) end def destroy_listen(user_id, channel_id) - delete('/api/v1/listens', :body => {:user_id => user_id, :channel_id => channel_id } ) + channel_query = channel_id.match("-") ? {:channel_uuid => channel_id} : {:channel_id => channel_id} + delete('/api/v1/listens', :body => {:user_id => user_id}.merge(channel_query) ) + end + + def couple(node_data) + response = post("/api/v1/couplings", :body => {:node_data => node_data}) + [User.new(self, response[0]), response[1]] if response + end + + def node + response = get("/api/v1/nodes/1") + Node.new(self, response) if response end [:get, :post, :update, :delete].each do |method| class_eval <<-EOS def #{method}(uri, options = {})