karotz 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.irbrc ADDED
@@ -0,0 +1,10 @@
1
+ $:.unshift 'lib'
2
+ require 'karotz'
3
+
4
+ Karotz::Configuration.configure do |config|
5
+ config.install_id = ENV['KAROTZ_INSTALL_ID']
6
+ config.api_key = ENV['KAROTZ_API_KEY']
7
+ config.secret = ENV['KAROTZ_SECRET']
8
+ end
9
+
10
+ @client = Karotz::Client.new(Karotz::Client.start)
data/README.rdoc CHANGED
@@ -7,13 +7,38 @@ This gem is still in early development.
7
7
 
8
8
  == Docs
9
9
 
10
- Right now, please have a look at the specs for usage examples.
11
-
12
10
  Supported methods:
13
11
 
14
12
  * ears
15
13
  * led
16
-
17
- == Todos
18
-
19
- * add color mixing method for RGB usage
14
+ * tts (text to speach)
15
+ * multimedia
16
+
17
+ Have a look at the whole api: http://dev.karotz.com/api/
18
+
19
+ == Examples
20
+
21
+ require 'karotz'
22
+ Karotz::Configuration.configure do |config|
23
+ config.install_id = ENV['KAROTZ_INSTALL_ID']
24
+ config.api_key = ENV['KAROTZ_API_KEY']
25
+ config.secret = ENV['KAROTZ_SECRET']
26
+ end
27
+
28
+ # bare
29
+ interactive_id = Karotz::Client.start
30
+ Karotz::Client.ears interactive_id
31
+ [...]
32
+ Karotz::Client.stop(interactive_id)
33
+
34
+ # using a session
35
+ Karotz::Client.session do |karotz|
36
+ karotz.ears
37
+ [...]
38
+ end
39
+
40
+ # using a client
41
+ karotz = Karotz::Client.create
42
+ karotz.ears
43
+ [...]
44
+ karotz.stop
data/lib/karotz/client.rb CHANGED
@@ -6,6 +6,7 @@ require 'crack'
6
6
 
7
7
  module Karotz
8
8
  class Client
9
+ attr_accessor :interactive_id
9
10
 
10
11
  def initialize(interactive_id)
11
12
  @interactive_id = interactive_id
@@ -27,7 +28,7 @@ module Karotz
27
28
  API = "http://api.karotz.com/api/karotz/"
28
29
  DIGEST = OpenSSL::Digest::Digest.new('sha1')
29
30
 
30
- class << self
31
+ class << self # TODO add the rest of the API calls
31
32
 
32
33
  #==========EARS=================
33
34
 
@@ -37,55 +38,64 @@ module Karotz
37
38
 
38
39
  #============LED================
39
40
 
40
- def led(interactive_id, params={:action => :pulse, :color => Color::BLUE, :period => 3000, :pulse => 500})
41
- request :led, interactive_id, params
41
+ def led(interactive_id, params={})
42
+ request :led, interactive_id, {:action => :pulse, :color => Color::BLUE, :period => 3000, :pulse => 500}.merge(params)
42
43
  end
44
+ alias :pulse :led
43
45
 
44
- def fade(interactive_id, params={:color => Color::BLUE, :period => 3000})
45
- request :led, interactive_id, {:action => :fade}.merge(params)
46
+ def fade(interactive_id, params={})
47
+ request :led, interactive_id, {:action => :fade, :color => Color::BLUE, :period => 3000}.merge(params)
46
48
  end
47
49
 
48
- def light(interactive_id, params={:color => Color::BLUE})
49
- request :led, interactive_id, {:action => :light}.merge(params)
50
+ def light(interactive_id, params={})
51
+ request :led, interactive_id, {:action => :light, :color => Color::BLUE}.merge(params)
50
52
  end
51
53
 
52
54
  #============TTS================
53
55
 
54
- def tts(interactive_id, params={:action => :speak, :text => "test", :lang => Language::ENGLISH})
55
- request :tts, interactive_id, params
56
+ def tts(interactive_id, params={})
57
+ request :tts, interactive_id, {:action => :speak, :text => "test", :lang => Language::ENGLISH}.merge(params)
56
58
  end
57
59
  alias :speak :tts
58
60
 
59
61
  #============MULTIMEDIA=========
60
62
 
61
- def multimedia(interactive_id, params={:action => :play, :url => "http://www.jimwalls.net/mp3/ATeam.mp3"})
62
- request :multimedia, interactive_id, params
63
+ def multimedia(interactive_id, params={})
64
+ request :multimedia, interactive_id, {:action => :play, :url => "http://www.jimwalls.net/mp3/ATeam.mp3"}.merge(params)
63
65
  end
64
66
  alias :play :multimedia
65
67
 
66
68
  #============LIFE_CYCLE=========
67
69
 
68
70
  def start
71
+ Configuration.validate_credentials!
69
72
  url = start_url(Configuration.install_id, Configuration.api_key, Configuration.secret)
