Class: Protolink::Protonet
- Inherits:
-
Object
- Object
- Protolink::Protonet
- Includes:
- HTTParty
- Defined in:
- lib/protolink/protonet.rb
Overview
protonet = Proto:Protonet.new ‘domain’, :user => ‘john.doe’, :password => ‘secret’, :token => ‘xyz’
channel = protonet.find_channel_by_name 'home'
channel.speak 'Hello world!'
Defined Under Namespace
Classes: ApiException
Class Method Summary collapse
-
.open(uri, username = nil, password = nil, proxy = nil) ⇒ Object
Create a new connection to the account with the given
uri
.
Instance Method Summary collapse
-
#channels ⇒ Object
Get an array of all the available channels.
- #couple(node_data) ⇒ Object
-
#create_channel(options = {}) ⇒ Object
Creates and returns a new Channel with the given
name
and optionally adescription
. -
#create_listen(user_id, channel_id) ⇒ Object
LISTENS.
- #create_rendezvous(first_user_id, second_user_id) ⇒ Object
-
#create_user(options) ⇒ Object
Creates and returns a new user with the given attributes.
- #current_user ⇒ Object
- #destroy_listen(user_id, channel_id) ⇒ Object
-
#find_channel(id) ⇒ Object
Find a Channel by id.
-
#find_channel_by_name(name) ⇒ Object
Find a Channel by name.
- #find_channel_by_uuid(uuid) ⇒ Object
- #find_or_create_channel_by_name(name, options = {}) ⇒ Object
- #find_or_create_user_by_login(login, options = {}) ⇒ Object
- #find_rendezvous(first_user_id, second_user_id) ⇒ Object
-
#find_user(id) ⇒ Object
Find a user by id.
- #find_user_by_login(login) ⇒ Object
- #global_channels ⇒ Object
-
#initialize(username) ⇒ Protonet
constructor
CHANNELS.
- #node ⇒ Object
- #socket(&blk) ⇒ Object
-
#users ⇒ Object
Get an array of all the available users.
Constructor Details
#initialize(username) ⇒ Protonet
CHANNELS
33 34 35 36 |
# File 'lib/protolink/protonet.rb', line 33 def initialize(username) @current_user_name = username super() end |
Class Method Details
.open(uri, username = nil, password = nil, proxy = nil) ⇒ Object
Create a new connection to the account with the given uri
.
Options:
-
:username
: your api users username -
:password
: your api users password -
:proxy
: a hash with your proxy options (e.g. => ‘user:[email protected]’, :port => 8800)
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/protolink/protonet.rb', line 20 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) if username && password if proxy clazz.http_proxy(proxy[:uri], proxy[:port]) end clazz.new(username) end |
Instance Method Details
#channels ⇒ Object
Get an array of all the available channels
43 44 45 46 47 |
# File 'lib/protolink/protonet.rb', line 43 def channels get('/api/v1/channels').map do |channel| Channel.new(self, channel) end end |
#couple(node_data) ⇒ Object
153 154 155 156 |
# File 'lib/protolink/protonet.rb', line 153 def couple(node_data) response = post("/api/v1/couplings", :body => {:node_data => node_data}) [User.new(self, response[0]), response[1]] if response end |
#create_channel(options = {}) ⇒ Object
Creates and returns a new Channel with the given name
and optionally a description
50 51 52 53 54 55 56 57 58 |
# File 'lib/protolink/protonet.rb', line 50 def create_channel(={}) name = [:name] || raise(ArgumentError, "Please provide a name for the channel") description = [:description] display_name = [:display_name] skip_autosubscribe = [:skip_autosubscribe] global = [: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 |
#create_listen(user_id, channel_id) ⇒ Object
LISTENS
143 144 145 146 |
# File 'lib/protolink/protonet.rb', line 143 def create_listen(user_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 |
#create_rendezvous(first_user_id, second_user_id) ⇒ Object
81 82 83 84 |
# File 'lib/protolink/protonet.rb', line 81 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 |
#create_user(options) ⇒ Object
Creates and returns a new user with the given attributes
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/protolink/protonet.rb', line 109 def create_user() login = [:login] || raise(ArgumentError, "Please provide a login for this user") password = [:password] name = [:name] email = [:email] || raise(ArgumentError, "Please provide an email for this user") avatar_url = [:avatar_url] external_profile_url = [:external_profile_url] channels = [:channels] if channels # not implemented yet no_channels = "true" else no_channels = "false" end post('/api/v1/users', :body => {:login => login, :name => name, :password => password, :email => email, :avatar_url => avatar_url, :no_channels => no_channels, :channels_to_subscribe => nil, :external_profile_url => external_profile_url } ) find_user_by_login(login) end |
#current_user ⇒ Object
38 39 40 |
# File 'lib/protolink/protonet.rb', line 38 def current_user @current_user ||= find_user_by_login(@current_user_name) end |
#destroy_listen(user_id, channel_id) ⇒ Object
148 149 150 151 |
# File 'lib/protolink/protonet.rb', line 148 def destroy_listen(user_id, channel_id) channel_query = channel_id.to_s.match("-") ? {:channel_uuid => channel_id} : {:channel_id => channel_id} delete('/api/v1/listens', :body => {:user_id => user_id}.merge(channel_query) ) end |
#find_channel(id) ⇒ Object
Find a Channel by id
65 66 67 68 |
# File 'lib/protolink/protonet.rb', line 65 def find_channel(id) response = get("/api/v1/channels/#{id}") Channel.new(self, response) if response end |
#find_channel_by_name(name) ⇒ Object
Find a Channel by name
71 72 73 74 |
# File 'lib/protolink/protonet.rb', line 71 def find_channel_by_name(name) response = get("/api/v1/channels/find_by_name/#{name}") Channel.new(self, response) if response end |
#find_channel_by_uuid(uuid) ⇒ Object
76 77 78 79 |
# File 'lib/protolink/protonet.rb', line 76 def find_channel_by_uuid(uuid) response = get("/api/v1/channels/find_by_uuid/#{uuid}") Channel.new(self, response) if response end |
#find_or_create_channel_by_name(name, options = {}) ⇒ Object
60 61 62 |
# File 'lib/protolink/protonet.rb', line 60 def find_or_create_channel_by_name(name, = {}) find_channel_by_name(name) || create_channel({:name => name}.merge()) end |
#find_or_create_user_by_login(login, options = {}) ⇒ Object
127 128 129 |
# File 'lib/protolink/protonet.rb', line 127 def find_or_create_user_by_login(login, = {}) find_user_by_login(login) || create_user({:login => login}.merge()) end |
#find_rendezvous(first_user_id, second_user_id) ⇒ Object
86 87 88 89 |
# File 'lib/protolink/protonet.rb', line 86 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 |
#find_user(id) ⇒ Object
Find a user by id
132 133 134 135 |
# File 'lib/protolink/protonet.rb', line 132 def find_user(id) response = get("/api/v1/users/#{id}") User.new(self, response) if response end |
#find_user_by_login(login) ⇒ Object
137 138 139 140 |
# File 'lib/protolink/protonet.rb', line 137 def find_user_by_login(login) response = get("/api/v1/users/find_by_login/#{login}") User.new(self, response) if response end |
#global_channels ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/protolink/protonet.rb', line 91 def global_channels response = get('/api/v1/channels?global=true') response && response.map do |channel| Channel.new(self, channel) end end |
#node ⇒ Object
158 159 160 161 |
# File 'lib/protolink/protonet.rb', line 158 def node response = get("/api/v1/nodes/1") Node.new(self, response) if response end |
#socket(&blk) ⇒ Object
163 164 165 166 167 |
# File 'lib/protolink/protonet.rb', line 163 def socket(&blk) EventMachine.run { EventMachine.connect URI.parse(self.class.base_uri).host, 5000, ProtoSocket, self, blk } end |