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
 
| 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require "oauth"
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            module  
     | 
| 
      
 3 
     | 
    
         
            +
            module CS
         
     | 
| 
       4 
4 
     | 
    
         
             
              module Auth
         
     | 
| 
       5 
5 
     | 
    
         
             
                class OAuth
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
         @@ -21,48 +21,48 @@ module CommonSense 
     | 
|
| 
       21 
21 
     | 
    
         
             
                    @oauth
         
     | 
| 
       22 
22 
     | 
    
         
             
                  end
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
                  def  
     | 
| 
      
 24 
     | 
    
         
            +
                  def execute(&block)
         
     | 
| 
       25 
25 
     | 
    
         
             
                    reset
         
     | 
| 
       26 
     | 
    
         
            -
                     
     | 
| 
       27 
     | 
    
         
            -
                    response = oauth.get(path, headers)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    response = yield
         
     | 
| 
       28 
27 
     | 
    
         
             
                    parse_response(response)
         
     | 
| 
       29 
28 
     | 
    
         
             
                    @response_body
         
     | 
| 
       30 
29 
     | 
    
         
             
                  end
         
     | 
| 
       31 
30 
     | 
    
         | 
| 
       32 
     | 
    
         
            -
                  def  
     | 
| 
       33 
     | 
    
         
            -
                     
     | 
| 
       34 
     | 
    
         
            -
             
     | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
      
 31 
     | 
    
         
            +
                  def get(path, query={}, headers = {})
         
     | 
| 
      
 32 
     | 
    
         
            +
                    execute do
         
     | 
| 
      
 33 
     | 
    
         
            +
                      path += '?' + URI.encode_www_form(query) unless query.empty?
         
     | 
| 
      
 34 
     | 
    
         
            +
                      headers = default_headers.merge(headers)
         
     | 
| 
      
 35 
     | 
    
         
            +
                      oauth.get(path, headers)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    end
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
       37 
38 
     | 
    
         | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
      
 39 
     | 
    
         
            +
                  def post(path, body = '', headers = {})
         
     | 
| 
      
 40 
     | 
    
         
            +
                    execute do
         
     | 
| 
      
 41 
     | 
    
         
            +
                      headers = default_headers.merge(headers)
         
     | 
| 
      
 42 
     | 
    
         
            +
                      oauth.post(path, body.to_json, headers)
         
     | 
| 
      
 43 
     | 
    
         
            +
                    end
         
     | 
| 
       39 
44 
     | 
    
         
             
                  end
         
     | 
| 
       40 
45 
     | 
    
         | 
| 
       41 
46 
     | 
    
         
             
                  def put(path, body = '', headers = {})
         
     | 
| 
       42 
     | 
    
         
            -
                     
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
             
     | 
| 
       45 
     | 
    
         
            -
                     
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
                    @response_body
         
     | 
| 
      
 47 
     | 
    
         
            +
                    execute do
         
     | 
| 
      
 48 
     | 
    
         
            +
                      headers = default_headers.merge(headers)
         
     | 
| 
      
 49 
     | 
    
         
            +
                      oauth.put(path, body.to_json, headers)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    end
         
     | 
| 
       48 
51 
     | 
    
         
             
                  end
         
     | 
| 
       49 
52 
     | 
    
         | 
| 
       50 
53 
     | 
    
         
             
                  def delete(path, query={}, headers = {})
         
     | 
| 
       51 
     | 
    
         
            -
                     
     | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
       56 
     | 
    
         
            -
                    @response_body
         
     | 
| 
      
 54 
     | 
    
         
            +
                    execute do
         
     | 
| 
      
 55 
     | 
    
         
            +
                      path += '?' + URI.encode_www_form(query) unless query.empty?
         
     | 
| 
      
 56 
     | 
    
         
            +
                      headers = default_headers.merge(headers)
         
     | 
| 
      
 57 
     | 
    
         
            +
                      oauth.delete(path, headers)
         
     | 
| 
      
 58 
     | 
    
         
            +
                    end
         
     | 
| 
       57 
59 
     | 
    
         
             
                  end
         
     | 
| 
       58 
60 
     | 
    
         | 
| 
       59 