73
+ Configuration.logger.debug "calling karotz api with url '#{url}'"
70
74
  response = HTTPI.get(url)
71
75
  answer = Crack::XML.parse(response.body)
76
+ Configuration.logger.debug "answer was '#{answer}'"
72
77
  raise "could not retrieve interactive_id" if answer["VoosMsg"].nil? || answer["VoosMsg"]["interactiveMode"].nil? || answer["VoosMsg"]["interactiveMode"]["interactiveId"].nil?
73
78
  answer["VoosMsg"]["interactiveMode"]["interactiveId"]
74
79
  end
75
80
 
76
- def stop(interactive_id, params={:action => :stop})
77
- request :interactivemode, interactive_id, params
81
+ def stop(interactive_id, params={})
82
+ request :interactivemode, interactive_id, {:action => :stop}.merge(params)
78
83
  end
79
84
 
80
- def session
81
- interactive_id = start
82
- yield(new(interactive_id))
85
+ def session # TODO multimedia-api is not blocking, so we need some whay to find out when we can kill the session properly
86
+ client = create
87
+ yield(client)
83
88
  ensure
84
- stop(interactive_id)
89
+ stop(client.interactive_id) if client
85
90
  end
86
91
 
87
92
  #===========HELPERS================
88
93
 
94
+ def create
95
+ interactive_id = start
96
+ new(interactive_id)
97
+ end
98
+
89
99
  def start_url(install_id, api_key, secret, once=rand(99999999999999), timestamp=Time.now.to_i)
