test_machine_shop 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/compiler.xml +23 -0
- data/.idea/copyright/profiles_settings.xml +5 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/inspectionProfiles/Project_Default.xml +7 -0
- data/.idea/inspectionProfiles/profiles_settings.xml +7 -0
- data/.idea/machineshop.iml +194 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +7 -0
- data/.idea/workspace.xml +722 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +977 -0
- data/Rakefile +8 -0
- data/doc.txt +50 -0
- data/lib/machineshop.rb +475 -0
- data/lib/machineshop/api_operations/create.rb +17 -0
- data/lib/machineshop/api_operations/delete.rb +11 -0
- data/lib/machineshop/api_operations/list.rb +16 -0
- data/lib/machineshop/api_operations/update.rb +11 -0
- data/lib/machineshop/api_resource.rb +35 -0
- data/lib/machineshop/configuration.rb +14 -0
- data/lib/machineshop/customer.rb +15 -0
- data/lib/machineshop/data_source_types.rb +28 -0
- data/lib/machineshop/data_sources.rb +35 -0
- data/lib/machineshop/database.rb +59 -0
- data/lib/machineshop/device.rb +30 -0
- data/lib/machineshop/device_instance.rb +30 -0
- data/lib/machineshop/end_points.rb +30 -0
- data/lib/machineshop/errors/api_connection_error.rb +4 -0
- data/lib/machineshop/errors/api_error.rb +4 -0
- data/lib/machineshop/errors/authentication_error.rb +4 -0
- data/lib/machineshop/errors/database_error.rb +5 -0
- data/lib/machineshop/errors/invalid_request_error.rb +10 -0
- data/lib/machineshop/errors/machineshop_error.rb +20 -0
- data/lib/machineshop/errors/schema_error.rb +5 -0
- data/lib/machineshop/json.rb +21 -0
- data/lib/machineshop/machineshop_cache.rb +49 -0
- data/lib/machineshop/machineshop_object.rb +165 -0
- data/lib/machineshop/mapping.rb +34 -0
- data/lib/machineshop/meter.rb +12 -0
- data/lib/machineshop/models/api_endpoint.rb +8 -0
- data/lib/machineshop/models/api_request.rb +28 -0
- data/lib/machineshop/models/schema.rb +184 -0
- data/lib/machineshop/report.rb +11 -0
- data/lib/machineshop/rule.rb +42 -0
- data/lib/machineshop/user.rb +43 -0
- data/lib/machineshop/users.rb +43 -0
- data/lib/machineshop/util.rb +193 -0
- data/lib/machineshop/utility.rb +30 -0
- data/lib/machineshop/version.rb +3 -0
- data/machineshop.gemspec +35 -0
- data/spec/lib/api_calls_spec.rb +628 -0
- data/spec/lib/custom_endpoint_test.rb +78 -0
- data/spec/lib/customer_spec.rb +87 -0
- data/spec/lib/data_source.rb +138 -0
- data/spec/lib/data_source_type_spec.rb +77 -0
- data/spec/lib/database_spec.rb +45 -0
- data/spec/lib/device_instances.rb +73 -0
- data/spec/lib/device_spec.rb +172 -0
- data/spec/lib/endpoint_spec.rb +37 -0
- data/spec/lib/geocode_spec +45 -0
- data/spec/lib/mapping_spec.rb +68 -0
- data/spec/lib/meter_spec.rb +49 -0
- data/spec/lib/report_spec.rb +52 -0
- data/spec/lib/rule_spec.rb +130 -0
- data/spec/lib/user_spec.rb +58 -0
- data/spec/spec_helper.rb +12 -0
- metadata +235 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
module MachineShop
|
2
|
+
class MachineshopCache
|
3
|
+
#require 'Rails'
|
4
|
+
#require 'active_support/cache'
|
5
|
+
require 'active_record'
|
6
|
+
cache = ActiveSupport::Cache.lookup_cache(:memory_store)
|
7
|
+
|
8
|
+
Rails.cache.class.class_eval do
|
9
|
+
attr_accessor :enabled
|
10
|
+
|
11
|
+
def read_with_enabled(*args, &block)
|
12
|
+
enabled ? read_without_enabled( *args, &block) : nil
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method_chain :read, :enabled
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
if File.basename($0) == "rake" && ARGV.include?("db:migrate")
|
20
|
+
Rails.cache.enabled = false
|
21
|
+
|
22
|
+
else
|
23
|
+
Rails.cache.enabled = true
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "Rails.cache" do
|
29
|
+
it 'can be disabled and enabled' do
|
30
|
+
Rails.cache.enabled = true
|
31
|
+
Rails.cache.enabled.should be_true
|
32
|
+
Rails.cache.write("foobar","foo")
|
33
|
+
Rails.cache.read("foobar").should == "foo"
|
34
|
+
|
35
|
+
Rails.cache.enabled = false
|
36
|
+
Rails.cache.enabled.should be_false
|
37
|
+
Rails.cache.write("foobar","foo")
|
38
|
+
Rails.cache.read("foobar").shoule be_nil
|
39
|
+
|
40
|
+
Rails.cache.enabled = true
|
41
|
+
Rails.cache.read("foobar").should == "foo"
|
42
|
+
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
module MachineShop
|
2
|
+
class MachineShopObject
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_accessor :auth_token
|
6
|
+
@@permanent_attributes = Set.new([:auth_token, :id])
|
7
|
+
|
8
|
+
# The default :id method is deprecated and isn't useful to us
|
9
|
+
if method_defined?(:id)
|
10
|
+
undef :id
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(id=nil, auth_token=nil)
|
14
|
+
# parameter overloading!
|
15
|
+
if id.kind_of?(Hash)
|
16
|
+
@retrieve_options = id.dup
|
17
|
+
@retrieve_options.delete(:id)
|
18
|
+
id = id[:id] ? id[:id] : id[:_id]
|
19
|
+
else
|
20
|
+
@retrieve_options = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
@auth_token = auth_token
|
24
|
+
|
25
|
+
@values = {}
|
26
|
+
# This really belongs in APIResource, but not putting it there allows us
|
27
|
+
# to have a unified inspect method
|
28
|
+
@unsaved_values = Set.new
|
29
|
+
@transient_values = Set.new
|
30
|
+
self.id = id if id
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.construct_from(values, auth_token=nil)
|
34
|
+
values[:id] = values[:_id] if values[:_id]
|
35
|
+
obj = self.new(values[:id], auth_token)
|
36
|
+
obj.refresh_from(values, auth_token)
|
37
|
+
obj
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s(*args)
|
41
|
+
MachineShop::JSON.dump(@values, :pretty => true)
|
42
|
+
end
|
43
|
+
|
44
|
+
def inspect()
|
45
|
+
id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
|
46
|
+
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + MachineShop::JSON.dump(@values, :pretty => true)
|
47
|
+
end
|
48
|
+
|
49
|
+
def refresh_from(values, auth_token, partial=false)
|
50
|
+
@auth_token = auth_token
|
51
|
+
case values
|
52
|
+
when Array
|
53
|
+
values = values[0]
|
54
|
+
else
|
55
|
+
#do nothing
|
56
|
+
end
|
57
|
+
values[:id] = values[:_id] if values[:_id]
|
58
|
+
|
59
|
+
removed = partial ? Set.new : Set.new(@values.keys - values.keys)
|
60
|
+
added = Set.new(values.keys - @values.keys)
|
61
|
+
|
62
|
+
instance_eval do
|
63
|
+
remove_accessors(removed)
|
64
|
+
add_accessors(added)
|
65
|
+
end
|
66
|
+
removed.each do |k|
|
67
|
+
@values.delete(k)
|
68
|
+
@transient_values.add(k)
|
69
|
+
@unsaved_values.delete(k)
|
70
|
+
end
|
71
|
+
values.each do |k, v|
|
72
|
+
@values[k] = Util.convert_to_machineshop_object(v, auth_token)
|
73
|
+
@transient_values.delete(k)
|
74
|
+
@unsaved_values.delete(k)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def [](k)
|
79
|
+
k = k.to_sym if k.kind_of?(String)
|
80
|
+
@values[k]
|
81
|
+
end
|
82
|
+
|
83
|
+
def []=(k, v)
|
84
|
+
send(:"#{k}=", v)
|
85
|
+
end
|
86
|
+
|
87
|
+
def keys
|
88
|
+
@values.keys
|
89
|
+
end
|
90
|
+
|
91
|
+
def values
|
92
|
+
@values.values
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_json(*a)
|
96
|
+
MachineShop::JSON.dump(@values)
|
97
|
+
end
|
98
|
+
|
99
|
+
def as_json(*a)
|
100
|
+
@values.as_json(*a)
|
101
|
+
end
|
102
|
+
|
103
|
+
def to_hash
|
104
|
+
@values
|
105
|
+
end
|
106
|
+
|
107
|
+
def each(&blk)
|
108
|
+
@values.each(&blk)
|
109
|
+
end
|
110
|
+
|
111
|
+
protected
|
112
|
+
|
113
|
+
def metaclass
|
114
|
+
class << self; self; end
|
115
|
+
end
|
116
|
+
|
117
|
+
def remove_accessors(keys)
|
118
|
+
metaclass.instance_eval do
|
119
|
+
keys.each do |k|
|
120
|
+
next if @@permanent_attributes.include?(k)
|
121
|
+
k_eq = :"#{k}="
|
122
|
+
remove_method(k) if method_defined?(k)
|
123
|
+
remove_method(k_eq) if method_defined?(k_eq)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def add_accessors(keys)
|
129
|
+
metaclass.instance_eval do
|
130
|
+
keys.each do |k|
|
131
|
+
next if @@permanent_attributes.include?(k)
|
132
|
+
k_eq = :"#{k}="
|
133
|
+
define_method(k) { @values[k] }
|
134
|
+
define_method(k_eq) do |v|
|
135
|
+
@values[k] = v
|
136
|
+
@unsaved_values.add(k)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def method_missing(name, *args)
|
143
|
+
# TODO: only allow setting in updateable classes.
|
144
|
+
if name.to_s.end_with?('=')
|
145
|
+
attr = name.to_s[0...-1].to_sym
|
146
|
+
@values[attr] = args[0]
|
147
|
+
@unsaved_values.add(attr)
|
148
|
+
add_accessors([attr])
|
149
|
+
return
|
150
|
+
else
|
151
|
+
return @values[name] if @values.has_key?(name)
|
152
|
+
end
|
153
|
+
|
154
|
+
begin
|
155
|
+
super
|
156
|
+
rescue NoMethodError => e
|
157
|
+
if @transient_values.include?(name)
|
158
|
+
raise NoMethodError.new(e.message + ". HINT: The '#{name}' attribute was set in the past, however. It was then wiped when refreshing the object with the result returned by MachineShop's API, probably as a result of a save(). The attributes currently available on this object are: #{@values.keys.join(', ')}")
|
159
|
+
else
|
160
|
+
raise
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module MachineShop
|
2
|
+
class Mapping
|
3
|
+
# Specific API calls
|
4
|
+
def self.geocode(params={}, auth_token)
|
5
|
+
MachineShop.gem_get(geocode_url, auth_token, params)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.directions(params={}, auth_token)
|
9
|
+
MachineShop.gem_get(directions_url, auth_token, params)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.distance(params={}, auth_token)
|
13
|
+
MachineShop.gem_get(distance_url, auth_token, params)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.geocode_url
|
19
|
+
url + '/geocode/json'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.directions_url
|
23
|
+
url + '/directions/json'
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.distance_url
|
27
|
+
url + '/distance/json'
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.url
|
31
|
+
'/platform/google'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -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
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
class ApiRequest < ActiveRecord::Base
|
3
|
+
validates :url, presence: true, uniqueness: true
|
4
|
+
# validates: url, presence: true, uniqueness: true
|
5
|
+
|
6
|
+
def self.cache(url,auth_token, cache_policy)
|
7
|
+
find_or_initialize_by(url: url, auth_token:auth_token).cache(cache_policy) do
|
8
|
+
if block_given?
|
9
|
+
yield
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def cache(cache_policy)
|
16
|
+
if new_record?
|
17
|
+
update_attributes(updated_at: Time.now.utc)
|
18
|
+
end
|
19
|
+
|
20
|
+
if updated_at < cache_policy.call.utc
|
21
|
+
update_attributes(updated_at: Time.now.utc)
|
22
|
+
else
|
23
|
+
# puts "Not expired"
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
# puts ActiveRecord::Schema.new.migrations_paths
|
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
|
+
|
17
|
+
|
18
|
+
create_table :data_source_types_caches do |t|
|
19
|
+
t.string :_id
|
20
|
+
t.string :active
|
21
|
+
t.string :type
|
22
|
+
t.string :exe_path
|
23
|
+
t.string :name
|
24
|
+
t.string :image_url
|
25
|
+
t.string :init_cmd
|
26
|
+
t.string :init_params
|
27
|
+
t.string :last_known_translator_port
|
28
|
+
t.string :long_description
|
29
|
+
t.string :user_id
|
30
|
+
t.string :manual_url
|
31
|
+
t.string :manufacturer
|
32
|
+
t.string :updated_at
|
33
|
+
t.string :created_at
|
34
|
+
t.string :deleted_at
|
35
|
+
t.string :model
|
36
|
+
t.string :payload
|
37
|
+
t.string :rule_ids
|
38
|
+
t.string :sample_data
|
39
|
+
t.string :software
|
40
|
+
t.string :translator
|
41
|
+
t.string :unit_price
|
42
|
+
t.string :auth_token
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
create_table :data_sources_caches do |t|
|
47
|
+
t.string :_id
|
48
|
+
t.string :_type
|
49
|
+
t.string :type
|
50
|
+
t.string :last_checked_datetime
|
51
|
+
t.string :name
|
52
|
+
t.string :user_name
|
53
|
+
t.string :sender
|
54
|
+
t.string :encrypted_password
|
55
|
+
t.string :pop_server
|
56
|
+
t.string :port
|
57
|
+
t.string :user_id
|
58
|
+
t.string :auth_token
|
59
|
+
t.string :data_source_type_id
|
60
|
+
t.string :updated_at
|
61
|
+
t.string :created_at
|
62
|
+
t.text :data_source_type
|
63
|
+
t.string :last_report
|
64
|
+
t.string :report_count
|
65
|
+
t.string :active
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
create_table :device_instance_caches do |t|
|
72
|
+
t.string :_id
|
73
|
+
t.string :alert_count
|
74
|
+
t.string :name
|
75
|
+
t.string :active
|
76
|
+
t.string :device_id
|
77
|
+
t.string :user_id
|
78
|
+
t.string :auth_token
|
79
|
+
t.string :updated_at
|
80
|
+
t.string :created_at
|
81
|
+
t.string :_type
|
82
|
+
t.text :device
|
83
|
+
t.text :last_report
|
84
|
+
t.string :report_count
|
85
|
+
t.string :tag_ids
|
86
|
+
t.string :rule_ids
|
87
|
+
t.string :user_type
|
88
|
+
t.string :deleted_at
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
create_table :api_requests do |t|
|
93
|
+
t.timestamps null: false
|
94
|
+
t.text :url, null: false
|
95
|
+
t.string :auth_token, null:false
|
96
|
+
end
|
97
|
+
|
98
|
+
create_table :report_caches do |t|
|
99
|
+
t.string :_id
|
100
|
+
t.string :created_at
|
101
|
+
t.string :deleted_at
|
102
|
+
t.string :device_datetime
|
103
|
+
t.string :device_instance_id
|
104
|
+
t.string :duplicate
|
105
|
+
t.text :payload
|
106
|
+
t.string :profile_timestamps
|
107
|
+
t.string :raw_data
|
108
|
+
t.string :report_processor
|
109
|
+
t.string :stale
|
110
|
+
t.string :updated_at
|
111
|
+
t.string :auth_token
|
112
|
+
end
|
113
|
+
|
114
|
+
create_table :comparison_rule_conditions_caches do |t|
|
115
|
+
t.string :auth_token
|
116
|
+
t.string :rule_condition
|
117
|
+
t.string :rule_description
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
create_table :join_rule_conditions_caches do |t|
|
122
|
+
t.string :rule_condition
|
123
|
+
t.string :rule_description
|
124
|
+
t.timestamp
|
125
|
+
end
|
126
|
+
|
127
|
+
create_table :rule_caches do |t|
|
128
|
+
t.string :_id
|
129
|
+
t.string :active
|
130
|
+
t.string :created_at
|
131
|
+
t.string :deleted_at
|
132
|
+
t.string :description
|
133
|
+
t.string :device_ids
|
134
|
+
t.string :device_instance_ids
|
135
|
+
t.string :downstream_rule_id
|
136
|
+
t.string :last_run_status
|
137
|
+
t.string :plain_english
|
138
|
+
t.string :tag_ids
|
139
|
+
t.string :then_actions
|
140
|
+
t.string :updated_at
|
141
|
+
t.string :user_id
|
142
|
+
t.string :auth_token
|
143
|
+
t.string :actions
|
144
|
+
t.string :comparison_value
|
145
|
+
t.string :deleted
|
146
|
+
t.string :device_attribute
|
147
|
+
t.string :last_run
|
148
|
+
t.string :modified_date
|
149
|
+
t.string :operator
|
150
|
+
t.string :rule_histories
|
151
|
+
t.string :else_actions
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
create_table :device_caches do |t|
|
156
|
+
t.string :_id
|
157
|
+
t.string :active
|
158
|
+
t.string :created_at
|
159
|
+
t.string :deleted_at
|
160
|
+
t.string :exe_path
|
161
|
+
t.string :image_url
|
162
|
+
t.string :init_cmd
|
163
|
+
t.string :init_params
|
164
|
+
t.string :last_known_translator_port
|
165
|
+
t.string :long_description
|
166
|
+
t.string :manual_url
|
167
|
+
t.string :manufacturer
|
168
|
+
t.string :model
|
169
|
+
t.string :name
|
170
|
+
t.string :rule_ids
|
171
|
+
t.string :sample_data
|
172
|
+
t.string :software
|
173
|
+
t.string :tag_ids
|
174
|
+
t.string :translator
|
175
|
+
t.string :type
|
176
|
+
t.string :unit_price
|
177
|
+
t.string :updated_at
|
178
|
+
t.string :user_id
|
179
|
+
t.string :auth_token
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
end
|