machineshop 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/machineshop.rb +146 -142
  3. data/lib/machineshop/api_operations/create.rb +1 -1
  4. data/lib/machineshop/api_operations/delete.rb +1 -1
  5. data/lib/machineshop/api_operations/list.rb +1 -1
  6. data/lib/machineshop/api_operations/update.rb +1 -1
  7. data/lib/machineshop/api_resource.rb +1 -1
  8. data/lib/machineshop/configuration.rb +5 -2
  9. data/lib/machineshop/data_source_types.rb +1 -1
  10. data/lib/machineshop/data_sources.rb +2 -2
  11. data/lib/machineshop/device.rb +1 -1
  12. data/lib/machineshop/device_instance.rb +1 -1
  13. data/lib/machineshop/end_points.rb +30 -0
  14. data/lib/machineshop/mapping.rb +3 -3
  15. data/lib/machineshop/models/api_endpoint.rb +8 -0
  16. data/lib/machineshop/models/api_request.rb +1 -0
  17. data/lib/machineshop/models/schema.rb +15 -0
  18. data/lib/machineshop/rule.rb +6 -22
  19. data/lib/machineshop/user.rb +3 -3
  20. data/lib/machineshop/users.rb +3 -3
  21. data/lib/machineshop/util.rb +82 -5
  22. data/lib/machineshop/utility.rb +2 -2
  23. data/lib/machineshop/version.rb +1 -1
  24. data/machineshop.gemspec +2 -6
  25. data/spec/lib/api_calls_spec.rb +628 -0
  26. data/spec/lib/custom_endpoint_test.rb +78 -0
  27. data/spec/lib/customer_spec.rb +39 -14
  28. data/spec/lib/data_source.rb +2 -0
  29. data/spec/lib/database_spec.rb +3 -1
  30. data/spec/lib/device_instances.rb +9 -1
  31. data/spec/lib/device_spec.rb +24 -0
  32. data/spec/lib/endpoint_spec.rb +37 -0
  33. data/spec/lib/{test_spec.rb → geocode_spec} +12 -14
  34. data/spec/lib/mapping_spec.rb +11 -0
  35. data/spec/lib/meter_spec.rb +8 -0
  36. data/spec/lib/report_spec.rb +6 -0
  37. data/spec/lib/rule_spec.rb +50 -26
  38. data/spec/lib/user_spec.rb +11 -0
  39. data/spec/spec_helper.rb +10 -1
  40. metadata +12 -5
  41. data/lib/machineshop/models/people.rb +0 -13
@@ -3,7 +3,7 @@ module MachineShop
3
3
  module APIOperations
4
4
  module Update
5
5
  def update(param={})
6
- response = MachineShop.put(url, @auth_token,param)
6
+ response = MachineShop.gem_put(url, @auth_token,param)
7
7
  refresh_from(response, @auth_token)
8
8
  end
9
9
  end
@@ -21,7 +21,7 @@ module MachineShop
21
21
  end
22
22
 
23
23
  def refresh
24
- response = MachineShop.get(url, @auth_token)
24
+ response = MachineShop.gem_get(url, @auth_token)
25
25
  refresh_from(response, auth_token)
26
26
  self
27
27
  end
@@ -1,11 +1,14 @@
1
1
  module MachineShop
2
2
  class Configuration
3
- attr_accessor :expiry_time, :enable_caching, :db_username, :db_password, :db_name, :db_host
3
+ attr_accessor :expiry_time, :enable_caching, :db_username, :db_password, :db_name, :db_host, :base_version, :custom_endpoints_cache_time
4
4
 
5
5
  def initialize
6
6
  #default values
7
7
  @expiry_time=0
8
- @enable_caching = true
8
+ @enable_caching = true
9
+ @base_version = "v0"
10
+ @custom_endpoints_cache_time = lambda{86400.seconds.ago}
11
+ #default 1 day
9
12
  end
10
13
  end
11
14
  end
@@ -12,7 +12,7 @@ module MachineShop
12
12
 
13
13
  def create_email_data_source(params)
14
14
  params.merge!({:data_source_type => self.id})
15
- MachineShop.post(email_data_source_url, @auth_token, params)
15
+ MachineShop.gem_post(email_data_source_url, @auth_token, params)
16
16
  end
17
17
 
18
18
  private
@@ -8,7 +8,7 @@ module MachineShop
8
8
  # Specific API calls
9
9
 
10
10
  def report_count(params)
11
- MachineShop.get(report_count_url, @auth_token, params)
11
+ MachineShop.gem_get(report_count_url, @auth_token, params)
12
12
  end
13
13
 
14
14
  def reports(filters={})
