Class: Protolink::Protonet

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#channelsObject

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(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]
  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

#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(options)
                   = options[:login] || raise(ArgumentError, "Please provide a login for this user")
  password              = options[:password]
  name                  = options[:name]
  email                 = options[:email] || raise(ArgumentError, "Please provide an email for this user")
  avatar_url            = options[:avatar_url]
  external_profile_url  = options[:external_profile_url]
  channels              = options[:channels]
  if channels
    # not implemented yet
    no_channels = "true"
  else
    no_channels = "false"
  end
  post('/api/v1/users', :body => {: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 } )
  ()
end

#current_userObject



38
39
40
# File 'lib/protolink/protonet.rb', line 38

def current_user
  @current_user ||= (@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, options = {})
  find_channel_by_name(name) || create_channel({:name => name}.merge(options))
end

#find_or_create_user_by_login(login, options = {}) ⇒ Object



127
128
129
# File 'lib/protolink/protonet.rb', line 127

def (, options = {})
  () || create_user({:login => }.merge(options))
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 ()
  response = get("/api/v1/users/find_by_login/#{}")
  User.new(self, response) if response
end

#global_channelsObject



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

#nodeObject



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

#usersObject

Get an array of all the available users



102
103
104
105
106
# File 'lib/protolink/protonet.rb', line 102

def users
  get('/api/v1/users.json').map do |user|
    User.new(self, user)
  end
end