vt_api 0.1.2 → 0.1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,59 +1,59 @@
1
- module VtApi
2
- module ApiV2
3
- # VT API 2.0 for comments
4
- module Comments
5
- # Representation of a comment
6
- class Comment
7
- attr_reader :text, :date_token, :date
8
-
9
- # Creates new Comment object from API data.
10
- # It is not recommended to use this directly.
11
- # Refer to API methods instead.
12
- #
13
- # @see Comments.get
14
- # @see Comments.put
15
- #
16
- # @param [String] date_token Date token as present in API (can be used in <code>Comments.get</code>).
17
- # @param [String] text Comment text
18
- def initialize(date_token, text)
19
- @date_token = date_token
20
- @date = DateTime.parse date_token
21
- @text = text
22
- end
23
- end
24
-
25
- # @see https://developers.virustotal.com/v2.0/reference#comments-get
26
- # @see Comment#date_token
27
- #
28
- # @param [String] resource
29
- # @param [String] before Must be in format of <code>date_token</code>. Refer to API docs for more info.
30
- # @return [Array<Comment>] Array of parsed comments.
31
- def self.get(resource:, before:)
32
- resp = ApiV2.provider.request 'comments.get', apikey: VtApi.options.token, resource: resource, before: before
33
-
34
- parse_comments resp
35
- end
36
-
37
- # @see https://developers.virustotal.com/v2.0/reference#comments-put
38
- #
39
- # @param [String] resource
40
- # @param [String] text Comment text string.
41
- # @return [Boolean] True if comment put successfully ('response_code' equals 1), false otherwise.
42
- def self.put(resource:, text:)
43
- resp = ApiV2.provider.request 'comments.put', apikey: VtApi.options.token, resource: resource, text: text
44
-
45
- resp.response_code == 1
46
- end
47
-
48
- private
49
-
50
- def self.parse_comments(api_resp)
51
- if api_resp.response_code.nil? or api_resp.response_code != 1
52
- []
53
- else
54
- api_resp.comments.map {|comment| Comment.new comment.date, comment.text}
55
- end
56
- end
57
- end
58
- end
1
+ module VtApi
2
+ module ApiV2
3
+ # VT API 2.0 for comments
4
+ module Comments
5
+ # Representation of a comment
6
+ class Comment
7
+ attr_reader :text, :date_token, :date
8
+
9
+ # Creates new Comment object from API data.
10
+ # It is not recommended to use this directly.
11
+ # Refer to API methods instead.
12
+ #
13
+ # @see Comments.get
14
+ # @see Comments.put
15
+ #
16
+ # @param [String] date_token Date token as present in API (can be used in <code>Comments.get</code>).
17
+ # @param [String] text Comment text
18
+ def initialize(date_token, text)
19
+ @date_token = date_token
20
+ @date = DateTime.parse date_token
21
+ @text = text
22
+ end
23
+ end
24
+
25
+ # @see https://developers.virustotal.com/v2.0/reference#comments-get
26
+ # @see Comment#date_token
27
+ #
28
+ # @param [String] resource
29
+ # @param [String] before Must be in format of <code>date_token</code>. Refer to API docs for more info.
30
+ # @return [Array<Comment>] Array of parsed comments.
31
+ def self.get(resource:, before:)
32
+ resp = ApiV2.provider.request 'comments.get', apikey: VtApi.options.token, resource: resource, before: before
33
+
34
+ parse_comments resp
35
+ end
36
+
37
+ # @see https://developers.virustotal.com/v2.0/reference#comments-put
38
+ #
39
+ # @param [String] resource
40
+ # @param [String] text Comment text string.
41
+ # @return [Boolean] True if comment put successfully ('response_code' equals 1), false otherwise.
42
+ def self.put(resource:, text:)
43
+ resp = ApiV2.provider.request 'comments.put', apikey: VtApi.options.token, resource: resource, text: text
44
+
45
+ resp.response_code == 1
46
+ end
47
+
48
+ private
49
+
50
+ def self.parse_comments(api_resp)
51
+ if api_resp.response_code.nil? or api_resp.response_code != 1
52
+ []
53
+ else
54
+ api_resp.comments.map {|comment| Comment.new comment.date, comment.text}
55
+ end
56
+ end
57
+ end
58
+ end
59
59
  end