@@ -22,7 +22,7 @@ module MachineShop
22
22
  end
23
23
 
24
24
  def delete
25
- MachineShop.delete("/platform/data_sources/#{self.id}?_type=#{self._type}", @auth_token,{})
25
+ MachineShop.gem_delete("/platform/data_sources/#{self.id}?_type=#{self._type}", @auth_token,{})
26
26
  end
27
27
 
28
28
  private
@@ -6,7 +6,7 @@ module MachineShop
6
6
 
7
7
  # Specific API calls
8
8
  def payload_fields(params=nil)
9
- MachineShop.get(payload_fields_url, @auth_token, params)
9
+ MachineShop.gem_get(payload_fields_url, @auth_token, params)
10
10
  end
11
11
 
12
12
  def create_instance(params)
@@ -7,7 +7,7 @@ module MachineShop
7
7
  # Specific API calls
8
8
 
9
9
  def report_count(params)
10
- MachineShop.get(report_count_url, @auth_token, params)
10
+ MachineShop.gem_get(report_count_url, @auth_token, params)
11
11
  end
12
12
 
13
13
  def reports(filters={})
@@ -0,0 +1,30 @@
1
+ module MachineShop
2
+ class EndPoints < APIResource
3
+ include MachineShop::APIOperations::List
4
+
5
+ # Specific API calls
6
+
7
+ def self.url()
8
+ return "/gem/routes"
9
+ end
10
+
11
+ def self.all(version=v0,namespace="",auth_token)
12
+
13
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
14
+
15
+ endpoints = MachineShop.gem_get(url+"/#{version}/#{namespace}",auth_token)
16
+
17
+ endpoints=endpoints.as_json
18
+ endpoints = endpoints["#{version}"]["api"]
19
+
20
+ if Util.db_connected?
21
+ endpoints.each do |endpoint|
22
+ apiend = ApiEndpoint.find_or_initialize_by(verb: endpoint['verb'], endpoint:endpoint['endpoint'], auth_token: auth_token)
23
+ apiend.save
24
+
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,15 +2,15 @@ module MachineShop
2
2
  class Mapping
3
3
  # Specific API calls
4
4
  def self.geocode(params={}, auth_token)
5
- MachineShop.get(geocode_url, auth_token, params)
5
+ MachineShop.gem_get(geocode_url, auth_token, params)
6
6
  end
7
7
 
8
8
  def self.directions(params={}, auth_token)
9
- MachineShop.get(directions_url, auth_token, params)
9
+ MachineShop.gem_get(directions_url, auth_token, params)
10
10
  end
11
11
 
12
12
  def self.distance(params={}, auth_token)
13
- MachineShop.get(distance_url, auth_token, params)
13
+ MachineShop.gem_get(distance_url, auth_token, params)
14
14
  end
15
15
 
16
16
  private
@@ -0,0 +1,8 @@
1
+ class ApiEndpoint < ActiveRecord::Base
2
+ validates :endpoint, presence: true
3
+ validates :verb, presence: true
4
+ # validates: url, presence: true, uniqueness: true
5
+
6
+ validates_uniqueness_of :endpoint, :scope=> [:verb,:auth_token]
7
+ # validates_uniqueness_of :user_id, :scope => [:entity_id, :post_id]
8
+ end
@@ -20,6 +20,7 @@ class ApiRequest < ActiveRecord::Base
20
20
  if updated_at < cache_policy.call.utc
21
21
  update_attributes(updated_at: Time.now.utc)
22
22
  else
23
+ # puts "Not expired"
23
24
  yield
24
25
  end
25
26
  end
@@ -1,5 +1,19 @@
1
1
  ActiveRecord::Schema.define do
2
+ # puts ActiveRecord::Schema.new.migrations_paths
2
3
  self.verbose = false
4
+ # change_table(:device_caches) do |t|
5
+ # t.column :auth_token, :string, limit: 60
6
+ # end
7
+
8
+
9
+ create_table :api_endpoints do |t|
10
+ t.string :_id
11
+ t.string :verb
12
+ t.string :endpoint
13
+ t.string :auth_token
14
+ end
15
+ add_index :api_endpoints, [:verb,:endpoint], unique=>true
16
+
3
17
 
4
18
  create_table :data_source_types_caches do |t|
5
19
  t.string :_id
@@ -51,6 +65,7 @@ ActiveRecord::Schema.define do
51
65
  t.string :active
52
66
 
53
67
  end
68
+
54
69
 
55
70
 
56
71
  create_table :device_instance_caches do |t|
@@ -4,54 +4,38 @@ module MachineShop
4
4
  include MachineShop::APIOperations::Create
5
5
  include MachineShop::APIOperations::Delete
6
6
 