61 
     | 
    
         
             
                  def head(path, headers = {})
         
     | 
| 
       60 
     | 
    
         
            -
                     
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                     
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
                    @response_body
         
     | 
| 
      
 62 
     | 
    
         
            +
                    execute do
         
     | 
| 
      
 63 
     | 
    
         
            +
                      headers = default_headers.merge(headers)
         
     | 
| 
      
 64 
     | 
    
         
            +
                      oauth.head(path, headers)
         
     | 
| 
      
 65 
     | 
    
         
            +
                    end
         
     | 
| 
       66 
66 
     | 
    
         
             
                  end
         
     | 
| 
       67 
67 
     | 
    
         | 
| 
       68 
68 
     | 
    
         
             
                  def base_uri=(uri = nil)
         
     | 
| 
         @@ -0,0 +1,61 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'delegate'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module CS
         
     | 
| 
      
 4 
     | 
    
         
            +
              module Collection
         
     | 
| 
      
 5 
     | 
    
         
            +
                class SensorDataCollection < SimpleDelegator
         
     | 
| 
      
 6 
     | 
    
         
            +
                  attr_accessor :session
         
     | 
| 
      
 7 
     | 
    
         
            +
                  attr_accessor :batch_size
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                  def initialize
         
     | 
| 
      
 10 
     | 
    
         
            +
                    @batch_size = 1000
         
     | 
| 
      
 11 
     | 
    
         
            +
                    super([])
         
     | 
| 
      
 12 
     | 
    
         
            +
                  end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
                  def save!
         
     | 
| 
      
 15 
     | 
    
         
            +
                    check_session!
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                    # group batch
         
     | 
| 
      
 18 
     | 
    
         
            +
                    self.each_slice(@batch_size) do |batch|
         
     | 
| 
      
 19 
     | 
    
         
            +
                      body = process_batch(batch)
         
     | 
| 
      
 20 
     | 
    
         
            +
                      @session.post(get_url, body)
         
     | 
| 
      
 21 
     | 
    
         
            +
                    end
         
     | 
| 
      
 22 
     | 
    
         
            +
                  end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  ##
         
     | 
| 
      
 25 
     | 
    
         
            +
                  # Given array of sensor data it will group the data by sensor_id
         
     | 
| 
      
 26 
     | 
    
         
            +
                  # and construct payload for multiple sensor upload
         
     | 
| 
      
 27 
     | 
    
         
            +
                  def process_batch(batch)
         
     | 
| 
      
 28 
     | 
    
         
            +
                    sensors = {}
         
     | 
| 
      
 29 
     | 
    
         
            +
                    batch.each do |point|
         
     | 
| 
      
 30 
     | 
    
         
            +
                      next if point.nil? || point.sensor_id.nil?
         
     | 
| 
      
 31 
     | 
    
         
            +
                      sensor_id = point.sensor_id
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                      if !sensors[sensor_id]
         
     | 
| 
      
 34 
     | 
    
         
            +
                        sensors[sensor_id] = {
         
     | 
| 
      
 35 
     | 
    
         
            +
                          sensor_id: sensor_id,
         
     | 
| 
      
 36 
     | 
    
         
            +
                          data: []
         
     | 
| 
      
 37 
     | 
    
         
            +
                        }
         
     | 
| 
      
 38 
     | 
    
         
            +
                      end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                      sensors[sensor_id][:data].push(point.to_cs_point)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    end
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
                    retval = []
         
     | 
| 
      
 44 
     | 
    
         
            +
                    sensors.each do |k, v|
         
     | 
| 
      
 45 
     | 
    
         
            +
                      retval.push(v)
         
     | 
| 
      
 46 
     | 
    
         
            +
                    end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                    {sensors: retval}
         
     | 
| 
      
 49 
     | 
    
         
            +
                  end
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                  def get_url
         
     | 
| 
      
 52 
     | 
    
         
            +
                    "/sensors/data.json"
         
     | 
| 
      
 53 
     | 
    
         
            +
                  end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                  private
         
     | 
| 
      
 56 
     | 
    
         
            +
                  def check_session!
         
     | 
| 
      
 57 
     | 
    
         
            +
                    raise Error::SessionEmptyError unless @session
         
     | 
