test_machine_shop 0.0.4

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 (73) hide show
  1. checksums.yaml +7 -0
  2. data/.idea/.name +1 -0
  3. data/.idea/.rakeTasks +7 -0
  4. data/.idea/compiler.xml +23 -0
  5. data/.idea/copyright/profiles_settings.xml +5 -0
  6. data/.idea/encodings.xml +5 -0
  7. data/.idea/inspectionProfiles/Project_Default.xml +7 -0
  8. data/.idea/inspectionProfiles/profiles_settings.xml +7 -0
  9. data/.idea/machineshop.iml +194 -0
  10. data/.idea/misc.xml +5 -0
  11. data/.idea/modules.xml +9 -0
  12. data/.idea/scopes/scope_settings.xml +5 -0
  13. data/.idea/vcs.xml +7 -0
  14. data/.idea/workspace.xml +722 -0
  15. data/Gemfile +9 -0
  16. data/LICENSE +22 -0
  17. data/README.md +977 -0
  18. data/Rakefile +8 -0
  19. data/doc.txt +50 -0
  20. data/lib/machineshop.rb +475 -0
  21. data/lib/machineshop/api_operations/create.rb +17 -0
  22. data/lib/machineshop/api_operations/delete.rb +11 -0
  23. data/lib/machineshop/api_operations/list.rb +16 -0
  24. data/lib/machineshop/api_operations/update.rb +11 -0
  25. data/lib/machineshop/api_resource.rb +35 -0
  26. data/lib/machineshop/configuration.rb +14 -0
  27. data/lib/machineshop/customer.rb +15 -0
  28. data/lib/machineshop/data_source_types.rb +28 -0
  29. data/lib/machineshop/data_sources.rb +35 -0
  30. data/lib/machineshop/database.rb +59 -0
  31. data/lib/machineshop/device.rb +30 -0
  32. data/lib/machineshop/device_instance.rb +30 -0
  33. data/lib/machineshop/end_points.rb +30 -0
  34. data/lib/machineshop/errors/api_connection_error.rb +4 -0
  35. data/lib/machineshop/errors/api_error.rb +4 -0
  36. data/lib/machineshop/errors/authentication_error.rb +4 -0
  37. data/lib/machineshop/errors/database_error.rb +5 -0
  38. data/lib/machineshop/errors/invalid_request_error.rb +10 -0
  39. data/lib/machineshop/errors/machineshop_error.rb +20 -0
  40. data/lib/machineshop/errors/schema_error.rb +5 -0
  41. data/lib/machineshop/json.rb +21 -0
  42. data/lib/machineshop/machineshop_cache.rb +49 -0
  43. data/lib/machineshop/machineshop_object.rb +165 -0
  44. data/lib/machineshop/mapping.rb +34 -0
  45. data/lib/machineshop/meter.rb +12 -0
  46. data/lib/machineshop/models/api_endpoint.rb +8 -0
  47. data/lib/machineshop/models/api_request.rb +28 -0
  48. data/lib/machineshop/models/schema.rb +184 -0
  49. data/lib/machineshop/report.rb +11 -0
  50. data/lib/machineshop/rule.rb +42 -0
  51. data/lib/machineshop/user.rb +43 -0
  52. data/lib/machineshop/users.rb +43 -0
  53. data/lib/machineshop/util.rb +193 -0
  54. data/lib/machineshop/utility.rb +30 -0
  55. data/lib/machineshop/version.rb +3 -0
  56. data/machineshop.gemspec +35 -0
  57. data/spec/lib/api_calls_spec.rb +628 -0
  58. data/spec/lib/custom_endpoint_test.rb +78 -0
  59. data/spec/lib/customer_spec.rb +87 -0
  60. data/spec/lib/data_source.rb +138 -0
  61. data/spec/lib/data_source_type_spec.rb +77 -0
  62. data/spec/lib/database_spec.rb +45 -0
  63. data/spec/lib/device_instances.rb +73 -0
  64. data/spec/lib/device_spec.rb +172 -0
  65. data/spec/lib/endpoint_spec.rb +37 -0
  66. data/spec/lib/geocode_spec +45 -0
  67. data/spec/lib/mapping_spec.rb +68 -0
  68. data/spec/lib/meter_spec.rb +49 -0
  69. data/spec/lib/report_spec.rb +52 -0
  70. data/spec/lib/rule_spec.rb +130 -0
  71. data/spec/lib/user_spec.rb +58 -0
  72. data/spec/spec_helper.rb +12 -0
  73. metadata +235 -0
