housing_misc 0.5.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.
@@ -0,0 +1,21 @@
1
+ require 'openssl'
2
+ KEY_LENGTH = 24
3
+ DEFAULT_KEY = "LQFWYPGCCDXMEKIYOLCH"
4
+ ENCRYPTION_ALGO = 'DES-EDE3-CBC'
5
+
6
+ class String
7
+ def encrypt(key=DEFAULT_KEY)
8
+ cipher = OpenSSL::Cipher.new(ENCRYPTION_ALGO).encrypt
9
+ cipher.key = (Digest::SHA1.hexdigest key)[0...KEY_LENGTH]
10
+ s = cipher.update(self) + cipher.final
11
+ s.unpack('H*').first
12
+ end
13
+
14
+ def decrypt(key=DEFAULT_KEY)
15
+ cipher = OpenSSL::Cipher.new(ENCRYPTION_ALGO).decrypt
16
+ cipher.key = (Digest::SHA1.hexdigest key)[0...KEY_LENGTH]
17
+ s = [self].pack("H*").unpack("C*").pack("c*")
18
+ cipher.update(s) + cipher.final
19
+ end
20
+
21
+ end
@@ -0,0 +1,10 @@
1
+ module HousingMisc
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace HousingMisc
4
+ require "multi_json"
5
+ #add this in config/routes.rb of service
6
+ # Rails.application.routes.draw do
7
+ # mount HousingMisc::Engine, at: "/housing_misc"
8
+ # end
9
+ end
10
+ end
@@ -0,0 +1,44 @@
1
+ module ActionController
2
+ class Base
3
+ def render *args
4
+ #this will override all render calls in all services so edit with CAUTION
5
+ options = args.extract_options!
6
+ param = (MultiJson.load(params['slice_keys']) rescue false)
7
+ args << transform_options(param,options)
8
+ super *args
9
+ end
10
+
11
+ private
12
+
13
+ def transform_options(param,options)
14
+ if options[:json] and param
15
+ if options[:json].is_a?(String)
16
+ options[:json] = MultiJson.load(options[:json]) rescue options[:json]
17
+ slice_keys = options[:json].is_a?(Hash) or options[:json].is_a?(Array)
18
+ end
19
+ if options[:json].is_a?(Hash)
20
+ valid_json = options[:json]
21
+ slice_keys = true
22
+ elsif options[:json].is_a?(Array)
23
+ valid_json = {:hits => options[:json]}
24
+ param = {"hits"=>Array.wrap(param)}
25
+ extract_hits = true
26
+ slice_keys = true
27
+ end
28
+ if slice_keys
29
+ temp_options = valid_json.deep_slice(*Array.wrap(param))
30
+ options[:json] = temp_options if temp_options.present?
31
+ if extract_hits
32
+ options[:json] = options[:json]["hits"]
33
+ end
34
+ end
35
+ end
36
+ if options[:json].is_a?(Hash) or options[:json].is_a?(Array)
37
+ #will use MultiJson.dump for ALL api calls to decrease api render time in general
38
+ options[:json] = MultiJson.dump(options[:json])
39
+ end
40
+ options
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,111 @@
1
+ module ModelsReflectionsHelper
2
+ VAL_HASH_KEYS = ["macros", "throughs", "foreign_keys", "primary_keys", "polymorphic", "as", "type", "join_table", "association_foreign_key"]
3
+ def tables_reflections_hash_generator(base_models)
4
+ @base_models = base_models
5
+ @tables_reflections_hash = {}
6
+ loop do
7
+ break if @base_models.empty?
8
+ model = @base_models.shift
9
+ table, model_reflections = get_model_reflections(model)
10
+ @tables_reflections_hash.store(table, model_reflections)
11
+ end
12
+ return @tables_reflections_hash
13
+ end
14
+
15
+ def get_model_reflections(model)
16
+ val_hash = {}
17
+ model_reflections = {}
18
+ reflections = model.constantize.reflections
19
+ model_table_name = model.constantize.table_name
20
+ reflections.keys.each do |reflection|
21
+ next if reflections[reflection].options[:polymorphic]
22
+ # models_foreign_type = reflections[reflection].foreign_type
23
+ # sql = "SELECT DISTINCT #{models_foreign_type} FROM #{model.constantize.table_name}"
24
+ # rows_of_model = ActiveRecord::Base.connection.exec_query(sql).rows
25
+ # model_names = convert_rows_to_col(rows_of_model)
26
+ # val_hash["table_name"] = "#{reflection}_polymorphic"
27
+ # val_hash["table_names"], val_hash["primary_keys"], val_hash["model_names"] = get_table_names_primary_keys_and_model_names(model_names, model)
28
+ # val_hash["polymorphic"] = ["TRUE"]
29
+ # val_hash["foreign_type"] = [models_foreign_type]
30
+ # else
31
+ model_name = reflections[reflection].class_name.constantize rescue ("#{model}::#{reflections[reflection].class_name}".constantize rescue "")
32
+ table_name = model_name.table_name rescue ""
33
+ next if table_name.blank?
34
+ val_hash["table_name"] = table_name
35
+ val_hash["throughs"] = [reflections[reflection].options[:through] || "FALSE"]
36
+ val_hash["polymorphic"] = ["FALSE"]
37
+ val_hash["as"] = [reflections[reflection].options[:as] || "FALSE"]
38
+ val_hash["type"] = [reflections[reflection].type || "FALSE"]
39
+ val_hash["join_table"] = reflections[reflection].macro == :has_and_belongs_to_many ? [reflections[reflection].options[:join_table] || get_join_table_name(model_table_name, table_name)] : ["FALSE"]
40
+ val_hash["association_foreign_key"] = reflections[reflection].macro == :has_and_belongs_to_many ? [reflections[reflection].options[:association_foreign_key] || reflections[reflection].association_foreign_key] : ["FALSE"]
41
+ val_hash["primary_keys"] = [reflections[reflection].options[:primary_key] || get_primary_key(reflections[reflection].macro, model_name, model)]
42
+ val_hash["model_name"] = model_name.to_s
43
+ update_base_models(val_hash["model_name"], model)
44
+ #end
45
+ val_hash["macros"] = [reflections[reflection].macro]
46
+ val_hash["foreign_keys"] = [reflections[reflection].foreign_key]
47
+ if model_reflections.key?(val_hash["table_name"])
48
+ VAL_HASH_KEYS.each do |val_hash_key|
49
+ model_reflections[val_hash["table_name"]][val_hash_key].concat(val_hash[val_hash_key])
50
+ end
51
+ else
52
+ model_reflections[val_hash["table_name"]] = val_hash
53
+ end
54
+ val_hash = {}
55
+ end
56
+ model_reflections["primary_key"] = model.constantize.primary_key
57
+ model_reflections["model"] = model
58
+ return model_table_name, model_reflections
59
+ end
60
+
61
+ def get_table_names_primary_keys_and_model_names(model_names, model)
62
+ table_names = []
63
+ primary_keys = []
64
+ table_models = []
65
+ model_names.each do |model_name|
66
+ table_name = model_name.constantize.table_name rescue ("#{model}::#{model_name}".constantize.table_name rescue "")
67
+ next if table_name.blank?
68
+ table_names.push(table_name)
69
+ table_models.push(model_name)
70
+ primary_keys.push(model_name.constantize.primary_key)
71
+ update_base_models(model_name, model)
72
+ end
73
+ return table_names, primary_keys, table_models
74
+ end
75
+
76
+ def convert_rows_to_col(array_of_rows)
77
+ column = array_of_rows.map{|row| row[0]}
78
+ return column
79
+ end
80
+
81
+ def get_primary_key(macro, reflection_model_name, model_name)
82
+ primary_key = ""
83
+ case macro
84
+ when :belongs_to, :has_and_belongs_to_many
85
+ primary_key = reflection_model_name.primary_key
86
+ when :has_one, :has_many
87
+ primary_key = model_name.constantize.primary_key
88
+ end
89
+ return primary_key
90
+ end
91
+
92
+ def update_base_models(model_name, model)
93
+ if !@tables_reflections_hash.key?(model_name.constantize.table_name) && (model_name != model) && !@base_models.include?(model_name)
94
+ @base_models.push(model_name)
95
+ end
96
+ end
97
+
98
+ def get_join_table_name(table1_name, table2_name)
99
+ table_name = ""
100
+ table1_name.length.times do |i|
101
+ next if table1_name[i]==table2_name[i] && table1_name[i] != "_"
102
+ if table1_name[i]==table2_name[i] && table1_name[i] == "_"
103
+ table_name = table1_name[0,i+1] + (table1_name[i+1, table1_name.length]<table2_name[i+1, table2_name.length] ? (table1_name[i+1, table1_name.length] + "_" + table2_name[i+1, table2_name.length]) : (table2_name[i+1, table2_name.length] + "_" + table1_name[i+1, table1_name.length]))
104
+ else
105
+ table_name = table1_name<table2_name ? (table1_name + "_" + table2_name) : (table2_name + "_" + table1_name)
106
+ end
107
+ break
108
+ end
109
+ return table_name
110
+ end
111
+ end
@@ -0,0 +1,14 @@
1
+ require 'net/http'
2
+ =begin
3
+ Generic method to modify all http requests being made using net/http
4
+ adds app-name header configured by the app
5
+ adds X-Request-ID header that gets sets by RequestTracer middleware
6
+ =end
7
+ class Net::HTTPGenericRequest
8
+ alias_method :old_initialize, :initialize
9
+ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
10
+ old_initialize(m, reqbody, resbody, uri_or_path, initheader)
11
+ self['app-name'] = HousingMisc.app_name
12
+ self['X-Request-ID'] = Thread.current[:global_request_id]
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require 'rack/cors'
2
+ module HousingMisc
3
+ class Railtie < Rails::Railtie
4
+
5
+ # middleware for support of CORS for housing subdomains
6
+ initializer "housing_misc.insert_middleware" do |app|
7
+ rails_version = Gem.loaded_specs["rails"].version
8
+ rails_5 = Gem::Version.create('5.0')
9
+ rack_cors = rails_version >= rails_5 ? Rack::Cors : "Rack::Cors"
10
+ app.config.middleware.insert_before 0, rack_cors do
11
+ allow do
12
+ origins { |source, env| env["HTTP_ORIGIN"].match?(Regexp.new (HousingMisc.origin || '\A^https?:\/\/([a-zA-Z0-9\-\.]+[.])*housing.com\z')) }
13
+ resource '*', :headers => :any, :credentials=> eval(HousingMisc.credentials_flag.to_s) || true, :methods => [:get, :post, :put, :delete, :options], expose: HousingMisc.expose_headers || "", max_age: HousingMisc.max_age || 86400
14
+ end
15
+ end
16
+ end
17
+
18
+ # middleware for introducing global request id
19
+ # not required once we move to rails 5 or beyond
20
+ initializer "housing_misc.insert_request_tracer_middleware" do |app|
21
+ app.config.middleware.insert_after ActionDispatch::RequestId, RequestTracer
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ def find_orig_and_swap_key(key)
2
+ keys = key.split(",")
3
+ return keys[0], keys[1] if keys.size > 1
4
+ return key, key
5
+ end
6
+
7
+ Hash.class_eval do
8
+ def deep_slice(*allowed_keys)
9
+ sliced = {}
10
+ allowed_keys.each do |allowed_key|
11
+ if allowed_key.is_a?(Hash)
12
+ allowed_key.each do |allowed_subkey, allowed_subkey_values|
13
+ allowed_subkey_org , allowed_subkey_to_replace = find_orig_and_swap_key(allowed_subkey)
14
+ if (has_key?(allowed_subkey_org) | has_key?(allowed_subkey_org.to_sym))
15
+ value = has_key?(allowed_subkey_org) ? self[allowed_subkey_org] : self[allowed_subkey_org.to_sym]
16
+ if value.is_a?(Hash)
17
+ sliced[allowed_subkey_to_replace] = value.deep_slice(*Array.wrap(allowed_subkey_values))
18
+ elsif value.is_a?(Array)
19
+ temp_array_slice = []
20
+ value.each do |val|
21
+ temp_array_slice << val.deep_slice(*Array.wrap(allowed_subkey_values))
22
+ end
23
+ sliced[allowed_subkey_to_replace] = temp_array_slice
24
+ else
25
+ raise ArgumentError, "can only deep-slice hash values, but value for #{allowed_subkey.inspect} was of type #{value.class.name}"
26
+ end
27
+ end
28
+ end
29
+ else
30
+ Array.wrap(allowed_key).each do |k|
31
+ k_org, k_to_replace = find_orig_and_swap_key(k)
32
+ if has_key?(k_org)
33
+ sliced[k_to_replace] = self[k_org]
34
+ elsif has_key?(k_org.to_sym)
35
+ sliced[k_to_replace] = self[k_org.to_sym]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ return sliced
41
+ end
42
+
43
+ end
@@ -0,0 +1,3 @@
1
+ module HousingMisc
2
+ VERSION = '0.5.4'
3
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: housing_misc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.4
5
+ platform: ruby
6
+ authors:
7
+ - Abdul Qadir
8
+ - Supantha Samanta
9
+ - Shubham Kukreja
10
+ - Mohit Ojha
11
+ - Rakesh Kumar
12
+ - Siddhant Jain
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+ date: 2020-03-03 00:00:00.000000000 Z
17
+ dependencies:
18
+ - !ruby/object:Gem::Dependency
19
+ name: rails
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 4.0.2
25
+ - - "<="
26
+ - !ruby/object:Gem::Version
27
+ version: 5.1.0
28
+ type: :runtime
29
+ prerelease: false
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 4.0.2
35
+ - - "<="
36
+ - !ruby/object:Gem::Version
37
+ version: 5.1.0
38
+ - !ruby/object:Gem::Dependency
39
+ name: sprockets
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '2.8'
45
+ - - "<"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.8'
55
+ - - "<"
56
+ - !ruby/object:Gem::Version
57
+ version: '4.0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: newrelic_rpm
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 6.8.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 6.8.0
72
+ - !ruby/object:Gem::Dependency
73
+ name: rack-cors
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ - !ruby/object:Gem::Dependency
87
+ name: oj
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ - !ruby/object:Gem::Dependency
101
+ name: multi_json
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ - !ruby/object:Gem::Dependency
115
+ name: rspec
116
+ requirement: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ - !ruby/object:Gem::Dependency
129
+ name: aerospike-store
130
+ requirement: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 0.1.0
135
+ type: :runtime
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 0.1.0
142
+ description: Gem for miscellaneous general requirements
143
+ email:
144
+ - abdul.qadir@housing.com
145
+ - supantha.samanta@housing.com
146
+ - shubham.kukreja@proptiger.com
147
+ - mohit.ojha@housing.com
148
+ - rakesh.kumar@housing.com
149
+ executables: []
150
+ extensions: []
151
+ extra_rdoc_files: []
152
+ files:
153
+ - README.md
154
+ - app/controllers/housing_misc/application_controller.rb
155
+ - app/controllers/housing_misc/cache_controller.rb
156
+ - app/controllers/housing_misc/logs_controller.rb
157
+ - app/controllers/housing_misc/model_reflections_controller.rb
158
+ - app/controllers/housing_misc/sidekiq_queue_check_controller.rb
159
+ - app/middleware/request_tracer.rb
160
+ - config/routes.rb
161
+ - lib/helpers/configuration.rb
162
+ - lib/housing_misc.rb
163
+ - lib/housing_misc/aerospike_cache_extension.rb
164
+ - lib/housing_misc/api_helper.rb
165
+ - lib/housing_misc/boolean_evaluator.rb
166
+ - lib/housing_misc/diff_checker.rb
167
+ - lib/housing_misc/distance_unit.rb
168
+ - lib/housing_misc/encrypt_decrypt.rb
169
+ - lib/housing_misc/engine.rb
170
+ - lib/housing_misc/json_slice_render.rb
171
+ - lib/housing_misc/models_reflections_helper.rb
172
+ - lib/housing_misc/net_http_generic_request.rb
173
+ - lib/housing_misc/railtie.rb
174
+ - lib/housing_misc/slice_helper.rb
175
+ - lib/housing_misc/version.rb
176
+ homepage: https://github.com/loconsolutions/housing.misc.gem
177
+ licenses:
178
+ - MIT
179
+ metadata: {}
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ required_rubygems_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ requirements: []
195
+ rubyforge_project:
196
+ rubygems_version: 2.7.6
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: Miscellaneous Gem
200
+ test_files: []