| 
      
 58 
     | 
    
         
            +
                  end
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
              end
         
     | 
| 
      
 61 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require ' 
     | 
| 
       2 
     | 
    
         
            -
            module  
     | 
| 
      
 1 
     | 
    
         
            +
            require 'cs/serializer'
         
     | 
| 
      
 2 
     | 
    
         
            +
            module CS
         
     | 
| 
       3 
3 
     | 
    
         
             
              module EndPoint
         
     | 
| 
       4 
     | 
    
         
            -
                include  
     | 
| 
      
 4 
     | 
    
         
            +
                include CS::Serializer
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
                attr_accessor :session
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
         @@ -22,7 +22,7 @@ module CommonSense 
     | 
|
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
                # get value of property name
         
     | 
| 
       24 
24 
     | 
    
         
             
                def parameter(name)
         
     | 
| 
       25 
     | 
    
         
            -
                  self.instance_variable_get(name)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  self.instance_variable_get("@#{name}")
         
     | 
| 
       26 
26 
     | 
    
         
             
                end
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
                # Persist end point object to CS. It will create a new record on CS
         
     | 
| 
         @@ -44,20 +44,20 @@ module CommonSense 
     | 
|
| 
       44 
44 
     | 
    
         
             
                #
         
     | 
| 
       45 
45 
     | 
    
         
             
                #     sensor.name = "accelerometer edit"
         
     | 
| 
       46 
46 
     | 
    
         
             
                #     sensor.save! # this will update the sensor
         
     | 
| 
       47 
     | 
    
         
            -
                def save!
         
     | 
| 
      
 47 
     | 
    
         
            +
                def save!(options={})
         
     | 
| 
       48 
48 
     | 
    
         
             
                  check_session!
         
     | 
| 
       49 
49 
     | 
    
         | 
| 
       50 
50 
     | 
    
         
             
                  if @id
         
     | 
| 
       51 
     | 
    
         
            -
                    self.update!
         
     | 
| 
      
 51 
     | 
    
         
            +
                    self.update!(options)
         
     | 
| 
       52 
52 
     | 
    
         
             
                  else
         
     | 
| 
       53 
     | 
    
         
            -
                    self.create!
         
     | 
| 
      
 53 
     | 
    
         
            +
                    self.create!(options)
         
     | 
| 
       54 
54 
     | 
    
         
             
                  end
         
     | 
| 
       55 
55 
     | 
    
         
             
                end
         
     | 
| 
       56 
56 
     | 
    
         | 
| 
       57 
     | 
    
         
            -
                # it will persist data to CS just like {#save!} but it will return  
     | 
| 
      
 57 
     | 
    
         
            +
                # it will persist data to CS just like {#save!} but it will return false instead of exception
         
     | 
| 
       58 
58 
     | 
    
         
             
                # if it encouter error while persiting data
         
     | 
| 
       59 
     | 
    
         
            -
                def save
         
     | 
| 
       60 
     | 
    
         
            -
                  save! rescue  
     | 
| 
      
 59 
     | 
    
         
            +
                def save(options={})
         
     | 
| 
      
 60 
     | 
    
         
            +
                  save!(options) rescue false
         
     | 
| 
       61 
61 
     | 
    
         
             
                end
         
     | 
| 
       62 
62 
     | 
    
         | 
| 
       63 
63 
     | 
    
         
             
                # Create a new end point object to CS. It will raise an exception if there is an error
         
     | 
| 
         @@ -74,8 +74,9 @@ module CommonSense 
     | 
|
| 
       74 
74 
     | 
    
         
             
                #
         
     | 
| 
       75 
75 
     | 
    
         
             
                #     sensor.create! # this will create new sensor on CS
         
     | 
| 
       76 
76 
     | 
    
         
             
                #     sensor.id # should give you the id of the sensor
         
     | 
| 
       77 
     | 
    
         
            -
                def create!
         
     | 
| 
      
 77 
     | 
    
         
            +
                def create!(options={})
         
     | 
| 
       78 
78 
     | 
    
         
             
                  parameter = self.to_parameters
         
     | 
| 
      
 79 
     | 
    
         
            +
                  parameter.merge!(options)
         
     | 