7
- =begin
8
- # Specific API calls
9
- def get_rules(auth_token)
10
- url = platform_url + "/rule"
11
- MachineShop.get(url,auth_token)
12
- #get url, auth_token
13
- end
14
- =end
15
-
16
7
  def self.platform_url
17
8
  '/platform'
18
9
  end
19
10
 
20
- =begin
21
- def get_rule(auth_token, id)
22
- url = platform_url + "/rule/#{id}"
23
- MachineShop.get(url, auth_token)
24
- end
25
- =end
26
-
27
11
  def self.get_join_rule_conditions(auth_token)
28
12
  url = platform_url + "/rule/join_rule_conditions"
29
- MachineShop.get(url, auth_token)
13
+ MachineShop.gem_get(url, auth_token)
30
14
  end
31
15
 
32
16
  def self.get_comparison_rule_conditions(auth_token)
33
17
  url = platform_url + "/rule/comparison_rule_conditions"
34
- MachineShop.get(url, auth_token)
18
+ MachineShop.gem_get(url, auth_token)
35
19
  end
36
20
 
37
21
  def self.get_deleted(auth_token)
38
22
  url = platform_url + "/rules/deleted"
39
- MachineShop.get(url, auth_token)
23
+ MachineShop.gem_get(url, auth_token)
40
24
  end
41
25
 
42
26
  def self.get_by_device_instance(auth_token,id)
43
27
  url = platform_url + "/rule/device_instance/#{id}"
44
- MachineShop.get(url, auth_token)
28
+ MachineShop.gem_get(url, auth_token)
45
29
  end
46
30
 
47
31
  def post_rule(auth_token, rule_hash)
48
32
  url = platform_url + "/rule"
49
- MachineShop.post(url, auth_token, rule_hash)
33
+ MachineShop.gem_post(url, auth_token, rule_hash)
50
34
  end
51
35
 
52
36
  def delete_rule(auth_token, id)
53
37
  url = platform_url + "/rule/#{id}"
54
- MachineShop.delete(url, auth_token)
38
+ MachineShop.gem_delete(url, auth_token)
55
39
  end
56
40
 
57
41
  end
@@ -4,7 +4,7 @@ module MachineShop
4
4
  # Specific API calls
5
5
  def self.authenticate(user_hash)
6
6
  #user_hash is => { email: email, password: password }
7
- response = MachineShop.post(authenticate_url, nil, user_hash)
7
+ response = MachineShop.gem_post(authenticate_url, nil, user_hash)
8
8
  auth_token = response[:authentication_token]
9
9
  id = response[:_id]
10
10
 
@@ -12,11 +12,11 @@ module MachineShop
12
12
  end
13
13
 
14
14
  def self.all_roles(auth_token)
15
- MachineShop.get(self.role_url, auth_token)
15
+ MachineShop.gem_get(self.role_url, auth_token)
16
16
  end
17
17
 
18
18
  def all_roles
19
- MachineShop.get(self.class.role_url, @auth_token)
19
+ MachineShop.gem_get(self.class.role_url, @auth_token)
20
20
  end
21
21
 
22
22
  def device_instances(filters={})
@@ -4,7 +4,7 @@ module MachineShop
4
4
  # Specific API calls
5
5
  def self.authenticate(user_hash)
6
6
  #user_hash is => { email: email, password: password }
7
- response = MachineShop.post(authenticate_url, nil, user_hash)
7
+ response = MachineShop.gem_post(authenticate_url, nil, user_hash)
8
8
  auth_token = response[:authentication_token]
9
9
  id = response[:_id]
10
10
 
@@ -12,11 +12,11 @@ module MachineShop
12
12
  end
13
13
 
14
14
  def self.all_roles(auth_token)
15
- MachineShop.get(self.role_url, auth_token)
15
+ MachineShop.gem_get(self.role_url, auth_token)
16
16
  end
17
17
 
18
18
  def all_roles
19
- MachineShop.get(self.class.role_url, @auth_token)
19
+ MachineShop.gem_get(self.class.role_url, @auth_token)
20
20
  end
21
21
 
22
22
  def device_instances(filters={})
@@ -11,7 +11,7 @@ module MachineShop
11
11
  when Array
12
12
  h.map { |v| objects_to_ids(v) }
13
13
  else
14
- h
14
+ h
15
15
  end
16
16
  end
17
17
 
@@ -34,12 +34,12 @@ module MachineShop
34
34
  when Hash
35
35
  # Try converting to a known object class. If none available, fall back to generic APIResource
36
36
  if klass_name = type
37
- klass = types[klass_name]
37
+ klass = types[klass_name]
38
38
  end
39
39
  klass ||= MachineShopObject
