cs 0.1.0beta3 → 0.1.0
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.
- checksums.yaml +7 -0
 - data/.travis.yml +8 -0
 - data/Gemfile +22 -6
 - data/README.md +15 -4
 - data/cs.gemspec +7 -7
 - data/lib/{commonsense-ruby-lib.rb → cs.rb} +80 -31
 - data/lib/{commonsense-ruby-lib → cs}/auth/http.rb +52 -33
 - data/lib/{commonsense-ruby-lib → cs}/auth/oauth.rb +28 -28
 - data/lib/cs/collection.rb +7 -0
 - data/lib/cs/collection/sensor_data_collection.rb +61 -0
 - data/lib/{commonsense-ruby-lib → cs}/end_point.rb +36 -24
 - data/lib/cs/end_point/group.rb +26 -0
 - data/lib/cs/end_point/notification.rb +16 -0
 - data/lib/{commonsense-ruby-lib → cs}/end_point/sensor.rb +22 -6
 - data/lib/{commonsense-ruby-lib → cs}/end_point/sensor_data.rb +17 -6
 - data/lib/cs/end_point/trigger.rb +16 -0
 - data/lib/{commonsense-ruby-lib → cs}/end_point/user.rb +8 -4
 - data/lib/{commonsense-ruby-lib → cs}/error.rb +7 -1
 - data/lib/cs/parameter_processor.rb +99 -0
 - data/lib/cs/relation.rb +244 -0
 - data/lib/cs/relation/group_relation.rb +24 -0
 - data/lib/cs/relation/notification_relation.rb +20 -0
 - data/lib/{commonsense-ruby-lib → cs}/relation/sensor_data_relation.rb +7 -52
 - data/lib/{commonsense-ruby-lib → cs}/relation/sensor_relation.rb +28 -55
 - data/lib/cs/relation/trigger_relation.rb +21 -0
 - data/lib/cs/relation/user_relation.rb +20 -0
 - data/lib/{commonsense-ruby-lib → cs}/serializer.rb +1 -1
 - data/lib/cs/session.rb +170 -0
 - data/lib/cs/time.rb +45 -0
 - data/lib/cs/version.rb +3 -0
 - data/spec/features/sensor_management_spec.rb +146 -45
 - data/spec/features/user_management_spec.rb +94 -22
 - data/spec/lib/cs/collection/sensor_data_collection_spec.rb +27 -0
 - data/spec/lib/cs/end_point/group_spec.rb +120 -0
 - data/spec/lib/cs/end_point/sensor_data_spec.rb +110 -0
 - data/spec/lib/{commonsense-ruby-lib → cs}/end_point/sensor_spec.rb +6 -6
 - data/spec/lib/{commonsense-ruby-lib → cs}/end_point/user_spec.rb +14 -7
 - data/spec/lib/{commonsense-ruby-lib → cs}/end_point_spec.rb +25 -12
 - data/spec/lib/cs/relation/group_relation_spec.rb +103 -0
 - data/spec/lib/cs/relation/sensor_data_relation_spec.rb +184 -0
 - data/spec/lib/cs/relation/sensor_relation_spec.rb +192 -0
 - data/spec/lib/cs/relation/user_relation_spec.rb +81 -0
 - data/spec/lib/cs/relation_spec.rb +151 -0
 - data/spec/lib/cs/session_spec.rb +91 -0
 - data/spec/lib/cs/time_spec.rb +71 -0
 - data/spec/lib/cs_spec.rb +85 -0
 - data/spec/spec_helper.rb +6 -26
 - metadata +69 -86
 - data/lib/commonsense-ruby-lib/end_point/group.rb +0 -28
 - data/lib/commonsense-ruby-lib/relation.rb +0 -233
 - data/lib/commonsense-ruby-lib/session.rb +0 -105
 - data/lib/commonsense-ruby-lib/version.rb +0 -3
 - data/spec/lib/commonsense-ruby-lib/end_point/sensor_data_spec.rb +0 -68
 - data/spec/lib/commonsense-ruby-lib/relation/sensor_data_relation_spec.rb +0 -444
 - data/spec/lib/commonsense-ruby-lib/relation/sensor_relation_spec.rb +0 -165
 - data/spec/lib/commonsense-ruby-lib/session_spec.rb +0 -43
 - data/spec/lib/commonsense-ruby-lib_spec.rb +0 -51
 
| 
         @@ -0,0 +1,81 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'cs/end_point/user'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            module CS
         
     | 