| 
       79 
80 
     | 
    
         
             
                  res = session.post(post_url, parameter)
         
     | 
| 
       80 
81 
     | 
    
         | 
| 
       81 
82 
     | 
    
         
             
                  if session.response_code != 201
         
     | 
| 
         @@ -90,11 +91,10 @@ module CommonSense 
     | 
|
| 
       90 
91 
     | 
    
         
             
                  true
         
     | 
| 
       91 
92 
     | 
    
         
             
                end
         
     | 
| 
       92 
93 
     | 
    
         | 
| 
       93 
     | 
    
         
            -
                # Create a new endpoint object to CS, just like {#create!} but it will return  
     | 
| 
      
 94 
     | 
    
         
            +
                # Create a new endpoint object to CS, just like {#create!} but it will return false
         
     | 
| 
       94 
95 
     | 
    
         
             
                # if there is an error.
         
     | 
| 
       95 
     | 
    
         
            -
                def create
         
     | 
| 
       96 
     | 
    
         
            -
                   
     | 
| 
       97 
     | 
    
         
            -
                  not result.nil?
         
     | 
| 
      
 96 
     | 
    
         
            +
                def create(options={})
         
     | 
| 
      
 97 
     | 
    
         
            +
                  create!(options) rescue false
         
     | 
| 
       98 
98 
     | 
    
         
             
                end
         
     | 
| 
       99 
99 
     | 
    
         | 
| 
       100 
100 
     | 
    
         
             
                # Retrieve Data from CS of the current object based on the id of the object.
         
     | 
| 
         @@ -125,10 +125,9 @@ module CommonSense 
     | 
|
| 
       125 
125 
     | 
    
         
             
                end
         
     | 
| 
       126 
126 
     | 
    
         | 
| 
       127 
127 
     | 
    
         
             
                # it will retrieve / reload current object form CS, just like {#retrieve!} but it
         
     | 
| 
       128 
     | 
    
         
            -
                # will return  
     | 
| 
      
 128 
     | 
    
         
            +
                # will return false instead of raise an exception if there is an error.
         
     | 
| 
       129 
129 
     | 
    
         
             
                def retrieve
         
     | 
| 
       130 
     | 
    
         
            -
                   
     | 
| 
       131 
     | 
    
         
            -
                  not result.nil?
         
     | 
| 
      
 130 
     | 
    
         
            +
                  retrieve! rescue false
         
     | 
| 
       132 
131 
     | 
    
         
             
                end
         
     | 
| 
       133 
132 
     | 
    
         | 
| 
       134 
133 
     | 
    
         
             
                # alias for {#retrieve}
         
     | 
| 
         @@ -143,11 +142,12 @@ module CommonSense 
     | 
|
| 
       143 
142 
     | 
    
         
             
                #     sensor = client.sensors.find(1)
         
     | 
| 
       144 
143 
     | 
    
         
             
                #     sensor.name = "new name"
         
     | 
| 
       145 
144 
     | 
    
         
             
                #     sensor.update!
         
     | 
| 
       146 
     | 
    
         
            -
                def update!
         
     | 
| 
      
 145 
     | 
    
         
            +
                def update!(options={})
         
     | 
| 
       147 
146 
     | 
    
         
             
                  check_session!
         
     | 
| 
       148 
147 
     | 
    
         
             
                  raise Error::ResourceIdError unless @id
         
     | 
| 
       149 
148 
     | 
    
         | 
| 
       150 
149 
     | 
    
         
             
                  parameter = self.to_parameters
         
     | 
| 
      
 150 
     | 
    
         
            +
                  parameter.merge!(options)
         
     | 
| 
       151 
151 
     | 
    
         
             
                  res = session.put(put_url, parameter)
         
     | 
| 
       152 
152 
     | 
    
         | 
| 
       153 
153 
     | 
    
         
             
                  if session.response_code != 200
         
     | 
| 
         @@ -160,9 +160,8 @@ module CommonSense 
     | 
|
| 
       160 
160 
     | 
    
         | 
| 
       161 
161 
     | 
    
         
             
                # Update current end point object to CS, just like {#update!} but it will return nil
         
     | 