@@ -1,156 +1,156 @@
1
- require 'date'
2
- require 'pathname'
3
-
4
- require 'faraday/upload_io'
5
- require 'mimemagic'
6
-
7
- require_relative '../config'
8
- require_relative '../../internal/api_provider'
9
- require_relative '../v2'
10
-
11
- module VtApi
12
- module ApiV2
13
- # Class that represents VT Public API 2.0 for files.
14
- class File
15
-
16
- # Representation of particular AV result on file.
17
- class AvResult
18
- attr_reader :av_name, :triggered, :version, :threat_name, :update
19
-
20
- # noinspection RubyResolve
21
- def initialize(av_name, scan_obj)
22
- @av_name = av_name
23
- @triggered = scan_obj.detected
24
- @version = scan_obj.version
25
- @threat_name = scan_obj.result
26
- @update = scan_obj.update
27
- end
28
- end
29
-
30
- # Existing ID types
31
- # @see #id
32
- ID_TYPES = %w(scan_id resource_id md5 sha1 sha256)
33
-
34
- class << self
35
- # @see https://developers.virustotal.com/v2.0/reference#file-report
36
- #
37
- # @param [String] resource
38
- # @return [File] File report if present, <code>nil</code> otherwise.
39
- def report(resource:)
40
- resp = ApiV2.provider.request 'file.report', apikey: VtApi.options.token, resource: resource
41
- from_response resp
42
- end
43
-
44
- # @see https://developers.virustotal.com/v2.0/reference#file-rescan
45
- #
46
- # @param [String] resource
47
- # @return [String] Scan ID.
48
- def schedule_rescan(resource:)
49
- resp = ApiV2.provider.request 'file.rescan', apikey: VtApi.options.token, resource: resource
50
- resp.scan_id
51
- end
52
-
53
- # @see https://developers.virustotal.com/v2.0/reference#file-scan
54
- #
55
- # @param [String|Pathname|UploadIO] file
56
- # @return [String] Scan ID.
57
- def schedule_scan(file:)
58
- if file.kind_of? String or file.kind_of? Pathname
59
- file = file_to_io file
60
- end
61
-
62
- resp = ApiV2.provider.request 'file.scan', apikey: VtApi.options.token, file: file
63
- resp.scan_id
64
- end
65
-
66
- private
67
-
68
- def file_to_io(file)
69
- file = ::File.realpath file
70
- file = ::File.absolute_path file
71
-
72
- mime = MimeMagic.by_path file
73
-
74
- UploadIO.new file, mime
75
- end
76
- end
77
-
78
- # Shorthand for #initialize.
79
- #
80
- # @see #initialize
81
- def self.from_response(api_resp)
82
- if api_resp.response_code.nil? or api_resp.response_code < 1
83
- nil
84
- else
85
- report = self.new api_resp
86
-
87
- report
88
- end
89
- end
90
-
91
- attr_reader :scan_date, :permalink, :scans, :scan_count, :trigger_count
92
-
93
- # Initializes new object from VT API response.
94
- # @note Direct creation of object cas cause errors since it doesn't contain any validity checks. Use predefined API method bindings instead.
95
- #
96
- # @see .report
97
- # @see .schedule_scan
98
- # @see .schedule_rescan
99
- #
100
- # @param [OpenStruct] api_resp
101
- def initialize(api_resp)
102
- load_ids! api_resp
103
- load_meta! api_resp
104
- load_scans! api_resp
105
- end
106
-
107
- # @return [Fixnum]
108
- def threat_level
109
- @trigger_count.to_f / @scan_count
110
- end
111
-
112
- # Get file identifier.
113
- # Since VT API offers many ways to identify the file, you can supply ID type to get specific file.
114
- #
115
- # @see ID_TYPES
116
- #
117
- # @param [Symbol|String] type
118
- # @return [String] ID string of specified type.
119
- def id(type = :id)
120
- unless ID_TYPES.include? type.to_sym
121
- raise ArgumentError, "There is no such id type (#{type}) in VT API 2.0"
122
- end
123
-
124
- @ids[type.to_sym]
125
- end
126
-
127
- private
128
-
129
- # noinspection RubyResolve
130
- def load_ids!(api_resp)
131
- @ids = OpenStruct.new ({
132
- scan_id: api_resp.scan_id,
133
- md5: api_resp.md5,
134
- sha1: api_resp.sha1,
135
- sha256: api_resp.sha256,
136
- resource_id: api_resp.resource
137
- })
138
- end
139
-
140
- def load_meta!(api_resp)
141
- @scan_date = DateTime.parse(api_resp.scan_date)
142
- @permalink = URI(api_resp.permalink)
143
- end
144
-
145
- # noinspection RubyResolve
146
- def load_scans!(api_resp)
147
- @scan_count = api_resp.total
148
- @trigger_count = api_resp.positives
149
-
150
- @scans = api_resp.scans.to_h.to_a.map do |key, value|
151
- [key, AvResult.new(key.to_s, value)]
152
- end.to_h
153
- end
154
- end
155
- end
1
+ require 'date'
2
+ require 'pathname'
3
+
4
+ require 'faraday/upload_io'
5
+ require 'mimemagic'
6
+
7
+ require_relative '../config'
8
+ require_relative '../../internal/api_provider'
9
+ require_relative '../v2'
10
+
11
+ module VtApi
12
+ module ApiV2
13
+ # Class that represents VT Public API 2.0 for files.
14
+ class File
15
+
16
+ # Representation of particular AV result on file.
17
+ class AvResult
18
+ attr_reader :av_name, :triggered, :version, :threat_name, :update
19
+
20
+ # noinspection RubyResolve
21
+ def initialize(av_name, scan_obj)
22
+ @av_name = av_name
23
+ @triggered = scan_obj.detected
24
+ @version = scan_obj.version
25
+ @threat_name = scan_obj.result
26
+ @update = scan_obj.update
27
+ end
28
+ end
29
+
30
+ # Existing ID types
31
+ # @see #id
32
+ ID_TYPES = %w(scan_id resource_id md5 sha1 sha256)
33
+
34
+ class << self
35
+ # @see https://developers.virustotal.com/v2.0/reference#file-report
36
+ #
37
+ # @param [String] resource
38
+ # @return [File] File report if present, <code>nil</code> otherwise.
39
+ def report(resource:)
40
+ resp = ApiV2.provider.request 'file.report', apikey: VtApi.options.token, resource: resource
41
+ from_response resp
42
+ end
43
+
44
+ # @see https://developers.virustotal.com/v2.0/reference#file-rescan
45
+ #
46
+ # @param [String] resource
47
+ # @return [String] Scan ID.
48
+ def schedule_rescan(resource:)
49
+ resp = ApiV2.provider.request 'file.rescan', apikey: VtApi.options.token, resource: resource
50
+ resp.scan_id
51
+ end
52
+
53
+ # @see https://developers.virustotal.com/v2.0/reference#file-scan
54
+ #
55
+ # @param [String|Pathname|UploadIO] file
56
+ # @return [String] Scan ID.
57
+ def schedule_scan(file:)
58
+ if file.kind_of? String or file.kind_of? Pathname
59
+ file = file_to_io file
60
+ end
61
+
62
+ resp = ApiV2.provider.request 'file.scan', apikey: VtApi.options.token, file: file
63
+ resp.scan_id
64
+ end
65
+
66
+ private
67
+
68
+ def file_to_io(file)
69
+ file = ::File.realpath file
70
+ file = ::File.absolute_path file
71
+
72
+ mime = MimeMagic.by_path file
73
+
74
+ UploadIO.new file, mime
75
+ end
76
+ end
77
+
78
+ # Shorthand for #initialize.
79
+ #
80
+ # @see #initialize
81
+ def self.from_response(api_resp)
82
+ if api_resp.response_code.nil? or api_resp.response_code < 1
83
+ nil
84
+ else
85
+ report = self.new api_resp
86
+
87
+ report
88
+ end
89
+ end
90
+
91
+ attr_reader :scan_date, :permalink, :scans, :scan_count, :trigger_count
92
+
93
+ # Initializes new object from VT API response.
94
+ # @note Direct creation of object cas cause errors since it doesn't contain any validity checks. Use predefined API method bindings instead.
95
+ #
96
+ # @see .report
97
+ # @see .schedule_scan
98
+ # @see .schedule_rescan
99
+ #
100
+ # @param [OpenStruct] api_resp
101
+ def initialize(api_resp)
102
+ load_ids! api_resp
103
+ load_meta! api_resp
104
+ load_scans! api_resp
105
+ end
106
+
107
+ # @return [Fixnum]
108
+ def threat_level
109
+ @trigger_count.to_f / @scan_count
110
+ end
111
+
112
+ # Get file identifier.
113
+ # Since VT API offers many ways to identify the file, you can supply ID type to get specific file.
114
+ #
115
+ # @see ID_TYPES
116
+ #
117
+ # @param [Symbol|String] type
118
+ # @return [String] ID string of specified type.
119
+ def id(type = :id)
120
+ unless ID_TYPES.include? type.to_sym
121
+ raise ArgumentError, "There is no such id type (#{type}) in VT API 2.0"
122
+ end
123
+
124
+ @ids[type.to_sym]
125
+ end
126
+
127
+ private
128
+
129
+ # noinspection RubyResolve
130
+ def load_ids!(api_resp)
131
+ @ids = OpenStruct.new ({
132
+ scan_id: api_resp.scan_id,
133
+ md5: api_resp.md5,
134
+ sha1: api_resp.sha1,
135
+ sha256: api_resp.sha256,
136
+ resource_id: api_resp.resource
137
+ })
138
+ end
139
+
140
+ def load_meta!(api_resp)
141
+ @scan_date = DateTime.parse(api_resp.scan_date)
142
+ @permalink = URI(api_resp.permalink)
143
+ end
144
+
145
+ # noinspection RubyResolve
146
+ def load_scans!(api_resp)
147
+ @scan_count = api_resp.total
148
+ @trigger_count = api_resp.positives
149
+
150
+ @scans = api_resp.scans.to_h.to_a.map do |key, value|
151
+ [key, AvResult.new(key.to_s, value)]
152
+ end.to_h
153
+ end
154
+ end
155
+ end
156
156
  end