| 
      
 5 
     | 
    
         
            +
              module Relation
         
     | 
| 
      
 6 
     | 
    
         
            +
                describe UserRelation do
         
     | 
| 
      
 7 
     | 
    
         
            +
                  let(:users) {
         
     | 
| 
      
 8 
     | 
    
         
            +
                    {"users" => [{"name" => "user1"}, {"name" => "user2"}, {"name" => "user3"}], "total" => 3}
         
     | 
| 
      
 9 
     | 
    
         
            +
                  }
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  let(:relation) {
         
     | 
| 
      
 12 
     | 
    
         
            +
                    relation = UserRelation.new
         
     | 
| 
      
 13 
     | 
    
         
            +
                    relation.stub("check_session!").and_return(true)
         
     | 
| 
      
 14 
     | 
    
         
            +
                    relation.stub("get_data!").and_return(users)
         
     | 
| 
      
 15 
     | 
    
         
            +
                    relation
         
     | 
| 
      
 16 
     | 
    
         
            +
                  }
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  describe "get_data!" do
         
     | 
| 
      
 19 
     | 
    
         
            +
                    it "should fetch sensor data from commonSense" do
         
     | 
| 
      
 20 
     | 
    
         
            +
                      session = double('Session')
         
     | 
| 
      
 21 
     | 
    
         
            +
                      option = {page: 100, per_page: 99}
         
     | 
| 
      
 22 
     | 
    
         
            +
                      session.should_receive(:get).with("/users.json", option)
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                      relation = UserRelation.new(session)
         
     | 
| 
      
 25 
     | 
    
         
            +
                      relation.get_data!(page: 100, per_page: 99)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    end
         
     | 
| 
      
 27 
     | 
    
         
            +
                  end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  describe "each" do
         
     | 
| 
      
 30 
     | 
    
         
            +
                    it "should get all user and yield each" do
         
     | 
| 
      
 31 
     | 
    
         
            +
                      session = double('Session')
         
     | 
| 
      
 32 
     | 
    
         
            +
                      relation = UserRelation.new(session)
         
     | 
| 
      
 33 
     | 
    
         
            +
                      relation.stub("get_data!").and_return(users)
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                      expect { |b| relation.each(&b) }.to yield_successive_args(EndPoint::User, EndPoint::User, EndPoint::User)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                    context "empty result" do
         
     | 
| 
      
 39 
     | 
    
         
            +
                      it "should not yield control" do
         
     | 
| 
      
 40 
     | 
    
         
            +
                        session = double('Session')
         
     | 
| 
      
 41 
     | 
    
         
            +
                        relation = UserRelation.new(session)
         
     | 
| 
      
 42 
     | 
    
         
            +
                        relation.stub("get_data!").and_return({"users" => [], "total" => 0})
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                        expect { |b| relation.each(&b) }.not_to yield_control
         
     | 
| 
      
 45 
     | 
    
         
            +
                      end
         
     | 
| 
      
 46 
     | 
    
         
            +
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                    context "limit specified" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                      it "should yield sensor at most specified by limit" do
         
     | 
| 
      
 50 
     | 
    
         
            +
                        relation.limit(1).to_a.count.should eq(1)
         
     | 
| 
      
 51 
     | 
    
         
            +
                      end
         
     | 
| 
      
 52 
     | 
    
         
            +
                    end
         
     | 
| 
      
 53 
     | 
    
         
            +
                  end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                  describe "last" do
         
     | 
| 
      
 57 
     | 
    
         
            +
                    it "should return the last record" do
         
     | 
| 
      
 58 
     | 
    
         
            +
                      relation = SensorRelation.new
         
     | 
| 
      
 59 
     | 
    
         
            +
                      relation.stub("count").and_return(3)
         
     | 
| 
      
 60 
     | 
    
         
            +
                      relation.should_receive("get_data").with(page:2, per_page:1).and_return({"sensors" => [{"name" => "sensor3"}], "total" => 3})
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                      first = relation.last
         
     | 
| 
      
 63 
     | 
    
         
            +
                      first.should be_kind_of(EndPoint::Sensor)
         
     | 
| 
      
 64 
     | 
    
         
            +
                      first.name.should eq("sensor3")
         
     | 
| 
      
 65 
     | 
    
         
            +
                    end
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                    context "with parameters given" do
         
     | 
| 
      
 68 
     | 
    
         
            +
                      it "should return the last record with options" 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:1, owned:1, physical:1, details:'full').and_return({"sensors" => [{"name" => "sensor3"}], "total" => 3})
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                        first = relation.where(shared: true, owned: true, physical:true, details: 'full').last
         
     | 
