cs 0.1.0beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/.gitignore +21 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE +22 -0
  4. data/README.md +132 -0
  5. data/Rakefile +7 -0
  6. data/cs.gemspec +22 -0
  7. data/lib/commonsense-ruby-lib.rb +127 -0
  8. data/lib/commonsense-ruby-lib/auth/http.rb +117 -0
  9. data/lib/commonsense-ruby-lib/auth/oauth.rb +101 -0
  10. data/lib/commonsense-ruby-lib/end_point.rb +276 -0
  11. data/lib/commonsense-ruby-lib/end_point/group.rb +28 -0
  12. data/lib/commonsense-ruby-lib/end_point/sensor.rb +36 -0
  13. data/lib/commonsense-ruby-lib/end_point/sensor_data.rb +70 -0
  14. data/lib/commonsense-ruby-lib/end_point/user.rb +50 -0
  15. data/lib/commonsense-ruby-lib/error.rb +51 -0
  16. data/lib/commonsense-ruby-lib/relation.rb +233 -0
  17. data/lib/commonsense-ruby-lib/relation/sensor_data_relation.rb +116 -0
  18. data/lib/commonsense-ruby-lib/relation/sensor_relation.rb +162 -0
  19. data/lib/commonsense-ruby-lib/serializer.rb +20 -0
  20. data/lib/commonsense-ruby-lib/session.rb +105 -0
  21. data/lib/commonsense-ruby-lib/version.rb +3 -0
  22. data/spec/features/sensor_data_management_spec.rb +4 -0
  23. data/spec/features/sensor_management_spec.rb +105 -0
  24. data/spec/features/user_management_spec.rb +70 -0
  25. data/spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb +68 -0
  26. data/spec/lib/commonsense-ruby-lib/end_point/sensor_spec.rb +98 -0
  27. data/spec/lib/commonsense-ruby-lib/end_point/user_spec.rb +36 -0
  28. data/spec/lib/commonsense-ruby-lib/end_point_spec.rb +190 -0
  29. data/spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb +444 -0
  30. data/spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb +165 -0
  31. data/spec/lib/commonsense-ruby-lib/session_spec.rb +43 -0
  32. data/spec/lib/commonsense-ruby-lib_spec.rb +51 -0
  33. data/spec/spec_helper.rb +40 -0
  34. data/spec/support/spec_config.yml.sample +6 -0
  35. data/spec/support/vcr.rb +5 -0
  36. metadata +175 -0
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+ require 'commonsense-ruby-lib/end_point/sensor'
3
+
4
+ module CommonSense
5
+ module Relation
6
+ describe SensorRelation do
7
+ describe "build" do
8
+ it "should return a sensor object" do
9
+ SensorRelation.new.build.should be_a_kind_of(EndPoint::Sensor)
10
+ end
11
+ end
12
+
13
+ let(:sensors) {
14
+ {"sensors" => [{"name" => "sensor1"}, {"name" => "sensor2"}, {"name" => "sensor3"}], "total" => 3}
15
+ }
16
+
17
+ let(:relation) {
18
+ relation = SensorRelation.new
19
+ relation.stub("check_session!").and_return(true)
20
+ relation.stub("get_data!").and_return(sensors)
21
+ relation
22
+ }
23
+
24
+ describe "get_data!" do
25
+ it "should fetch sensor data from commonSense" do
26
+ session = double('Session')
27
+ option = {page: 100, per_page: 99, shared: 1, owned:1, physical: 1, details: "full"}
28
+ session.should_receive(:get).with("/sensors.json", option)
29
+
30
+ relation = SensorRelation.new(session)
31
+ relation.get_data!(page: 100, per_page: 99, shared:true, owned:true, physical:true, details:"full")
32
+ end
33
+ end
34
+
35
+ describe "get_data" do
36
+ it "should not throw an exception" do
37
+ relation = SensorRelation.new
38
+ relation.stub(:get_data!).and_return { raise Error }
39
+
40
+ expect { relation.get_data}.to_not raise_error
41
+ end
42
+ end
43
+
44
+ describe "each" do
45
+ it "should get all sensor and yield each" do
46
+ relation = SensorRelation.new
47
+ relation.stub("get_data").and_return(sensors)
48
+
49
+ expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::Sensor, EndPoint::Sensor, EndPoint::Sensor)
50
+ end
51
+ end
52
+
53
+ describe "count" do
54
+ it "should return the total number of record" do
55
+ relation.count.should eq(3)
56
+ end
57
+ end
58
+
59
+ describe "first" do
60
+ it "should return the first record" do
61
+ first = relation.first
62
+ first.should be_kind_of(EndPoint::Sensor)
63
+ first.name.should eq("sensor1")
64
+ end
65
+ end
66
+
67
+ describe "last" do
68
+ it "should return the last record" do
69
+ relation = SensorRelation.new
70
+ relation.stub("count").and_return(3)
71
+ relation.should_receive("get_data").with(page:2, per_page:1, shared:nil, owned:nil, physical:nil, details:nil).and_return({"sensors" => [{"name" => "sensor3"}], "total" => 3})
72
+
73
+ first = relation.last
74
+ first.should be_kind_of(EndPoint::Sensor)
75
+ first.name.should eq("sensor3")
76
+ end
77
+ end
78
+
79
+ describe "where" do
80
+ it "should update the query parameter" do
81
+ relation = SensorRelation.new
82
+ relation.where(page:0, per_page:10, shared: true, owned:true, physical: true, details: "full")
83
+ relation.page.should eq(0)
84
+ relation.per_page.should eq(10)
85
+ relation.shared.should eq(1)
86
+ relation.owned.should eq(1)
87
+ relation.physical.should eq(1)
88
+ relation.details.should eq("full")
89
+
90
+ relation.where(page:0, per_page:10, shared: false, owned:false, physical: false, details: "no")
91
+ relation.page.should eq(0)
92
+ relation.per_page.should eq(10)
93
+ relation.shared.should eq(0)
94
+ relation.owned.should eq(0)
95
+ relation.physical.should eq(0)
96
+ relation.details.should eq("no")
97
+
98
+ relation.where(page:nil, per_page:nil, shared: nil, owned:nil, physical: nil, details: nil)
99
+ relation.page.should eq(0)
100
+ relation.per_page.should eq(1000)
101
+ relation.shared.should be_nil
102
+ relation.owned.should be_nil
103
+ relation.physical.should be_nil
104
+ relation.details.should be_nil
105
+
106
+ relation.page = 100
107
+ expect { relation.where(page: 'a') }.to raise_error ArgumentError
108
+
109
+ relation.per_page = 999
110
+ expect { relation.where(per_page: 'a') }.to raise_error ArgumentError
111
+
112
+ relation.shared = false
113
+ relation.where(shared: "true")
114
+ relation.shared.should be_true
115
+
116
+ relation.shared = false
117
+ relation.where(shared: 1)
118
+ relation.shared.should be_true
119
+
120
+ relation.shared = true
121
+ relation.where(shared: 0)
122
+ relation.shared.should eq(0)
123
+
124
+ relation.shared = true
125
+ relation.where(shared: false)
126
+ relation.shared.should eq(0)
127
+
128
+ relation.shared = true
129
+ relation.where(shared: "false")
130
+ relation.shared.should eq(0)
131
+
132
+ relation.details = nil
133
+ relation.where(details: "no")
134
+ relation.details.should eq("no")
135
+
136
+ relation.details = nil
137
+ relation.where(details: "full")
138
+ relation.details.should eq("full")
139
+
140
+ relation.details = nil
141
+ expect { relation.where(details: 1) }.to raise_error ArgumentError
142
+
143
+
144
+ relation.details = nil
145
+ expect { relation.where(details: false) }.to raise_error ArgumentError
146
+ end
147
+ end
148
+
149
+ describe "find_by_name" do
150
+ it "should return an array of matching sensor name" do
151
+ relation = SensorRelation.new
152
+ relation.session = double('Session')
153
+ relation.stub("count").and_return(3)
154
+ relation.should_receive("get_data").with(page:0, per_page:nil, shared:nil, owned:nil, physical:nil, details:nil).and_return({"sensors" => [{"name" => "sensor11"}, {"name" => "sensor12"}, {"name" => "sensor2"}], "total" => 3})
155
+
156
+ sensors = relation.find_by_name(/sensor1/)
157
+ sensors.should be_kind_of(Array)
158
+ sensors.size.should eq(2)
159
+ sensors[0].name.should eq("sensor11")
160
+ sensors[1].name.should eq("sensor12")
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe "session" do
4
+ before(:each) do
5
+ @client = client = CommonSense::Client.new(base_uri: ENV['spec_base_uri'])
6
+ end
7
+
8
+ describe "login" do
9
+ describe "with corrent username and password" do
10
+ it "should create new session" do
11
+ session_id = @client.login($user.username, 'password')
12
+ session_id.should_not be_nil
13
+ @client.session.should_not be_nil
14
+ @client.session.response_code.should eq(200)
15
+ @client.session.session_id.should_not be_nil
16
+ end
17
+ end
18
+
19
+ describe "with incorrect username or password" do
20
+ it "should create new session", :vcr do
21
+ session_id = @client.login($user.username, "x#{$user.password}")
22
+ session_id.should be_nil
23
+ @client.session.should_not be_nil
24
+ @client.session.response_code.should eq(403)
25
+ @client.session.session_id.should be_nil
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "oauth" do
31
+ describe "with correct access token" do
32
+ it "should create new session with oauth" do
33
+ pending
34
+ session = @client.oauth(CONFIG['CS_CONSUMER_KEY'], CONFIG['CS_CONSUMER_SECRET'],
35
+ CONFIG['CS_ACCESS_TOKEN'], CONFIG['CS_ACCESS_TOKEN_SECRET'])
36
+ session.should be_true
37
+ @client.current_user.should_not be_nil
38
+ @client.session.response_code.should eq(200)
39
+ @client.session.response_body.should_not be_nil
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe "commonsense-ruby-lib" do
4
+ describe "client with authentication" do
5
+ before(:each) do
6
+ @client = client = create_client
7
+ @session_id = @client.login($user.username, 'password')
8
+ @session_id.should_not be_nil
9
+ end
10
+
11
+ describe "current_user" do
12
+ it "should return current user information" do
13
+ current_user = @client.current_user
14
+ current_user.username.should eq($user.username)
15
+ current_user.to_h.should be_kind_of Hash
16
+ end
17
+ end
18
+
19
+ describe "groups" do
20
+ it "should return groups that current user belongs to" do
21
+ groups = @client.current_groups
22
+ groups.should be_empty
23
+ end
24
+ end
25
+
26
+ describe "new_user" do
27
+ it "should create a new user" do
28
+
29
+ end
30
+ end
31
+
32
+ describe "sensors" do
33
+ it "should return Sensors relation" do
34
+ @client.sensors.should be_a_kind_of(CommonSense::Relation::SensorRelation)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "with session_id" do
40
+ end
41
+
42
+ describe "with OAuth" do
43
+ before(:each) do
44
+ @client = client = CommonSense::Client.new
45
+ @client.oauth(CONFIG['CS_CONSUMER_KEY'], CONFIG['CS_CONSUMER_SECRET'],
46
+ CONFIG['CS_ACCESS_TOKEN'], CONFIG['CS_ACCESS_TOKEN_SECRET'])
47
+ end
48
+
49
+ #it_behaves_like "Client"
50
+ end
51
+ end
@@ -0,0 +1,40 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ require 'commonsense-ruby-lib'
4
+ #require 'vcr'
5
+ require 'pry'
6
+
7
+ Dir[File.join(File.dirname(__FILE__),("support/**/*.rb"))].each {|f| require f}
8
+
9
+ CONFIG = YAML.load(File.read(File.expand_path("support/spec_config.yml", File.dirname(__FILE__))))
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+
14
+ # create a single user
15
+ config.before(:all) do
16
+ unless $user
17
+ $username = "user#{Time.now.to_f}@tester.com"
18
+ $password = "password"
19
+
20
+ client = CommonSense::Client.new(base_uri: ENV['spec_base_uri'])
21
+ $user = client.new_user
22
+ $user.username = $username
23
+ $user.email = $user.username
24
+ $user.password = 'password'
25
+ $user.name = 'Jan'
26
+ $user.surname = 'jagger'
27
+ $user.address = 'Lloydstraat 5'
28
+ $user.zipcode = '3024ea'
29
+ $user.country = 'NETHERLANDS'
30
+ $user.mobile = '123456789'
31
+ $user.save
32
+ end
33
+ end
34
+ end
35
+
36
+ def create_client
37
+ CommonSense::Client.new(base_uri: ENV['spec_base_uri'])
38
+ end
39
+
40
+ ENV['spec_base_uri'] ||= 'http://api.dev.sense-os.local'
@@ -0,0 +1,6 @@
1
+ CS_USER: joe
2
+ CS_PASSWORD: foobar
3
+ CS_CONSUMER_KEY:
4
+ CS_CONSUMER_SECRET:
5
+ CS_ACCESS_TOKEN:
6
+ CS_ACCESS_TOKEN_SECRET:
@@ -0,0 +1,5 @@
1
+ #VCR.configure do |c|
2
+ # c.cassette_library_dir = File.join(File.dirname(__FILE__), "..", "vcr")
3
+ # c.hook_into :fakeweb
4
+ # c.configure_rspec_metadata!
5
+ #end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0beta3
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Ahmy Yulrizka
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.13.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.13.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: launchy
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.3.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: fakeweb
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: httparty
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.11.0
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.11.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: oauth
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 0.4.7
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.4.7
94
+ description: CommonSense API client library
95
+ email:
96
+ - ahmy@sense-os.nl
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE
104
+ - README.md
105
+ - Rakefile
106
+ - cs.gemspec
107
+ - lib/commonsense-ruby-lib.rb
108
+ - lib/commonsense-ruby-lib/auth/http.rb
109
+ - lib/commonsense-ruby-lib/auth/oauth.rb
110
+ - lib/commonsense-ruby-lib/end_point.rb
111
+ - lib/commonsense-ruby-lib/end_point/group.rb
112
+ - lib/commonsense-ruby-lib/end_point/sensor.rb
113
+ - lib/commonsense-ruby-lib/end_point/sensor_data.rb
114
+ - lib/commonsense-ruby-lib/end_point/user.rb
115
+ - lib/commonsense-ruby-lib/error.rb
116
+ - lib/commonsense-ruby-lib/relation.rb
117
+ - lib/commonsense-ruby-lib/relation/sensor_data_relation.rb
118
+ - lib/commonsense-ruby-lib/relation/sensor_relation.rb
119
+ - lib/commonsense-ruby-lib/serializer.rb
120
+ - lib/commonsense-ruby-lib/session.rb
121
+ - lib/commonsense-ruby-lib/version.rb
122
+ - spec/features/sensor_data_management_spec.rb
123
+ - spec/features/sensor_management_spec.rb
124
+ - spec/features/user_management_spec.rb
125
+ - spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb
126
+ - spec/lib/commonsense-ruby-lib/end_point/sensor_spec.rb
127
+ - spec/lib/commonsense-ruby-lib/end_point/user_spec.rb
128
+ - spec/lib/commonsense-ruby-lib/end_point_spec.rb
129
+ - spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb
130
+ - spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb
131
+ - spec/lib/commonsense-ruby-lib/session_spec.rb
132
+ - spec/lib/commonsense-ruby-lib_spec.rb
133
+ - spec/spec_helper.rb
134
+ - spec/support/spec_config.yml.sample
135
+ - spec/support/vcr.rb
136
+ homepage: https://github.com/senseobservationsystems/commonsense-ruby-lib
137
+ licenses: []
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>'
152
+ - !ruby/object:Gem::Version
153
+ version: 1.3.1
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.23
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: Client library to communicate with CommonSense written in ruby
160
+ test_files:
161
+ - spec/features/sensor_data_management_spec.rb
162
+ - spec/features/sensor_management_spec.rb
163
+ - spec/features/user_management_spec.rb
164
+ - spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb
165
+ - spec/lib/commonsense-ruby-lib/end_point/sensor_spec.rb
166
+ - spec/lib/commonsense-ruby-lib/end_point/user_spec.rb
167
+ - spec/lib/commonsense-ruby-lib/end_point_spec.rb
168
+ - spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb
169
+ - spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb
170
+ - spec/lib/commonsense-ruby-lib/session_spec.rb
171
+ - spec/lib/commonsense-ruby-lib_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/support/spec_config.yml.sample
174
+ - spec/support/vcr.rb
175
+ has_rdoc: