Class: Karotz::Client

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

Constant Summary collapse

API =
"http://api.karotz.com/api/karotz/"
DIGEST =
OpenSSL::Digest::Digest.new('sha1')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interactive_id) ⇒ Client

Returns a new instance of Client.



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

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



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

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.



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

def interactive_id
  @interactive_id
end

Class Method Details

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

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


67
68
69
# File 'lib/karotz/client.rb', line 67

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

.config(interactive_id) ⇒ Object

CONFIG=========


93
94
95
96
# File 'lib/karotz/client.rb', line 93

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

.createObject Also known as: connect



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

def create
  interactive_id = start
  new(interactive_id)
end

.create_query(params) ⇒ Object



164
165
166
# File 'lib/karotz/client.rb', line 164

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=================


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

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

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



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

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

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

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


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

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



54
55
56
# File 'lib/karotz/client.rb', line 54

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=========


74
75
76
# File 'lib/karotz/client.rb', line 74

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

.nyan(interactive_id) ⇒ Object



79
80
81
# File 'lib/karotz/client.rb', line 79

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

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/karotz/client.rb', line 153

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

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



148
149
150
151
# File 'lib/karotz/client.rb', line 148

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



39
40
41
# File 'lib/karotz/client.rb', line 39

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



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

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=========


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

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 = HTTPI.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================


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/karotz/client.rb', line 132

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(DIGEST, secret, query)
  encoded = Base64.encode64(hmac).chomp
  signed  = CGI.escape(encoded)
  "#{API}start?#{query}&signature=#{signed}"
end

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



111
112
113
# File 'lib/karotz/client.rb', line 111

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

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

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


60
61
62
# File 'lib/karotz/client.rb', line 60

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=========


85
86
87
# File 'lib/karotz/client.rb', line 85

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)


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

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