| 
      
 74 
     | 
    
         
            +
                        first.should be_kind_of(EndPoint::Sensor)
         
     | 
| 
      
 75 
     | 
    
         
            +
                        first.name.should eq("sensor3")
         
     | 
| 
      
 76 
     | 
    
         
            +
                      end
         
     | 
| 
      
 77 
     | 
    
         
            +
                    end
         
     | 
| 
      
 78 
     | 
    
         
            +
                  end
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
              end
         
     | 
| 
      
 81 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,151 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module CS
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe Relation do
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                let(:relation) {
         
     | 
| 
      
 7 
     | 
    
         
            +
                  class Model
         
     | 
| 
      
 8 
     | 
    
         
            +
                    include Relation
         
     | 
| 
      
 9 
     | 
    
         
            +
                    parameter :number, Integer
         
     | 
| 
      
 10 
     | 
    
         
            +
                    parameter :page, Integer, default: 0
         
     | 
| 
      
 11 
     | 
    
         
            +
                    parameter :per_page, Integer, default: 1000, maximum: 1000
         
     | 
| 
      
 12 
     | 
    
         
            +
                    parameter :start_date, Time
         
     | 
| 
      
 13 
     | 
    
         
            +
                    parameter :end_date, Time
         
     | 
| 
      
 14 
     | 
    
         
            +
                    parameter :date, Time
         
     | 
| 
      
 15 
     | 
    
         
            +
                    parameter :last, Boolean
         
     | 
| 
      
 16 
     | 
    
         
            +
                    parameter :sort, String, valid_values: ["ASC", "DESC"]
         
     | 
| 
      
 17 
     | 
    
         
            +
                    parameter :interval, Integer, valid_values: [604800, 86400, 3600, 1800, 600, 300, 60]
         
     | 
| 
      
 18 
     | 
    
         
            +
                    parameter :sensor_id, String
         
     | 
| 
      
 19 
     | 
    
         
            +
                    parameter_alias :from, :start_date
         
     | 
| 
      
 20 
     | 
    
         
            +
                    parameter_alias :to, :end_date
         
     | 
| 
      
 21 
     | 
    
         
            +
                  end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  Model.new
         
     | 
| 
      
 24 
     | 
    
         
            +
                }
         
     | 
| 
      
 25 
     | 
    
         
            +
             
     | 
| 
      
 26 
     | 
    
         
            +
                describe "parameter" do
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  context "Integer parameter with default" do
         
     | 
| 
      
 29 
     | 
    
         
            +
                    it "should assign default parameter" do
         
     | 
| 
      
 30 
     | 
    
         
            +
                      relation.page.should eq(0)
         
     | 
| 
      
 31 
     | 
    
         
            +
                    end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                    it "should update the parameter" do
         
     | 
| 
      
 34 
     | 
    
         
            +
                      relation.where(page: 2)
         
     | 
| 
      
 35 
     | 
    
         
            +
                      relation.page.should eq(2)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    end
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                    it "should set only to a maximum number" do
         
     | 
| 
      
 39 
     | 
    
         
            +
                      relation.where(per_page: 2000)
         
     | 
| 
      
 40 
     | 
    
         
            +
                      relation.per_page.should eq(1000)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                    it "should raise exception if not a number given" do
         
     | 
| 
      
 44 
     | 
    
         
            +
                      expect {
         
     | 
| 
      
 45 
     | 
    
         
            +
                        relation.where(number: 'a')
         
     | 
| 
      
 46 
     | 
    
         
            +
                      }.to raise_error(ArgumentError, "Received non Integer value for parameter 'number'")
         
     | 
| 
      
 47 
     | 
    
         
            +
                    end
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                  context  "Integer with default value" do
         
     | 
| 
      
 51 
     | 
    
         
            +
                    describe "valid parameter given" do
         
     | 
| 
      
 52 
     | 
    
         
            +
                      it "should update the  parameter" do
         
     | 
| 
      
 53 
     | 
    
         
            +
                        [604800, 86400, 3600, 1800, 600, 300, 60].each do |interval|
         
     | 
| 
      
 54 
     | 
    
         
            +
                          relation.where(interval: interval)
         
     | 
| 
      
 55 
     | 
    
         
            +
                          relation.interval.should eq(interval)
         
     | 
| 
      
 56 
     | 
    
         
            +
                        end
         
     | 
| 
      
 57 
     | 
    
         
            +
                      end
         
     | 