@@ -1,78 +1,78 @@
1
- module VtApi
2
- module ApiV2
3
- # Class that represents URL scan report.
4
- class URL
5
- class << self
6
-
7
- # @see https://developers.virustotal.com/v2.0/reference#url-report
8
- #
9
- # @param [String] resource
10
- # @param [Boolean] scan Shedule URL scan if it is not present in system.
11
- # @return [URL] URL report object is present, <code>nil</code> otherwise.
12
- def report(resource:, scan: false)
13
- resp = ApiV2.provider.request 'url.report', apikey: VtApi.options.token, resource: resource, schedule_scan: (scan ? 1 : 0)
14
- pp resp
15
- from_response resp
16
- end
17
-
18
- # @see https://developers.virustotal.com/v2.0/reference#url-scan
19
- #
20
- # @param [String] url
21
- # @return [String] Scheduled scan ID.
22
- def schedule_scan(url:)
23
- resp = ApiV2.provider.request 'url.report', apikey: VtApi.options.token, url: url
24
- resp.scan_id
25
- end
26
-
27
- end
28
-
29
- # Shorthand for #initialize.
30
- #
31
- # @see #initialize
32
- def self.from_response(api_resp)
33
- if api_resp.response_code.nil? or api_resp.response_code < 1
34
- nil
35
- else
36
- report = self.new api_resp
37
- report
38
- end
39
- end
40
-
41
- attr_reader :id, :url, :scan_date, :permalink, :filescan_id
42
-
43
- # Initializes new object from VT API response.
44
- # @note Direct creation of object cas cause errors since it doesn't contain any validity checks. Use predefined API method bindings instead.
45
- #
46
- # @see .report
47
- # @see .schedule_scan
48
- #
49
- # @param [OpenStruct] api_resp
50
- def initialize(api_resp)
51
- load_id!(api_resp)
52
- load_meta!(api_resp)
53
- load_scans!(api_resp)
54
- end
55
-
56
- private
57
-
58
- # noinspection RubyResolve
59
- def load_scans!(api_resp)
60
- @scan_count = api_resp.total
61
- @trigger_count = api_resp.positives
62
- @scans = api_resp.scans
63
- end
64
-
65
- def load_meta!(api_resp)
66
- @scan_date = api_resp.scan_date
67
- @permalink = api_resp.permalink
68
- @filescan_id = api_resp.filescan_id
69
- end
70
-
71
- # noinspection RubyResolve
72
- def load_id!(api_resp)
73
- @id = api_resp.scan_id
74
- @url = api_resp.url
75
- end
76
- end
77
- end
1
+ module VtApi
2
+ module ApiV2
3
+ # Class that represents URL scan report.
4
+ class URL
5
+ class << self
6
+
7
+ # @see https://developers.virustotal.com/v2.0/reference#url-report
8
+ #
9
+ # @param [String] resource
10
+ # @param [Boolean] scan Shedule URL scan if it is not present in system.
11
+ # @return [URL] URL report object is present, <code>nil</code> otherwise.
12
+ def report(resource:, scan: false)
13
+ resp = ApiV2.provider.request 'url.report', apikey: VtApi.options.token, resource: resource, schedule_scan: (scan ? 1 : 0)
14
+ pp resp
15
+ from_response resp
16
+ end
17
+
18
+ # @see https://developers.virustotal.com/v2.0/reference#url-scan
19
+ #
20
+ # @param [String] url
21
+ # @return [String] Scheduled scan ID.
22
+ def schedule_scan(url:)
23
+ resp = ApiV2.provider.request 'url.report', apikey: VtApi.options.token, url: url
24
+ resp.scan_id
25
+ end
26
+
27
+ end
28
+
29
+ # Shorthand for #initialize.
30
+ #
31
+ # @see #initialize
32
+ def self.from_response(api_resp)
33
+ if api_resp.response_code.nil? or api_resp.response_code < 1
34
+ nil
35
+ else
36
+ report = self.new api_resp
37
+ report
38
+ end
39
+ end
40
+
41
+ attr_reader :id, :url, :scan_date, :permalink, :filescan_id
42
+
43
+ # Initializes new object from VT API response.
44
+ # @note Direct creation of object cas cause errors since it doesn't contain any validity checks. Use predefined API method bindings instead.
45
+ #
46
+ # @see .report
47
+ # @see .schedule_scan
48
+ #
49
+ # @param [OpenStruct] api_resp
50
+ def initialize(api_resp)
51
+ load_id!(api_resp)
52
+ load_meta!(api_resp)
53
+ load_scans!(api_resp)
54
+ end
55
+
56
+ private
57
+
58
+ # noinspection RubyResolve
59
+ def load_scans!(api_resp)
60
+ @scan_count = api_resp.total
61
+ @trigger_count = api_resp.positives
62
+ @scans = api_resp.scans
63
+ end
64
+
65
+ def load_meta!(api_resp)
66
+ @scan_date = api_resp.scan_date
67
+ @permalink = api_resp.permalink
68
+ @filescan_id = api_resp.filescan_id
69
+ end
70
+
71
+ # noinspection RubyResolve
72
+ def load_id!(api_resp)
73
+ @id = api_resp.scan_id
74
+ @url = api_resp.url
75
+ end
76
+ end
77
+ end
78
78
  end