| 
       162 
162 
     | 
    
         
             
                # if there is an error
         
     | 
| 
       163 
     | 
    
         
            -
                def update
         
     | 
| 
       164 
     | 
    
         
            -
                   
     | 
| 
       165 
     | 
    
         
            -
                  not result.nil?
         
     | 
| 
      
 163 
     | 
    
         
            +
                def update(options={})
         
     | 
| 
      
 164 
     | 
    
         
            +
                  update!(options) rescue false
         
     | 
| 
       166 
165 
     | 
    
         
             
                end
         
     | 
| 
       167 
166 
     | 
    
         | 
| 
       168 
167 
     | 
    
         | 
| 
         @@ -192,8 +191,7 @@ module CommonSense 
     | 
|
| 
       192 
191 
     | 
    
         
             
                # Delete the current end point object from CS, just like {#delete!} but it will return nil
         
     | 
| 
       193 
192 
     | 
    
         
             
                # if there is an error
         
     | 
| 
       194 
193 
     | 
    
         
             
                def delete
         
     | 
| 
       195 
     | 
    
         
            -
                   
     | 
| 
       196 
     | 
    
         
            -
                  not result.nil?
         
     | 
| 
      
 194 
     | 
    
         
            +
                  delete! rescue false
         
     | 
| 
       197 
195 
     | 
    
         
             
                end
         
     | 
| 
       198 
196 
     | 
    
         | 
| 
       199 
197 
     | 
    
         
             
                # return the commonsense URL for method
         
     | 
| 
         @@ -205,6 +203,12 @@ module CommonSense 
     | 
|
| 
       205 
203 
     | 
    
         
             
                  url
         
     | 
| 
       206 
204 
     | 
    
         
             
                end
         
     | 
| 
       207 
205 
     | 
    
         | 
| 
      
 206 
     | 
    
         
            +
                def duplicate
         
     | 
| 
      
 207 
     | 
    
         
            +
                  clone = self.dup
         
     | 
| 
      
 208 
     | 
    
         
            +
                  clone.id = nil
         
     | 
| 
      
 209 
     | 
    
         
            +
                  clone
         
     | 
| 
      
 210 
     | 
    
         
            +
                end
         
     | 
| 
      
 211 
     | 
    
         
            +
             
     | 
| 
       208 
212 
     | 
    
         
             
                protected
         
     | 
| 
       209 
213 
     | 
    
         
             
                def scan_header_for_id(location_header)
         
     | 
| 
       210 
