Class: Karotz::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/karotz/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interactive_id) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/karotz/client.rb', line 10

def initialize(interactive_id)
  @interactive_id = interactive_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/karotz/client.rb', line 14

def method_missing(meth, *args, &blk)
  if self.class.respond_to? meth
    args.unshift @interactive_id
    self.class.send meth, *args, &blk
  else
    super
  end
end

Instance Attribute Details

#interactive_idObject

Returns the value of attribute interactive_id.



8
9
10
# File 'lib/karotz/client.rb', line 8

def interactive_id
  @interactive_id
end

Class Method Details

.asr(interactive_id, params = {}) ⇒ Object Also known as: listen

ASR================


65
66
67
# File 'lib/karotz/client.rb', line 65

def asr(interactive_id, params={})
  request :asr, interactive_id, {:grammar => 'ruby', :lang => Language::ENGLISH}.merge(params)
end

.config(interactive_id) ⇒ Object

CONFIG=========


91
92
93
94
# File 'lib/karotz/client.rb', line 91

def config(interactive_id)
  answer = perform_request(:config, interactive_id)
  answer["ConfigResponse"]
end

.createObject Also known as: connect



137
138
139
140
# File 'lib/karotz/client.rb', line 137

def create
  interactive_id = start
  new(interactive_id)
end

.create_query(params) ⇒ Object



183
184
185
# File 'lib/karotz/client.rb', line 183