90
100
  params = {
91
101
  'installid' => install_id,
@@ -106,9 +116,11 @@ module Karotz
106
116
  raise "interactive_id is needed!" unless interactive_id
107
117
  raise "endpoint is needed!" unless endpoint
108
118
  url = "#{API}#{endpoint}?#{create_query({ :interactiveid => interactive_id }.merge(params))}"
119
+ Configuration.logger.debug "calling karotz api with url '#{url}'"
109
120
  response = HTTPI.get(url)
110
121
  answer = Crack::XML.parse(response.body)
111
- raise "bad response from server" unless answer["VoosMsg"]["response"]["code"] == "OK"
122
+ Configuration.logger.debug "answer was '#{answer}'"
123
+ raise "bad response from server" if answer["VoosMsg"].nil? || answer["VoosMsg"]["response"].nil? || answer["VoosMsg"]["response"]["code"] != "OK"
112
124
  end
113
125
 
114
126
  def create_query(params)
data/lib/karotz/color.rb CHANGED
@@ -10,5 +10,7 @@ module Karotz
10
10
  RED = "FF0000"
11
11
  YELLOW = "75FF00"
12
12
  WHITE = "4FFF68"
13
+
14
+ # TODO add color mixing method for RGB usage
13
15
  end
14
16
  end
@@ -1,3 +1,5 @@
1
+ require 'logger'
2
+
1
3
  module Karotz
2
4
  class Configuration
3
5
  class << self
@@ -26,7 +28,7 @@ module Karotz
26
28
  end
27
29
 
28
30
  def blank?(key)
29
- val = self.send :key
31
+ val = self.send key
30
32
  val.nil? || val.empty?
31
33
  end
32
34
 
@@ -1,3 +1,3 @@
1
1
  module Karotz
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,18 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: http://api.karotz.com:80/api/karotz/start?apikey=7afdd4b7-3bc8-4469-bda2-1d8bc1e218a0&installid=68e0a42b-aca3-49aa-9dc2-85a8f141e8c5&once=31070003985075&signature=Vo4ukPmZg7lB/3PkBWLXulkT/ss=&timestamp=1324980926
6
+ body:
7
+ headers:
8
+ response: !ruby/struct:VCR::Response
9
+ status: !ruby/struct:VCR::ResponseStatus
10
+ code: 200
11
+ message: OK
12
+ headers:
13
+ content-length:
14
+ - '388'
15
+ connection:
16
+ - keep-alive
17
+ body: <VoosMsg><id>6eaff778-ca04-435d-8d14-517757ebe99b</id><recipient>2addc8ff97e59fe4616021094db393d5</recipient><interactiveMode><action>START</action><interactiveId>e014174e-069b-4518-b8ea-d82184ae7c77</interactiveId><configId>941894ed-d048-42f6-ab68-fbe95abaa157</configId><access>ears</access><access>led</access><access>multimedia</access><access>tts</access></interactiveMode></VoosMsg>
18
+ http_version: '1.1'
data/spec/karotz_spec.rb CHANGED
@@ -30,7 +30,6 @@ module Karotz
30
30
  end
31
31
 
32
32
  context "ears" do
33
-
34
33
  it "should wiggle the ears", :vcr => true do
35
34
  Client.ears(@interactive_id)
36
35
  end
@@ -63,7 +62,7 @@ module Karotz
63
62
 
64
63
  context "led" do
65
64
  it "should pulse", :vcr => true do
66
- Client.led(@interactive_id)
65
+ Client.pulse(@interactive_id)
67
66
  end
68
67
 
69
68
  it "should fade", :vcr => true do
@@ -96,6 +95,12 @@ module Karotz
96
95
  end
97
96
  end
98
97
 
98
+ it "should create a client with a interactive_id", :vcr => true do
99
+ Client.create.tap do |it|
100
+ it.interactive_id.should_not be_nil
101
+ end
102
+ end
103
+
99
104
  it "should start and stop the interactiveMode", :vcr => true do
100
105
  interactive_id = Client.start
101
106
  interactive_id.should_not be_empty
@@ -108,6 +113,5 @@ module Karotz
108
113
  end
109
114
  end
110
115
  end
111
-
112
116
  end
113
117
  end
data/spec/spec_helper.rb CHANGED
@@ -22,15 +22,15 @@ RSpec.configure do |config|
22
22
  end
23
23
 
24
24
  config.before :each do
25
- HTTPI.log = false
26
-
27
25
  @install_id = ENV['KAROTZ_INSTALL_ID']
28
26
  @api_key = ENV['KAROTZ_API_KEY']
29
27
  @secret = ENV['KAROTZ_SECRET']
30
28
  # retrieved via http://www.karotz.com/authentication/run/karotz/API_KEY
31
29
  @interactive_id = "209301e8-05a6-47b4-b257-600eb62f7d25"
32
30
 
31
+ HTTPI.log = false
33
32
  Karotz::Configuration.reset
33
+ Karotz::Configuration.logger.level = Logger::WARN
34
34
  end
35
35
  end
36
36
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karotz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-26 00:00:00.000000000 Z
12
+ date: 2011-12-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2156300460 !ruby/object:Gem::Requirement
16
+ requirement: &2160175000 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2156300460
24
+ version_requirements: *2160175000
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2156300040 !ruby/object:Gem::Requirement
27
+ requirement: &2160174580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2156300040
35
+ version_requirements: *2160174580
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: pry
38
- requirement: &2156299620 !ruby/object:Gem::Requirement
38
+ requirement: &2160174160 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2156299620
46
+ version_requirements: *2160174160
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: guard-rspec
49
- requirement: &2156299200 !ruby/object:Gem::Requirement
49
+ requirement: &2160173740 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2156299200
57
+ version_requirements: *2160173740
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: webmock
60
- requirement: &2156298780 !ruby/object:Gem::Requirement
60
+ requirement: &2160173320 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2156298780
68
+ version_requirements: *2160173320
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: vcr
71
- requirement: &2156298360 !ruby/object:Gem::Requirement
71
+ requirement: &2160189280 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2156298360
79
+ version_requirements: *2160189280
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: httpclient
82
- requirement: &2156314320 !ruby/object:Gem::Requirement
82
+ requirement: &2160188860 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *2156314320
90
+ version_requirements: *2160188860
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: httpi
93
- requirement: &2156313900 !ruby/object:Gem::Requirement
93
+ requirement: &2160188440 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *2156313900
101
+ version_requirements: *2160188440
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: crack
104
- requirement: &2156313480 !ruby/object:Gem::Requirement
104
+ requirement: &2160188020 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *2156313480
112
+ version_requirements: *2160188020
113
113
  description: ruby bindings for karotz rest api
114
114
  email:
115
115
  - phoetmail@googlemail.com
@@ -119,6 +119,7 @@ extra_rdoc_files: []
119
119
  files:
120
120
  - .document
121
121
  - .gitignore
122
+ - .irbrc
122
123
  - .rvmrc
123
124
  - .specfile
124
125
  - .travis.yml
@@ -142,6 +143,7 @@ files:
142
143
  - spec/cassettes/karotz/client_led_should_fade.yml
143
144
  - spec/cassettes/karotz/client_led_should_light.yml
144
145
  - spec/cassettes/karotz/client_led_should_pulse.yml
146
+ - spec/cassettes/karotz/client_lifecycle_should_create_a_client_with_a_interactive_id.yml
145
147
  - spec/cassettes/karotz/client_lifecycle_should_do_something_in_a_block.yml
146
148
  - spec/cassettes/karotz/client_lifecycle_should_start_and_stop_the_interactivemode.yml
147
149
  - spec/cassettes/karotz/client_multimedia_should_play_mp3.yml
@@ -182,6 +184,7 @@ test_files:
182
184
  - spec/cassettes/karotz/client_led_should_fade.yml
183
185
  - spec/cassettes/karotz/client_led_should_light.yml
184
186
  - spec/cassettes/karotz/client_led_should_pulse.yml
187
+ - spec/cassettes/karotz/client_lifecycle_should_create_a_client_with_a_interactive_id.yml
185
188
  - spec/cassettes/karotz/client_lifecycle_should_do_something_in_a_block.yml
186
189
  - spec/cassettes/karotz/client_lifecycle_should_start_and_stop_the_interactivemode.yml
187
190
  - spec/cassettes/karotz/client_multimedia_should_play_mp3.yml