| 
      
 58 
     | 
    
         
            +
                    end
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
                    describe "invalid parameter given" do
         
     | 
| 
      
 61 
     | 
    
         
            +
                      it "should set the parameter to nil" do
         
     | 
| 
      
 62 
     | 
    
         
            +
                        expect { relation.where(interval: 10) }.to raise_error(ArgumentError)
         
     | 
| 
      
 63 
     | 
    
         
            +
                        expect { relation.where(interval: 'a') }.to raise_error(ArgumentError)
         
     | 
| 
      
 64 
     | 
    
         
            +
                      end
         
     | 
| 
      
 65 
     | 
    
         
            +
                    end
         
     | 
| 
      
 66 
     | 
    
         
            +
                  end
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
                  context "Time parameter given" do
         
     | 
| 
      
 69 
     | 
    
         
            +
                    describe "number given" do
         
     | 
| 
      
 70 
     | 
    
         
            +
                      it "should update the start_date" do
         
     | 
| 
      
 71 
     | 
    
         
            +
                        relation.where(start_date: 2)
         
     | 
| 
      
 72 
     | 
    
         
            +
                        relation.start_date.to_f.should eq(2.0)
         
     | 
| 
      
 73 
     | 
    
         
            +
                        relation.start_date.should be_kind_of(Time)
         
     | 
| 
      
 74 
     | 
    
         
            +
                      end
         
     | 
| 
      
 75 
     | 
    
         
            +
                    end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
                    describe "Time given" do
         
     | 
| 
      
 78 
     | 
    
         
            +
                      it "should update the start_date" do
         
     | 
| 
      
 79 
     | 
    
         
            +
                        relation.where(start_date: Time.at(19))
         
     | 
| 
      
 80 
     | 
    
         
            +
                        relation.start_date.to_f.should eq(19)
         
     | 
| 
      
 81 
     | 
    
         
            +
                      end
         
     | 
| 
      
 82 
     | 
    
         
            +
                    end
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
                    describe "Object that respond to 'to_time` given" do
         
     | 
| 
      
 85 
     | 
    
         
            +
                      it "should update the start_date" do
         
     | 
| 
      
 86 
     | 
    
         
            +
                        double = double()
         
     | 
| 
      
 87 
     | 
    
         
            +
                        double.should_receive(:to_time).and_return (Time.at(2))
         
     | 
| 
      
 88 
     | 
    
         
            +
                        relation.where(start_date: double)
         
     | 
| 
      
 89 
     | 
    
         
            +
                        relation.start_date.to_f.should eq(2.0)
         
     | 
| 
      
 90 
     | 
    
         
            +
                        relation.start_date.should be_kind_of(Time)
         
     | 
| 
      
 91 
     | 
    
         
            +
                      end
         
     | 
| 
      
 92 
     | 
    
         
            +
                    end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                    describe "Object that not respond to 'to_time' given" do
         
     | 
| 
      
 95 
     | 
    
         
            +
                      it "should raise error" do
         
     | 
| 
      
 96 
     | 
    
         
            +
                        expect { relation.where(end_date: 'foo') }.to raise_error(NoMethodError)
         
     | 
| 
      
 97 
     | 
    
         
            +
                      end
         
     | 
| 
      
 98 
     | 
    
         
            +
                    end
         
     | 
| 
      
 99 
     | 
    
         
            +
                  end
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                  context "Boolean parameter given" do
         
     | 
| 
      
 102 
     | 
    
         
            +
                    describe "with boolean value" do
         
     | 
| 
      
 103 
     | 
    
         
            +
                      it "should update the last parameter" do
         
     | 
| 
      
 104 
     | 
    
         
            +
                        [true, false].each do |value|
         
     | 
| 
      
 105 
     | 
    
         
            +
                          relation.where(last: value)
         
     | 
| 
      
 106 
     | 
    
         
            +
                          relation.parameter(:last).should be_true
         
     | 
| 
      
 107 
     | 
    
         
            +
                        end
         
     | 
| 
      
 108 
     | 
    
         
            +
                      end
         
     | 
| 
      
 109 
     | 
    
         
            +
                    end
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                    describe "with value not boolean given, should assign true" do
         
     | 
| 
      
 112 
     | 
    
         
            +
                      it "should assign true" do
         
     | 
| 
      
 113 
     | 
    
         
            +
                        relation.where(last: 'a')
         
     | 
| 
      
 114 
     | 
    
         
            +
                        relation.parameter(:last).should be_true
         
     | 