def create_query(params)
  params.map { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.sort.join('&')
end

.ears(interactive_id, params = {:reset => true}) ⇒ Object

EARS=================


31
32
33
# File 'lib/karotz/client.rb', line 31

def ears(interactive_id, params={:reset => true})
  request :ears, interactive_id, params
end

.fade(interactive_id, params = {}) ⇒ Object



46
47
48
# File 'lib/karotz/client.rb', line 46

def fade(interactive_id, params={})
  request :led, interactive_id, {:action => :fade, :color => Color::BLUE, :period => 3000}.merge(params)
end

.get(url) ⇒ Object



166
167
168
169
170
# File 'lib/karotz/client.rb', line 166

def get(url)
  request = HTTPI::Request.new(url)
  request.proxy = Configuration.proxy if Configuration.proxy
  HTTPI.get request
end

.king_of_the_jungle(interactive_id, params = {}) ⇒ Object Also known as: freak

FUN_STUFF=========


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/karotz/client.rb', line 98

def king_of_the_jungle(interactive_id, params={})
  colors = Color.all
  play interactive_id, :url => 'http://soundjax.com/reddo/35463%5Eat_tarzan.mp3'
  rand(50).times do |i| # do some pseudo random blinking and crazy ears
    color = colors[(colors.size % (i + 1)) + 1]
    sleep = rand
    led interactive_id, :color => color, :period => (sleep * 1000).to_i
    ears interactive_id, :left => i / 2, :right => i * -1
    sleep(sleep)
  end
end

.led(interactive_id, params = {}) ⇒ Object Also known as: pulse

LED================


41
42
43
# File 'lib/karotz/client.rb', line 41

def led(interactive_id, params={})
  request :led, interactive_id, {:action => :pulse, :color => Color::BLUE, :period => 3000, :pulse => 500}.merge(params)
end

.light(interactive_id, params = {}) ⇒ Object



50
51
52
# File 'lib/karotz/client.rb', line 50

def light(interactive_id, params={})
  request :led, interactive_id, {:action => :light, :color => Color::BLUE}.merge(params)
end

.multimedia(interactive_id, params = {}) ⇒ Object Also known as: play

MULTIMEDIA=========


72
73
74
# File 'lib/karotz/client.rb', line 72

def multimedia(interactive_id, params={})
  request :multimedia, interactive_id, {:action => Multimedia::PLAY, :url => "http://www.jimwalls.net/mp3/ATeam.mp3"}.merge(params)
end

.nyan(interactive_id) ⇒ Object



77
78
79
# File 'lib/karotz/client.rb', line 77

def nyan(interactive_id)
  multimedia interactive_id, :url => "http://api.ning.com:80/files/3zmSvhA*3jKxFJj1I5uh5dp5oCynyyMksQjwS3JWWQNlriTzDzX61KtlFnuQtx-hEmV7NdqVgofmZvh7cXOX-UVJ47m1SR4a/nyanlooped.mp3"
end

.perform_request(action, interactive_id, params = {}) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/karotz/client.rb', line 172

def perform_request(action, interactive_id, params={})
  raise "interactive_id is needed!" unless interactive_id
  raise "action is needed!" unless action
  url = "#{Configuration.endpoint}#{action}?#{create_query({ :interactiveid => interactive_id }.merge(params))}"
  Configuration.logger.debug "calling karotz api with url '#{url}'"
  response = get(url)
  answer = Crack::XML.parse(response.body)
  Configuration.logger.debug "answer was '#{answer}'"
  answer
end

.request(endpoint, interactive_id, params = {}) ⇒ Object



161
162
163
164
# File 'lib/karotz/client.rb', line 161

def request(endpoint, interactive_id, params={})
  answer = perform_request(endpoint, interactive_id, params)
  raise "bad response from server" if answer["VoosMsg"].nil? || answer["VoosMsg"]["response"].nil? || answer["VoosMsg"]["response"]["code"] != "OK"
end

.sad(interactive_id) ⇒ Object



35
36
37
# File 'lib/karotz/client.rb', line 35

def sad(interactive_id)
  ears interactive_id, :left => 9, :right => 9
end

.sessionObject

TODO multimedia-api is not blocking, so we need some whay to find out when we can kill the session properly



130
131
132
133
134
135
# File 'lib/karotz/client.rb', line 130

def session # TODO multimedia-api is not blocking, so we need some whay to find out when we can kill the session properly
  client = create
  yield(client)
ensure
  stop(client.interactive_id) if client
end

.startObject

LIFE_CYCLE=========


113
114
115
116
117
118
119
120
121
122
# File 'lib/karotz/client.rb', line 113

def start
  Configuration.validate_credentials!
  url = start_url(Configuration.install_id, Configuration.api_key, Configuration.secret)
  Configuration.logger.debug "calling karotz api with url '#{url}'"
  response = get(url)
  answer = Crack::XML.parse(response.body)
  Configuration.logger.debug "answer was '#{answer}'"
  raise "could not retrieve interactive_id" if answer["VoosMsg"].nil? || answer["VoosMsg"]["interactiveMode"].nil? || answer["VoosMsg"]["interactiveMode"]["interactiveId"].nil?
  answer["VoosMsg"]["interactiveMode"]["interactiveId"]
end

.start_url(install_id, api_key, secret, once = rand(99999999999999), timestamp = Time.now.to_i) ⇒ Object

HELPERS================


145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/karotz/client.rb', line 145

def start_url(install_id, api_key, secret, once=rand(99999999999999), timestamp=Time.now.to_i)
  params = {
    'installid'   => install_id,
    'apikey'      => api_key,
    'once'        => once,
    'timestamp'   => timestamp,
  }
  query   = create_query(params)
  hmac    = OpenSSL::HMAC.digest(Configuration.digest, secret, query)
  encoded = Base64.encode64(hmac).chomp
  signed  = CGI.escape(encoded)
  "#{Configuration.endpoint}start?#{query}&signature=#{signed}"
end

.stop(interactive_id, params = {}) ⇒ Object Also known as: destroy, disconnect



124
125
126
# File 'lib/karotz/client.rb', line 124

def stop(interactive_id, params={})
  request(:interactivemode, interactive_id, {:action => :stop}.merge(params))
end

.tts(interactive_id, params = {}) ⇒ Object Also known as: speak, say, talk

TTS================


56
57
58
# File 'lib/karotz/client.rb', line 56

def tts(interactive_id, params={})
  request :tts, interactive_id, {:action => :speak, :text => "test", :lang => Language::ENGLISH}.merge(params)
end

.webcam(interactive_id, params = {}) ⇒ Object Also known as: snap, cam

WEBCAM=========


83
84
85
# File 'lib/karotz/client.rb', line 83

def webcam(interactive_id, params={})
  request :webcam, interactive_id, {:action => :photo, :url => "https://picasaweb.google.com/data/feed/api/phoet6/default/albumid/default"}.merge(params)
end

Instance Method Details

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/karotz/client.rb', line 23

def respond_to?(meth)
  self.class.respond_to?(meth) || super
end