40
40
  klass.construct_from(resp, auth_token)
41
41
  else
42
- resp
42
+ resp
43
43
  end
44
44
  end
45
45
 
@@ -49,7 +49,7 @@ module MachineShop
49
49
  rescue
50
50
  false
51
51
  else
52
- true
52
+ true
53
53
  end
54
54
  end
55
55
 
@@ -65,7 +65,7 @@ module MachineShop
65
65
  when Array
66
66
  object.map { |value| symbolize_names(value) }
67
67
  else
68
- object
68
+ object
69
69
  end
70
70
  end
71
71
 
@@ -101,6 +101,83 @@ module MachineShop
101
101
  end
102
102
  result
103
103
  end
104
+
105
+ def self.get_klass_from_url(url)
106
+ id=nil
107
+ klass=nil
108
+ splitted = url.split('/')
109
+ klass = splitted[-1]
110
+ if /[0-9]/.match(klass)
111
+ id=splitted[-1]
112
+
113
+ if splitted[-3]=="rule"
114
+ klass="rule"
115
+ else
116
+ klass = splitted[-2]
117
+ end
118
+ end
119
+ return id,klass
120
+ end
121
+
122
+ #Check if db_connected
123
+ def self.db_connected?
124
+ db_connected = true
125
+ begin
126
+ MachineShop::Database.new
127
+ rescue DatabaseError =>e
128
+ # puts e.message
129
+ db_connected= false
130
+ rescue SchemaError =>e
131
+ # puts e.message
132
+ # db_connected=true
133
+ end
134
+
135
+ db_connected
136
+ end
137
+
138
+
139
+ def self.valid_endpoint(name,auth_token,verb,params)
140
+ if Util.db_connected?
141
+ endpoints_upto_date=false
142
+
143
+ ApiRequest.cache("/gem/routes/#{MachineShop.configuration.base_version}", auth_token, MachineShop.configuration.custom_endpoints_cache_time) do
144
+ #time not expired, cached routes are oke
145
+ endpoints_upto_date = true
146
+ end
147
+
148
+ if !endpoints_upto_date
149
+ #request the endpoints
150
+ MachineShop::EndPoints.all(MachineShop.configuration.base_version,auth_token)
151
+
152
+ end
153
+
154
+ endpoint = ApiEndpoint.where(:verb=>verb).where(:auth_token=>auth_token).where("api_endpoints.endpoint LIKE :endpoint", {:endpoint => "/#{name}%"}).take(1)
155
+ if !endpoint.empty?
156
+ splitEndpoints = (endpoint[0].endpoint).split("/").reject{|val| val=="" }
157
+ if splitEndpoints[0]==name
158
+ url="/"
159
+ key=0
160
+ splitEndpoints.each do |v|
161
+ if v.start_with? ":" #parameter
162
+ if params[key]
163
+ url+=params[key]
164
+ key+=1
165
+ else
166
+ raise APIError.new("Invalid parameters, Please provide value for #{v}")
167
+ end
168
+ else
169
+ url+="#{v}/"
170
+ end
171
+ end
172
+ end
173
+ return url
174
+ else
175
+ raise APIError.new("Invalid url request")
176
+ return false
177
+ end
178
+ end
179
+ end
180
+
104
181
  end
105
182
  end
106
183
 
@@ -1,11 +1,11 @@
1
1
  module MachineShop
2
2
  class Utility < APIResource
3
3
  def self.email(params={}, auth_token)
4
- MachineShop.post(email_url, auth_token, params)
4
+ MachineShop.gem_post(email_url, auth_token, params)
5
5
  end
6
6
 
7
7
  def self.sms(params={}, auth_token)
8
- MachineShop.post(sms_url, auth_token, params)
8
+ MachineShop.gem_post(sms_url, auth_token, params)
9
9
  end
10
10
 
11
11
  private
@@ -1,3 +1,3 @@
1
1
  module MachineShop
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
data/machineshop.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_development_dependency 'rake'
19
19
  gem.add_development_dependency 'rspec'
20
-
20
+
21
21
  # gem.add_development_dependency 'mysql'
22
22
 
23
23
  # gem.add_runtime_dependency 'mysql'
@@ -31,9 +31,5 @@ Gem::Specification.new do |gem|
31
31
  gem.required_ruby_version = '>= 1.9.3'
32
32
  gem.post_install_message = "Thanks for installing Machineshop gem, Enjoy!!"
33
33
  gem.requirements << 'mysql, activerecord'
34
-
35
-
36
- # Test for rake task database
37
- # gem.executables << 'rake'
38
- # gem.extensions << 'lib/machineshop/models/schema.rb'
34
+
39
35
  end