| 
      
 115 
     | 
    
         
            +
                      end
         
     | 
| 
      
 116 
     | 
    
         
            +
                    end
         
     | 
| 
      
 117 
     | 
    
         
            +
                  end
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                  context "String parameter with valid value" do
         
     | 
| 
      
 120 
     | 
    
         
            +
                    describe "valid parameter given" do
         
     | 
| 
      
 121 
     | 
    
         
            +
                      it "should update the sort parameter" do
         
     | 
| 
      
 122 
     | 
    
         
            +
                        relation.where(sort: 'ASC')
         
     | 
| 
      
 123 
     | 
    
         
            +
                        relation.parameter(:sort).should eq('ASC')
         
     | 
| 
      
 124 
     | 
    
         
            +
             
     | 
| 
      
 125 
     | 
    
         
            +
                        relation.where(sort: 'DESC')
         
     | 
| 
      
 126 
     | 
    
         
            +
                        relation.parameter(:sort).should eq('DESC')
         
     | 
| 
      
 127 
     | 
    
         
            +
                      end
         
     | 
| 
      
 128 
     | 
    
         
            +
                    end
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
                    describe "invalid parameter given" do
         
     | 
| 
      
 131 
     | 
    
         
            +
                      it "should set parameter to nil" do
         
     | 
| 
      
 132 
     | 
    
         
            +
                        expect { relation.where(sort: 'foo') }.to raise_error(ArgumentError)
         
     | 
| 
      
 133 
     | 
    
         
            +
                      end
         
     | 
| 
      
 134 
     | 
    
         
            +
                    end
         
     | 
| 
      
 135 
     | 
    
         
            +
                  end
         
     | 
| 
      
 136 
     | 
    
         
            +
                end
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
                describe "get_options" do
         
     | 
| 
      
 139 
     | 
    
         
            +
                  it "return parameter that have default value" do
         
     | 
| 
      
 140 
     | 
    
         
            +
                    options = relation.get_options({})
         
     | 
| 
      
 141 
     | 
    
         
            +
                    options[:page].should eq(0)
         
     | 
| 
      
 142 
     | 
    
         
            +
                    options[:per_page].should eq(1000)
         
     | 
| 
      
 143 
     | 
    
         
            +
                  end
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
                  it "should return aliases" do
         
     | 
| 
      
 146 
     | 
    
         
            +
                    option = relation.where(start_date: 1).get_options
         
     | 
| 
      
 147 
     | 
    
         
            +
                    option[:start_date].should eq(1)
         
     | 
| 
      
 148 
     | 
    
         
            +
                  end
         
     | 
| 
      
 149 
     | 
    
         
            +
                end
         
     | 
| 
      
 150 
     | 
    
         
            +
              end
         
     | 
| 
      
 151 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,91 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'logger'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'webmock/rspec'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            describe "session" do
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
              let!(:user) do
         
     | 
| 
      
 8 
     | 
    
         
            +
                username = "user1@tester.com"
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                client = CS::Client.new(base_uri: base_uri)
         
     | 
| 
      
 11 
     | 
    
         
            +
                user = client.new_user
         
     | 
| 
      
 12 
     | 
    
         
            +
                user.username = username
         
     | 
| 
      
 13 
     | 
    
         
            +
                user.email = user.username
         
     | 
| 
      
 14 
     | 
    
         
            +
                user.password = 'password'
         
     | 
| 
      
 15 
     | 
    
         
            +
                user.name = 'Jan'
         
     | 
| 
      
 16 
     | 
    
         
            +
                user.surname = 'jagger'
         
     | 
| 
      
 17 
     | 
    
         
            +
                user.address = 'Lloydstraat 5'
         
     | 
| 
      
 18 
     | 
    
         
            +
                user.zipcode = '3024ea'
         
     | 
| 
      
 19 
     | 
    
         
            +
                user.country = 'NETHERLANDS'
         
     | 
| 
      
 20 
     | 
    
         
            +
                user.mobile = '123456789'
         
     | 
| 
      
 21 
     | 
    
         
            +
                user
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              describe "login" do
         
     | 
| 
      
 25 
     | 
    
         
            +
                describe "with corrent username and password" do
         
     | 
| 
      
 26 
     | 
    
         
            +
                  it "should create new session" do
         
     | 
| 
      
 27 
     | 
    
         
            +
                    client = create_client
         
     | 
| 
      
 28 
     | 
    
         
            +
                    CS::Auth::HTTP.any_instance.stub(login: "1234")
         
     | 