@@ -0,0 +1,17 @@
1
+ #Defining class method inside a module
2
+ module MachineShop
3
+ module APIOperations
4
+ module Create
5
+ module ClassMethods
6
+ def create(params={}, auth_token=nil)
7
+ response = MachineShop.gem_post(self.url, auth_token, params)
8
+ Util.convert_to_machineshop_object(response, auth_token, self.class_name)
9
+ end
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module MachineShop
2
+ module APIOperations
3
+ module Delete
4
+ def delete
5
+ response = MachineShop.gem_delete(url, @auth_token,{})
6
+ refresh_from(response, @auth_token)
7
+ self
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module MachineShop
2
+ module APIOperations
3
+ module List
4
+ module ClassMethods
5
+ def all(filters={}, auth_token=nil)
6
+ response = MachineShop.gem_get(url, auth_token, filters)
7
+ Util.convert_to_machineshop_object(response, auth_token, self.class_name)
8
+ end
9
+ end
10
+
11
+ def self.included(base)
12
+ base.extend(ClassMethods)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ #Defining class method inside a module
2
+ module MachineShop
3
+ module APIOperations
4
+ module Update
5
+ def update(param={})
6
+ response = MachineShop.gem_put(url, @auth_token,param)
7
+ refresh_from(response, @auth_token)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ module MachineShop
2
+ class APIResource < MachineShopObject
3
+ def self.class_name
4
+ self.name.split('::')[-1]
5
+ end
6
+
7
+ def self.url()
8
+ if self == APIResource
9
+ raise NotImplementedError.new('APIResource is an abstract class. You should perform actions on its subclasses (Device, Rule, etc.)')
10
+ end
11
+ ret = "/platform/#{CGI.escape(class_name.underscore)}"
12
+ ret
13
+ end
14
+
15
+ def url
16
+ unless id = self['id']
17
+ raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
18
+ end
19
+ ret = "#{self.class.url}/#{CGI.escape(id)}"
20
+ ret
21
+ end
22
+
23
+ def refresh
24
+ response = MachineShop.gem_get(url, @auth_token)
25
+ refresh_from(response, auth_token)
26
+ self
27
+ end
28
+
29
+ def self.retrieve(id, auth_token=nil)
30
+ instance = self.new(id, auth_token)
31
+ instance.refresh
32
+ instance
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ module MachineShop
2
+ class Configuration
3
+ attr_accessor :expiry_time, :enable_caching, :db_username, :db_password, :db_name, :db_host, :base_version, :custom_endpoints_cache_time
4
+
5
+ def initialize
6
+ #default values
7
+ @expiry_time=0
8
+ @enable_caching = true
9
+ @base_version = "v0"
10
+ @custom_endpoints_cache_time = lambda{86400.seconds.ago}
11
+ #default 1 day
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module MachineShop
2
+ class Customer < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Delete
6
+ include MachineShop::APIOperations::Update
7
+
8
+
9
+ def self.update(id,auth_token,params={})
10
+ response = MachineShop.put(self.url+"/#{id}", auth_token, params)
11
+ Util.convert_to_machineshop_object(response, auth_token, self.class_name)
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,28 @@
1
+ module MachineShop
2
+ class DataSourceTypes < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Delete
6
+
7
+
8
+ def create_data_source(params)
9
+ params.merge!({:data_source_type => self.id})
10
+ DataSources.create(params, @auth_token)
11
+ end
12
+
13
+ def create_email_data_source(params)
14
+ params.merge!({:data_source_type => self.id})
15
+ MachineShop.gem_post(email_data_source_url, @auth_token, params)
16
+ end
17
+
18
+ private
19
+ def self.url
20
+ '/platform/data_source_types'
21
+ end
22
+
23
+ def email_data_source_url
24
+ '/platform/email_data_sources'
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ module MachineShop
2
+ class DataSources < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Update
6
+ # include MachineShop::APIOperations::Delete
7
+
8
+ # Specific API calls
9
+
10
+ def report_count(params)
11
+ MachineShop.gem_get(report_count_url, @auth_token, params)
12
+ end
13
+
14
+ def reports(filters={})
15
+ filters.merge!(:device_instance_id => self.id)
16
+ MachineShop::Report.all(filters, @auth_token)
17
+ end
18
+
19
+ def meters(filters={})
20
+ filters.merge!(:device_instance_id => self.id)
21
+ MachineShop::Meter.all(filters, @auth_token)
22
+ end
23
+
24
+ def delete
25
+ MachineShop.gem_delete("/platform/data_sources/#{self.id}?_type=#{self._type}", @auth_token,{})
26
+ end
27
+
28
+ private
29
+
30
+ def report_count_url
31
+ url + '/report_count'
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,59 @@
1
+ module MachineShop
2
+
3
+ require 'machineshop/errors/machineshop_error'
4
+ # require 'machineshop/errors/api_error'
5
+ require 'machineshop/errors/database_error'
6
+ require 'machineshop/errors/schema_error'
7
+ require 'mysql'
8
+ require 'active_record'
9
+ # require 'machineshop/configuration'
10
+
11
+ class Database < Configuration
12
+
13
+ attr_accessor :con, :rs,:db_connected
14
+
15
+ def initialize()
16
+
17
+ # @db_connected=true
18
+ begin
19
+ ActiveRecord::Base.establish_connection(
20
+ adapter: 'mysql', # or 'postgresql' or 'sqlite3'
21
+ host: MachineShop.configuration.db_host,
22
+ database: MachineShop.configuration.db_name,
23
+ username: MachineShop.configuration.db_username,
24
+ password: MachineShop.configuration.db_password,
25
+ )
26
+
27
+ begin
28
+ load 'machineshop/models/schema.rb'
29
+ rescue Mysql::Error => e
30
+ # raise SchemaError.new(e.message)
31
+ rescue StandardError => e
32
+ # raise SchemaError.new(e.message)
33
+ end
34
+
35
+
36
+ rescue Mysql::Error =>e
37
+ raise DatabaseError.new("Mysql error ")
38
+ rescue StandardError =>e
39
+ raise DatabaseError.new("Database Error #1")
40
+ rescue Exception => e
41
+ # @db_connected=false
42
+ raise DatabaseError.new("Connection to Database refused")
43
+ # load schema file
44
+ end
45
+
46
+
47
+
48
+
49
+
50
+ end
51
+
52
+ def self.serialize_data(data)
53
+ Marshal.dump(data)
54
+ end
55
+
56
+ end
57
+
58
+
59
+ end
@@ -0,0 +1,30 @@
1
+ module MachineShop
2
+ class Device < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Delete
6
+
7
+ # Specific API calls
8
+ def payload_fields(params=nil)
9
+ MachineShop.gem_get(payload_fields_url, @auth_token, params)
10
+ end
11
+
12
+ def create_instance(params)
13
+ params.merge!({:device_id => self.id})
14
+ DeviceInstance.create(params, @auth_token)
15
+ end
16
+
17
+ def instances(params={})
18
+ params.merge!({:device_id => self.id})
19
+ DeviceInstance.all(params, @auth_token)
20
+ end
21
+
22
+ private
23
+
24
+ def payload_fields_url
25
+ url + '/payload_fields'
26
+ end
27
+
28
+
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ module MachineShop
2
+ class DeviceInstance < APIResource
3
+ include MachineShop::APIOperations::List
4
+ include MachineShop::APIOperations::Create
5
+ include MachineShop::APIOperations::Delete
6
+
7
+ # Specific API calls
8
+
9
+ def report_count(params)
10
+ MachineShop.gem_get(report_count_url, @auth_token, params)
11
+ end
12
+
13
+ def reports(filters={})
14
+ filters.merge!(:device_instance_id => self.id)
15
+ MachineShop::Report.all(filters, @auth_token)
16
+ end
17
+
18
+ def meters(filters={})
19
+ filters.merge!(:device_instance_id => self.id)
20
+ MachineShop::Meter.all(filters, @auth_token)
21
+ end
22
+
23
+ private
24
+
25
+ def report_count_url
26
+ url + '/report_count'
27
+ end
28
+
29
+ end
30
+ end
@@ -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
@@ -0,0 +1,4 @@
1
+ module MachineShop
2
+ class APIConnectionError < MachineShopError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MachineShop
2
+ class APIError < MachineShopError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module MachineShop
2
+ class AuthenticationError < MachineShopError
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module MachineShop
2
+ class DatabaseError < MachineShopError
3
+
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module MachineShop
2
+ class InvalidRequestError < MachineShopError
3
+ attr_accessor :param
4
+
5
+ def initialize(message, param, http_status=nil, http_body=nil, json_body=nil)
6
+ super(message, http_status, http_body, json_body)
7
+ @param = param
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module MachineShop
2
+ class MachineShopError < StandardError
3
+ attr_reader :message
4
+ attr_reader :http_status
5
+ attr_reader :http_body
6
+ attr_reader :json_body
7
+
8
+ def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil)
9
+ @message = message
10
+ @http_status = http_status
11
+ @http_body = http_body
12
+ @json_body = json_body
13
+ end
14
+
15
+ def to_s
16
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
17
+ "#{status_string}#{@message}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module MachineShop
2
+ class SchemaError < MachineShopError
3
+
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ module MachineShop
2
+ module JSON
3
+ if MultiJson.respond_to?(:dump)
4
+ def self.dump(*args)
5
+ MultiJson.dump(*args)
6
+ end
7
+
8
+ def self.load(*args)
9
+ MultiJson.load(*args)
10
+ end
11
+ else
12
+ def self.dump(*args)
13
+ MultiJson.encode(*args)
14
+ end
15
+
16
+ def self.load(*args)
17
+ MultiJson.decode(*args)
18
+ end
19
+ end
20
+ end
21
+ end