data/lib/vt_api/api/v2.rb CHANGED
@@ -1,15 +1,16 @@
1
- require_relative 'config'
2
- require_relative 'v2/file'
3
- require_relative 'v2/url'
4
-
5
- module VtApi
6
- module ApiV2
7
-
8
- # Get ApiProvider singleton configured for VT Public API 2.0.
9
- #
10
- # @return [ApiProvider]
11
- def self.provider
12
- @provider ||= ApiProvider.new Versions::API_V2, VtApi.options.adapter
13
- end
14
- end
1
+ require_relative 'config'
2
+ require_relative 'v2/file'
3
+ require_relative 'v2/url'
4
+ require_relative 'v2/comments'
5
+
6
+ module VtApi
7
+ module ApiV2
8
+
9
+ # Get ApiProvider singleton configured for VT Public API 2.0.
10
+ #
11
+ # @return [ApiProvider]
12
+ def self.provider
13
+ @provider ||= ApiProvider.new Versions::API_V2, VtApi.options.adapter
14
+ end
15
+ end
15
16
  end
data/lib/vt_api/api.rb CHANGED
@@ -1,2 +1,2 @@
1
- require_relative 'api/config'
2
- require_relative 'api/v2'
1
+ require_relative 'api/config'
2
+ require_relative 'api/v2'