| 
      
 29 
     | 
    
         
            +
                    session_id = client.login!(user.username, 'password')
         
     | 
| 
      
 30 
     | 
    
         
            +
                    session_id.should_not be_nil
         
     | 
| 
      
 31 
     | 
    
         
            +
                    client.session.should_not be_nil
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                describe "with incorrect username or password" do
         
     | 
| 
      
 36 
     | 
    
         
            +
                  it "should create new session" do
         
     | 
| 
      
 37 
     | 
    
         
            +
                    client = create_client
         
     | 
| 
      
 38 
     | 
    
         
            +
                    CS::Auth::HTTP.any_instance.stub(login: false)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    session_id = client.login(user.username, "foo")
         
     | 
| 
      
 40 
     | 
    
         
            +
                    session_id.should be_false
         
     | 
| 
      
 41 
     | 
    
         
            +
                    client.session.should_not be_nil
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
      
 43 
     | 
    
         
            +
                end
         
     | 
| 
      
 44 
     | 
    
         
            +
              end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
              describe "logger" do
         
     | 
| 
      
 47 
     | 
    
         
            +
                context "Debug level given" do
         
     | 
| 
      
 48 
     | 
    
         
            +
                  it "should write to logger (STDOUT)" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                    client = create_client
         
     | 
| 
      
 50 
     | 
    
         
            +
                    client.login(user.username, 'password')
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                    CS::Auth::HTTP.any_instance.stub(get: "")
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                    session = client.session
         
     | 
| 
      
 55 
     | 
    
         
            +
                    logger = double().as_null_object
         
     | 
| 
      
 56 
     | 
    
         
            +
                    session.logger = logger
         
     | 
| 
      
 57 
     | 
    
         
            +
                    logger.should_receive("info").with("").ordered
         
     | 
| 
      
 58 
     | 
    
         
            +
                    logger.should_receive("info").with("GET /users/current.json").ordered
         
     | 
| 
      
 59 
     | 
    
         
            +
                    logger.should_receive("debug").with("headers: {}").ordered
         
     | 
| 
      
 60 
     | 
    
         
            +
                    session.get('/users/current.json', '',{})
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
              describe "oauth" do
         
     | 
| 
      
 66 
     | 
    
         
            +
                describe "with correct access token" do
         
     | 
| 
      
 67 
     | 
    
         
            +
                   it "should create new session with oauth" do
         
     | 
| 
      
 68 
     | 
    
         
            +
                     client = create_client
         
     | 
| 
      
 69 
     | 
    
         
            +
                     CS::Auth::OAuth.should_receive(:new).with('CS_CONSUMER_KEY', 'CS_CONSUMER_SECRET',
         
     | 
| 
      
 70 
     | 
    
         
            +
                                            'CS_ACCESS_TOKEN', 'CS_ACCESS_TOKEN_SECRET', base_uri)
         
     | 
| 
      
 71 
     | 
    
         
            +
                     client.oauth('CS_CONSUMER_KEY', 'CS_CONSUMER_SECRET',
         
     | 
| 
      
 72 
     | 
    
         
            +
                                            'CS_ACCESS_TOKEN', 'CS_ACCESS_TOKEN_SECRET')
         
     | 
| 
      
 73 
     | 
    
         
            +
                   end
         
     | 
| 
      
 74 
     | 
    
         
            +
                end
         
     | 
| 
      
 75 
     | 
    
         
            +
              end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
              describe "API_KEY" do
         
     | 
| 
      
 78 
     | 
    
         
            +
                it "should append API key in the url" do
         
     | 
| 
      
 79 
     | 
    
         
            +
                  client = create_client
         
     | 
| 
      
 80 
     | 
    
         
            +
                  client.api_key = '123456'
         
     | 
| 
      
 81 
     | 
    
         
            +
                  CS::Auth::HTTP.should_receive(:get).
         
     | 
| 
      
 82 
     | 
    
         
            +
                    with("/sensors.json?API_KEY=123456",
         
     | 
| 
      
 83 
     | 
    
         
            +
                         {:query=>{:page=>0, :per_page=>1000},
         
     | 
| 
      
 84 
     | 
    
         
            +
                          :headers=>{"Content-Type"=>"application/json"}}
         
     | 
| 
      
 85 
     | 
    
         
            +
                        )
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                  result = client.sensors
         
     | 
| 
      
 88 
     | 
    
         
            +
                  result.to_a
         
     | 
| 
      
 89 
     | 
    
         
            +
                end
         
     | 
