Module: CS::Relation

Included in:
GroupRelation, NotificationRelation, SensorDataRelation, SensorRelation, SensorTriggersRelation, TriggerRelation, UserRelation
Defined in:
lib/cs/relation.rb,
lib/cs/relation/user_relation.rb,
lib/cs/relation/group_relation.rb,
lib/cs/relation/sensor_relation.rb,
lib/cs/relation/trigger_relation.rb,
lib/cs/relation/sensor_data_relation.rb,
lib/cs/relation/notification_relation.rb

Defined Under Namespace

Modules: ClassMethod Classes: GroupRelation, NotificationRelation, SensorDataRelation, SensorRelation, SensorTriggersRelation, TriggerRelation, UserRelation

Instance Method Summary collapse

Instance Method Details

#allObject



51
52
53
# File 'lib/cs/relation.rb', line 51

def all
  self.to_a
end

#build(attributes = {}) ⇒ Object

Create a new Endpoint object.

example:

sensor = client.sensors.build


22
23
24
25
26
# File 'lib/cs/relation.rb', line 22

def build(attributes={})
  resource = resource_class.new(attributes)
  resource.session = self.session
  resource
end

#check_session!Object



5
6
7
# File 'lib/cs/relation.rb', line 5

def check_session!
  raise Error::SessionEmptyError unless @session
end

#countObject



55
56
57
58
59
# File 'lib/cs/relation.rb', line 55

def count
  check_session!
  resource = get_single_resource
  resource["total"] if resource
end

#each(&block) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cs/relation.rb', line 156

def each(&block)
  counter = 0
  self.each_batch do |data|
    data.each do |data_point|
      resource = resource_class.new(data_point)
      resource.session = session
      yield resource
      counter += 1

      return if @limit && @limit == counter
    end
  end
end

#each_batch(params = {}, &block) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cs/relation.rb', line 138

def each_batch(params={}, &block)
  check_session!
  options = get_options(params)

  self.page ||= 0;

  begin
    options[:page] = self.page
    data = get_data(options)

    data = data[resource_class.resources_name] unless data.nil?
    if !data.nil? && !data.empty?
      yield data
      self.page += 1
    end
  end while data && data.size == self.per_page
end

#find_or_create(attribute) ⇒ Object



87
88
89
# File 'lib/cs/relation.rb', line 87

def find_or_create(attribute)
  find_or_create!(attribute) rescue nil
end

#find_or_create!(attribute) ⇒ Object



81
82
83
84
85
# File 'lib/cs/relation.rb', line 81

def find_or_create!(attribute)
  resource = find_or_new(attribute)
  resource.save! if resource.id.nil?
  resource
end

#find_or_new(attribute) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cs/relation.rb', line 61

def find_or_new(attribute)
  check_session!

  self.each do |resource|
    found = true
    attribute.each do |key, value|
      if resource.parameter(key) != value
        found = false
        break
      end
    end

    return resource if found
  end

  resource = resource_class.new(attribute)
  resource.session = self.session
  resource
end

#firstObject



91
92
93
94
# File 'lib/cs/relation.rb', line 91

def first
  resource = get_single_resource
  parse_single_resource(resource)
end

#get_data(params = {}) ⇒ Object



47
48
49
# File 'lib/cs/relation.rb', line 47

def get_data(params={})
  get_data!(params)
end

#get_data!(params = {}) ⇒ Object



28
29
30
31
32
# File 'lib/cs/relation.rb', line 28

def get_data!(params={})
  check_session!
  options = get_options(params)
  session.get(get_url, options)
end

#get_options(input = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cs/relation.rb', line 108

def get_options(input={})
  options = {}

  self.class.parameters.each do |name, param_option|
    value = self.parameter(name) # get value from object
    value = input[name] if input.has_key?(name) # override

    value = process_param(name, value, param_option)
    value = value.to_f if value.kind_of?(Time)

    options[name] = value if value
  end

  options
end

#get_urlObject



9
10
11
# File 'lib/cs/relation.rb', line 9

def get_url
  raise Error::NotImplementedError, "the class #{self.class} does not respond to 'get_url' "
end

#initialize(session = nil) ⇒ Object



13
14
15
# File 'lib/cs/relation.rb', line 13

def initialize(session=nil)
  @session = session
end

#inspectObject



101
102
103
104
105
106
# File 'lib/cs/relation.rb', line 101

def inspect
  entries = to_a.take(11).map!(&:inspect)
  entries[10] = '...' if entries.size == 11

  "#<#{self.class.name} [#{entries.join(", \n")}]>"
end

#lastObject



96
97
98
99
# File 'lib/cs/relation.rb', line 96

def last
  resource = get_single_resource(page: count - 1)
  parse_single_resource(resource)
end

#limit(num) ⇒ Object



34
35
36
37
# File 'lib/cs/relation.rb', line 34

def limit(num)
  @limit = num
  return self
end

#parameter(name) ⇒ Object



39
40
41
# File 'lib/cs/relation.rb', line 39

def parameter(name)
  self.instance_variable_get("@#{name}")
end

#parametersObject



43
44
45
# File 'lib/cs/relation.rb', line 43

def parameters
  self.class.parameters
end

#where(params = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cs/relation.rb', line 124

def where(params={})
  process_alias!(params)
  params.keep_if {|k| self.class.parameters[k]}

  params.each do |name,value|
    param_option = self.class.parameters[name]

    value = process_param(name, value, param_option)
    self.send("#{name}=", value)
  end

  self
end