crud_inspector 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c904027e989cf4e41f00d729f1677059efb654ff
4
- data.tar.gz: 8e527e68743f255327df873e0edc631df47f909d
3
+ metadata.gz: d61ac712616d852a422de47809e13dc6febca7a3
4
+ data.tar.gz: 82f18e9d026bb1c7cceb2bbbf4762c109a8c8183
5
5
  SHA512:
6
- metadata.gz: e1c2bd60c694ab24ee91aaa44d4240c172274fe192d707b42067409238450973d9d891ec08bf975691e8ef979e859b16b74f62d14f26d6a74a24989181fd7109
7
- data.tar.gz: 3842dbc49d27a6aec9d3aa8a7db2dba0ca6a78561cb4657c8855d6e2f2d85115fdbd27b1e3012c5bbd8e8ca4a81f1d34d7bc3a73791ef5c81df2275b840fe6ba
6
+ metadata.gz: 6c7c595c79dc789f8539eb65145ec8456292037d5ce6f95162f6672758028fc0db536737cde77b14e8e12ea69db0ecd61b026cf807a65f8f6ca682d3a6744a75
7
+ data.tar.gz: 6832b2c810a2a33e3a29b6e5edabd92f7d163243a68b0007de07002d7c54b12720850097f74ad3d463a06deb49d447491521b3413cf88a08215059fa1a90df58
@@ -0,0 +1,35 @@
1
+ require 'fileutils'
2
+
3
+ module CrudInspector
4
+ module Files
5
+ class Cleaner
6
+ def initialize(repo)
7
+ @repo = repo
8
+ end
9
+
10
+ def try_clean
11
+ delete_oldest_file if file_count > 30
12
+ end
13
+
14
+ def delete_oldest_file
15
+ FileUtils.rm full_path(files.first)
16
+ end
17
+
18
+ def files
19
+ @files ||= begin
20
+ entries = Dir.entries(@repo).drop_while { |file| ['.','..'].include?(file) }
21
+ entries = entries.sort_by { |filename| File.ctime(full_path(filename)) }
22
+ entries
23
+ end
24
+ end
25
+
26
+ def file_count
27
+ files.count
28
+ end
29
+
30
+ def full_path(filename)
31
+ @repo.dup + '/' + filename
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require 'fileutils'
2
+
3
+ module CrudInspector
4
+ module Files
5
+ class Manager
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+
10
+ def perform
11
+ prepare_repo
12
+ writer = CrudInspector::Files::Writer.new(@options, repo)
13
+ case @options[:type]
14
+ when :rest
15
+ writer.extend(CrudInspector::Files::MimeTypes::Json).write
16
+ when :soap
17
+ writer.extend(CrudInspector::Files::MimeTypes::Xml).write
18
+ else
19
+ 'type not supported'
20
+ end
21
+
22
+ end
23
+
24
+ private
25
+ def repo
26
+ @repo ||= File.join(Rails.root, "crud_tracking", "services", @options[:klass].to_s.snakecase.gsub("::", "/"))
27
+ end
28
+
29
+ def prepare_repo
30
+ if Dir.exist?(repo)
31
+ CrudInspector::Files::Cleaner.new(repo).try_clean
32
+ else
33
+ FileUtils.mkdir_p(repo)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ module CrudInspector::Files
2
+ module MimeTypes
3
+ module Json
4
+ PAYLOAD = %w(args payload).freeze
5
+
6
+ def format(obj)
7
+ JSON.pretty_generate obj
8
+ rescue JSON::GeneratorError
9
+ if payload(obj).class == String && payload(obj).length > 1000
10
+ obj[PAYLOAD.first][PAYLOAD.second] = ''
11
+ end
12
+ JSON.pretty_generate obj
13
+ end
14
+
15
+ def extension
16
+ 'json'
17
+ end
18
+
19
+ def content_body
20
+ request? ? format(request_body) : format(response_body)
21
+ end
22
+
23
+ def request_body
24
+ @options[:request].as_json
25
+ end
26
+
27
+ def response_body
28
+ JSON.parse(@options[:response].dup.body.as_json)
29
+ rescue JSON::ParserError
30
+ @options[:response].body
31
+ end
32
+
33
+ def payload(obj)
34
+ obj.dig(*PAYLOAD)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ module CrudInspector::Files
2
+ module MimeTypes
3
+ module Xml
4
+ require "rexml/document"
5
+
6
+ def extension
7
+ 'xml'
8
+ end
9
+
10
+ def format(transaction)
11
+ doc = REXML::Document.new(transaction.body)
12
+ output = ""
13
+ formatter = REXML::Formatters::Pretty.new
14
+ formatter.write(doc, output)
15
+ output
16
+ end
17
+
18
+ def content_body
19
+ request? ? format(@options[:request]) : format(@options[:response])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,60 @@
1
+ module CrudInspector
2
+ module Files
3
+ class Writer
4
+ def initialize(options, repo)
5
+ @options = options
6
+ @repo = repo
7
+ end
8
+
9
+ def write
10
+ retries ||= 0
11
+ File.open(filepath, 'w') { |f| f.write(content) }
12
+ end
13
+
14
+ private
15
+ def timestamp
16
+ @timestamp ||= Time.now.strftime('%Y-%m-%d_%Hh_%Mm_%Ss')
17
+ end
18
+
19
+ def filepath
20
+ @repo + '/' + timestamp + "_" + transaction_type.to_s + "." + extension
21
+ end
22
+
23
+ def transaction_type
24
+ @options[:request].present? ? :request : :response
25
+ end
26
+
27
+ def request?
28
+ transaction_type == :request
29
+ end
30
+
31
+ def request_url
32
+ @options[:request].url
33
+ end
34
+
35
+ def response_code
36
+ @options[:response].code
37
+ end
38
+
39
+ def request_url_content
40
+ "REQUEST URL: #{request_url}"
41
+ end
42
+
43
+ def response_code_content
44
+ "RESPONSE CODE: #{response_code}"
45
+ end
46
+
47
+ def content
48
+ @content ||=
49
+ <<-CONTENT
50
+ CREATED AT: #{timestamp}
51
+ #{request? ? request_url_content : response_code_content}
52
+
53
+ #{transaction_type.to_s.upcase}:
54
+ #{content_body}
55
+
56
+ CONTENT
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,3 +1,3 @@
1
1
  module CrudInspector
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,3 +1,21 @@
1
+ require 'fileutils'
2
+
1
3
  module CrudInspector
2
- # Your code goes here...
4
+ class Tracker
5
+ def initialize(options = {})
6
+ @options = options
7
+ end
8
+
9
+ def track
10
+ return unless Rails.env.development?
11
+ case @options[:type]
12
+ when :rest
13
+ CrudInspector::Files::Manager.new(@options.merge!(type: :rest)).perform
14
+ when :soap
15
+ CrudInspector::Files::Manager.new(@options.merge!(type: :soap)).perform
16
+ else
17
+ "no type given"
18
+ end
19
+ end
20
+ end
3
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crud_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Finlayson
@@ -49,6 +49,11 @@ files:
49
49
  - README.md
50
50
  - Rakefile
51
51
  - lib/crud_inspector.rb
52
+ - lib/crud_inspector/files/cleaner.rb
53
+ - lib/crud_inspector/files/manager.rb
54
+ - lib/crud_inspector/files/mime_types/json.rb
55
+ - lib/crud_inspector/files/mime_types/xml.rb
56
+ - lib/crud_inspector/files/writer.rb
52
57
  - lib/crud_inspector/version.rb
53
58
  - lib/tasks/crud_inspector_tasks.rake
54
59
  homepage: https://github.com/bdfinlayson/crud_inspector