| 
      
 90 
     | 
    
         
            +
              end
         
     | 
| 
      
 91 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,71 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module CS
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe Time do
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                context "Initialize with nill" do
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                  it "should return current time" do
         
     | 
| 
      
 9 
     | 
    
         
            +
                    ::Time.should_receive(:new).with(no_args())
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                    Time.new
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
                context "initialize with Time object" do
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  it "should set date as time" do
         
     | 
| 
      
 19 
     | 
    
         
            +
                    time = ::Time.new
         
     | 
| 
      
 20 
     | 
    
         
            +
                    Time.new(time).time.should be_instance_of(::Time)
         
     | 
| 
      
 21 
     | 
    
         
            +
                  end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                context "initialize with number" do
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  it "should set data based on that time" do
         
     | 
| 
      
 28 
     | 
    
         
            +
                    epoch = 0
         
     | 
| 
      
 29 
     | 
    
         
            +
                    time = Time.new(epoch)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    time.should be_instance_of(Time)
         
     | 
| 
      
 31 
     | 
    
         
            +
                    time.time.to_i.should == 0
         
     | 
| 
      
 32 
     | 
    
         
            +
                  end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                context "initialize with TimeLord" do
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                  it "should set time property as ruby Time Object" do
         
     | 
| 
      
 39 
     | 
    
         
            +
                    require 'time-lord'
         
     | 
| 
      
 40 
     | 
    
         
            +
                    period = 1.hours.ago
         
     | 
| 
      
 41 
     | 
    
         
            +
                    period.should be_instance_of(TimeLord::Period)
         
     | 
| 
      
 42 
     | 
    
         
            +
                    time = Time.new(period)
         
     | 
| 
      
 43 
     | 
    
         
            +
                    expected = ::Time.new.to_i - 3600
         
     | 
| 
      
 44 
     | 
    
         
            +
                    time.time.to_i.should be_within(1).of(expected)
         
     | 
| 
      
 45 
     | 
    
         
            +
                  end
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
                context "given an object" do
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                  it "should get the time by calling to_time on that object" do
         
     | 
| 
      
 52 
     | 
    
         
            +
                    mock = double()
         
     | 
| 
      
 53 
     | 
    
         
            +
                    t = ::Time.new
         
     | 
| 
      
 54 
     | 
    
         
            +
                    mock.should_receive(:to_time).and_return(t)
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                    time = Time.new(mock)
         
     | 
| 
      
 57 
     | 
    
         
            +
                    time.time.should == t
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                  end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                it "should proxy object to date" do
         
     | 
| 
      
 64 
     | 
    
         
            +
                  subject = ::Time.new
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                  subject.should_receive(:to_f).and_return(3.33333)
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
                  time = Time.new(subject).to_f
         
     | 
| 
      
 69 
     | 
    
         
            +
                end
         
     | 
| 
      
 70 
     | 
    
         
            +
              end
         
     | 
| 
      
 71 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/lib/cs_spec.rb
    ADDED
    
    | 
         @@ -0,0 +1,85 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'webmock/rspec'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            module CS
         
     | 
| 
      
 5 
     | 
    
         
            +
              describe Client do
         
     | 
| 
      
 6 
     | 
    
         
            +
                describe "client with authentication" do
         
     | 
| 
      
 7 
     | 
    
         
            +
                  let(:client) do
         
     | 
| 
      
 8 
     | 
    
         
            +
                    create_client
         
     | 
| 
      
 9 
     | 
    
         
            +
                  end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                  let!(:logged_in_client) do
         
     | 
| 
      
 12 
     | 
    
         
            +
                    client = CS::Client.new(base_uri: base_uri)
         
     | 
| 
      
 13 
     | 
    
         
            +
                    client.session_id = '123499'
         
     | 
| 
      
 14 
     | 
    
         
            +
                    client
         
     | 
| 
      
 15 
     | 
    
         
            +
                  end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                  describe "current_user" do
         
     | 
| 
      
 18 
     | 
    
         
            +
                    it "should return current user information" do
         
     | 
| 
      
 19 
     | 
    
         
            +
                      user = EndPoint::User.new
         
     | 
| 
      
 20 
     | 
    
         
            +
                      EndPoint::User.any_instance.stub(current_user: user)
         
     | 
| 
      
 21 
     | 
    
         
            +
                      current_user = client.current_user
         
     | 
| 
      
 22 
     | 
    
         
            +
                      current_user.should == user
         
     | 