214 
     | 
    
         
             
                  location_header.scan(/.*\/#{resources}\/(.*)/)[0] if location_header
         
     | 
| 
         @@ -268,9 +272,17 @@ module CommonSense 
     | 
|
| 
       268 
272 
     | 
    
         
             
                    class_variable_set(:@@delete_url, "/#{resources}/:id.json")
         
     | 
| 
       269 
273 
     | 
    
         
             
                  end
         
     | 
| 
       270 
274 
     | 
    
         | 
| 
      
 275 
     | 
    
         
            +
                  def resources_name
         
     | 
| 
      
 276 
     | 
    
         
            +
                    class_variable_get(:@@resources)
         
     | 
| 
      
 277 
     | 
    
         
            +
                  end
         
     | 
| 
      
 278 
     | 
    
         
            +
             
     | 
| 
       271 
279 
     | 
    
         
             
                  def resource(resource)
         
     | 
| 
       272 
280 
     | 
    
         
             
                    class_variable_set(:@@resource, resource)
         
     | 
| 
       273 
281 
     | 
    
         
             
                  end
         
     | 
| 
      
 282 
     | 
    
         
            +
             
     | 
| 
      
 283 
     | 
    
         
            +
                  def resource_name
         
     | 
| 
      
 284 
     | 
    
         
            +
                    class_variable_get(:@@resource)
         
     | 
| 
      
 285 
     | 
    
         
            +
                  end
         
     | 
| 
       274 
286 
     | 
    
         
             
                end
         
     | 
| 
       275 
287 
     | 
    
         
             
              end
         
     | 
| 
       276 
288 
     | 
    
         
             
            end
         
     | 
| 
         @@ -0,0 +1,26 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module CS
         
     | 
| 
      
 2 
     | 
    
         
            +
              module EndPoint
         
     | 
| 
      
 3 
     | 
    
         
            +
                class Group
         
     | 
| 
      
 4 
     | 
    
         
            +
                  include EndPoint
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
                  attribute :name, :anonymous, :public, :hidden, :access_password, :description,
         
     | 
| 
      
 7 
     | 
    
         
            +
                    :required_sensors, :default_list_users, :default_add_users, :default_remove_users,
         
     | 
| 
      
 8 
     | 
    
         
            +
                    :default_list_sensors, :default_add_sensors, :required_show_id, :required_show_email,
         
     | 
| 
      
 9 
     | 
    
         
            +
                    :required_show_first_name, :required_show_surname, :required_show_phone_number,
         
     | 
| 
      
 10 
     | 
    
         
            +
                    :required_show_username
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  resources "groups"
         
     | 
| 
      
 13 
     | 
    
         
            +
                  resource "group"
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  def initialize(hash={})
         
     | 
| 
      
 16 
     | 
    
         
            +
                    from_hash(hash)
         
     | 
| 
      
 17 
     | 
    
         
            +
                  end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  # get groups that user belongs to
         
     | 
| 
      
 20 
     | 
    
         
            +
                  def current_groups(options={})
         
     | 
| 
      
 21 
     | 
    
         
            +
                    relation = Relation::GroupRelation.new(@session)
         
     | 
| 
      
 22 
     | 
    
         
            +
                    relation.to_a
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
                end
         
     | 
| 
      
 25 
     | 
    
         
            +
              end
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -1,14 +1,24 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            module  
     | 
| 
      
 1 
     | 
    
         
            +
            module CS
         
     | 
| 
       2 
2 
     | 
    
         
             
              module EndPoint
         
     | 
| 
       3 
3 
     | 
    
         
             
                class Sensor
         
     | 
| 
       4 
4 
     | 
    
         
             
                  include EndPoint
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
                  attribute :name, :display_name, :device_type, :pager_type, :data_type, :data_structure
         
     | 
| 
       7 
     | 
    
         
            -
                  resources  
     | 
| 
       8 
     | 
    
         
            -
                  resource  
     | 
| 
      
 7 
     | 
    
         
            +
                  resources "sensors"
         
     | 
| 
      
 8 
     | 
    
         
            +
                  resource "sensor"
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                  def initialize(hash={})
         
     | 
| 
       11 
11 
     | 
    
         
             
                    from_hash(hash)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    parse_data_structure
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  def retrieve!
         
     | 
| 
      
 16 
     | 
    
         
            +
                    super
         
     | 
| 
      
 17 
     | 
    
         
            +
                    parse_data_structure
         
     | 
| 
      
 18 
     | 
    
         
            +
                    true
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  def parse_data_structure
         
     | 
| 
       12 
22 
     | 
    
         
             
                    if self.data_type == "json"
         
     | 
| 
       13 
23 
     | 
    
         
             
                      if self.data_structure && self.data_structure.kind_of?(String)
         
     | 
| 
       14 
24 
     | 
    
         
             
                        self.data_structure = JSON.parse(self.data_structure) rescue nil
         
     | 
| 
         @@ -16,8 +26,8 @@ module CommonSense 
     | 
|
| 
       16 
26 
     | 
    
         
             
                    end
         
     | 
| 
       17 
27 
     | 
    
         
             
                  end
         
     | 
| 
       18 
28 
     | 
    
         | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
                  def  
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                  def to_cs_sensor
         
     | 
| 
       21 
31 
     | 
    
         
             
                    param = self.to_h(false)
         
     | 
| 
       22 
32 
     | 
    
         
             
                    if param[:data_type] == "json"
         
     | 
| 
       23 
33 
     | 
    
         
             
                      if param[:data_structure] && !param[:data_structure].kind_of?(String)
         
     | 
| 
         @@ -25,7 +35,13 @@ module CommonSense 
     | 
|
| 
       25 
35 
     | 
    
         
             
                      end
         
     | 
| 
       26 
36 
     | 
    
         
             
                    end
         
     | 
| 
       27 
37 
     | 
    
         | 
| 
       28 
     | 
    
         
            -
                     
     | 
| 
      
 38 
     | 
    
         
            +
                    param
         
     | 
| 
      
 39 
     | 
    
         
            +
                  end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                  # overide Endpoint#to_parameters
         
     | 
| 
      
 42 
     | 
    
         
            +
                  def to_parameters
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                    {sensor: to_cs_sensor}
         
     | 
| 
       29 
45 
     | 
    
         
             
                  end
         
     | 
| 
       30 
46 
     | 
    
         | 
| 
       31 
47 
     | 
    
         
             
                  def data
         
     | 
| 
         @@ -1,9 +1,9 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            module  
     | 
| 
      
 1 
     | 
    
         
            +
            module CS
         
     | 
| 
       2 
2 
     | 
    
         
             
              module EndPoint
         
     | 
| 
       3 
3 
     | 
    
         
             
                # This object represent a sensor data point
         
     | 
| 
       4 
4 
     | 
    
         
             
                # usage example:
         
     | 
| 
       5 
5 
     | 
    
         
             
                #
         
     | 
| 
       6 
     | 
    
         
            -
                #    client =  
     | 
| 
      
 6 
     | 
    
         
            +
                #    client = CS::Client.new
         
     | 
| 
       7 
7 
     | 
    
         
             
                #    client.login('username', 'password')
         
     | 
| 
       8 
8 
     | 
    
         
             
                #
         
     | 
| 
       9 
9 
     | 
    
         
             
                #    # Find the first position sensor
         
     | 
| 
         @@ -19,13 +19,14 @@ module CommonSense 
     | 
|
| 
       19 
19 
     | 
    
         
             
                #    sensor.data.build(date: Time.now, value: {"lux => 1}).save!
         
     | 
| 
       20 
20 
     | 
    
         
             
                #
         
     | 
| 
       21 
21 
     | 
    
         
             
                class SensorData
         
     | 
| 
       22 
     | 
    
         
            -
                  include  
     | 
| 
      
 22 
     | 
    
         
            +
                  include CS::EndPoint
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
24 
     | 
    
         
             
                  attr_accessor :month, :week, :year
         
     | 
| 
       25 
25 
     | 
    
         
             
                  attribute :date, :value, :sensor_id
         
     | 
| 
       26 
     | 
    
         
            -
                   
     | 
| 
      
 26 
     | 
    
         
            +
                  resources "data"
         
     | 
| 
      
 27 
     | 
    
         
            +
                  resource "data"
         
     | 
| 
       27 
28 
     | 
    
         | 
| 
       28 
     | 
    
         
            -
                  def  
     | 
| 
      
 29 
     | 
    
         
            +
                  def to_cs_point
         
     | 
| 
       29 
30 
     | 
    
         
             
                    param = self.to_h(false)
         
     | 
| 
       30 
31 
     | 
    
         
             
                    param.delete(:sensor_id)
         
     | 
| 
       31 
32 
     | 
    
         
             
                    value = param[:value]
         
     | 
| 
         @@ -33,7 +34,17 @@ module CommonSense 
     | 
|
| 
       33 
34 
     | 
    
         
             
                      param[:value] = value.to_json unless value.kind_of?(String) || value.kind_of?(Numeric)
         
     | 
| 
       34 
35 
     | 
    
         
             
                    end
         
     | 
| 
       35 
36 
     | 
    
         | 
| 
       36 
     | 
    
         
            -
                     
     | 
| 
      
 37 
     | 
    
         
            +
                    param[:date] = CS::Time.new(date).to_f if param[:date]
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                    param
         
     | 
| 
      
 40 
     | 
    
         
            +
                  end
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  def to_parameters
         
     | 
| 
      
 43 
     | 
    
         
            +
                    {data: [to_cs_point]}
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                  def date_human
         
     | 
| 
      
 47 
     | 
    
         
            +
                    Time.new(@date)
         
     | 
| 
       37 
48 
     | 
    
         
             
                  end
         
     | 
| 
       38 
49 
     | 
    
         | 
| 
       39 
50 
     | 
    
         
             
                  # there is no currently end point for geting data by id
         
     |