| 
      
 23 
     | 
    
         
            +
                      current_user.to_h.should be_kind_of Hash
         
     | 
| 
      
 24 
     | 
    
         
            +
                    end
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  describe "current_groups" do
         
     | 
| 
      
 28 
     | 
    
         
            +
                    it "should return groups that current user belongs to" do
         
     | 
| 
      
 29 
     | 
    
         
            +
                      groups = [ EndPoint::Group.new ]
         
     | 
| 
      
 30 
     | 
    
         
            +
                      EndPoint::Group.any_instance.stub(current_groups: groups)
         
     | 
| 
      
 31 
     | 
    
         
            +
                      groups = logged_in_client.current_groups
         
     | 
| 
      
 32 
     | 
    
         
            +
                      groups.should_not be_empty
         
     | 
| 
      
 33 
     | 
    
         
            +
                    end
         
     | 
| 
      
 34 
     | 
    
         
            +
                  end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
                  describe "sensors" do
         
     | 
| 
      
 37 
     | 
    
         
            +
                    it "should return Sensors relation" do
         
     | 
| 
      
 38 
     | 
    
         
            +
                      client.sensors.should be_a_kind_of(CS::Relation::SensorRelation)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    end
         
     | 
| 
      
 40 
     | 
    
         
            +
                  end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  describe "logger" do
         
     | 
| 
      
 43 
     | 
    
         
            +
                    context "when login using user & password" do
         
     | 
| 
      
 44 
     | 
    
         
            +
                      it "should assign the new session" do
         
     | 
| 
      
 45 
     | 
    
         
            +
                        logger = double()
         
     | 
| 
      
 46 
     | 
    
         
            +
                        Session.any_instance.stub(login: '1234')
         
     | 
| 
      
 47 
     | 
    
         
            +
                        client.logger = logger
         
     | 
| 
      
 48 
     | 
    
         
            +
                        client.login('foo', 'bar')
         
     | 
| 
      
 49 
     | 
    
         
            +
                        client.session.logger.should == logger
         
     | 
| 
      
 50 
     | 
    
         
            +
                      end
         
     | 
| 
      
 51 
     | 
    
         
            +
                    end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                    context "when login using oauth" do
         
     | 
| 
      
 54 
     | 
    
         
            +
                      it "should assign logger" do
         
     | 
| 
      
 55 
     | 
    
         
            +
                        logger = double()
         
     | 
| 
      
 56 
     | 
    
         
            +
                        client.logger = logger
         
     | 
| 
      
 57 
     | 
    
         
            +
                        client.oauth('', '', '', '')
         
     | 
| 
      
 58 
     | 
    
         
            +
                        client.session.logger.should == logger
         
     | 
| 
      
 59 
     | 
    
         
            +
                      end
         
     | 
| 
      
 60 
     | 
    
         
            +
                    end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
      
 62 
     | 
    
         
            +
                    context "when specifying session_id" do
         
     | 
| 
      
 63 
     | 
    
         
            +
                      it "should assign logger" do
         
     | 
| 
      
 64 
     | 
    
         
            +
                        logger = double()
         
     | 
| 
      
 65 
     | 
    
         
            +
                        client.logger = logger
         
     | 
| 
      
 66 
     | 
    
         
            +
                        client.session_id = '1234'
         
     | 
| 
      
 67 
     | 
    
         
            +
                        client.session.logger.should == logger
         
     | 
| 
      
 68 
     | 
    
         
            +
                      end
         
     | 
| 
      
 69 
     | 
    
         
            +
                    end
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                    context "setting logger" do
         
     | 
| 
      
 72 
     | 
    
         
            +
                      it "it should set logger to existing session" do
         
     | 
| 
      
 73 
     | 
    
         
            +
                        logger = double()
         
     | 
| 
      
 74 
     | 
    
         
            +
                        client.session_id = '1234'
         
     | 
| 
      
 75 
     | 
    
         
            +
                        client.logger = logger
         
     | 
| 
      
 76 
     | 
    
         
            +
                        client.session.logger.should == logger
         
     | 
| 
      
 77 
     | 
    
         
            +
                        client.logger = nil
         
     | 
| 
      
 78 
     | 
    
         
            +
                        client.session.logger.should be_nil
         
     | 
| 
      
 79 
     | 
    
         
            +
                      end
         
     | 
| 
      
 80 
     | 
    
         
            +
                    end
         
     | 
| 
      
 81 
     | 
    
         
            +
                  end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
                end
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
      
 85 
     | 
    
         
            +
            end
         
     |