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,65 +1,65 @@
1
- require 'faraday'
2
-
3
- require 'json'
4
- require 'ostruct'
5
-
6
- require_relative 'versions/default'
7
- require_relative '../../vt_api'
8
-
9
- module VtApi
10
- # API intercation class.
11
- #
12
- # Uses [Faraday] for HTTP interaction.
13
- class ApiProvider
14
-
15
- # Create new ApiProvider.
16
- #
17
- # @param [ApiVersion] api_version API interface to use.
18
- # @param [Object] adapter Faraday HTTP adapter.
19
- # @return [Object]
20
- def initialize(api_version = Versions::DEFAULT, adapter = :net_http)
21
- unless api_version.kind_of? ApiVersion
22
- raise ArgumentError, "Invalid API interface supplied! Must be subclass of 'ApiVersion', got #{api_version.class} instead"
23
- end
24
-
25
- @api = api_version
26
- @connection = Faraday.new @api.base_url do |conn|
27
- conn.request :multipart
28
- conn.request :url_encoded
29
-
30
- conn.adapter adapter
31
- end
32
- end
33
-
34
- # Performs a request to given API method.
35
- # Requested method must be described in API interface.
36
- #
37
- # @raise [UndefinedMethodError]
38
- # @raise [MissingParametersError]
39
- # @raise [ApiError]
40
- #
41
- # @param [String] method_name
42
- # @param [Hash] params
43
- # @return [OpenStruct] Method result object.
44
- def request(method_name, params = {})
45
- unless @api.endpoint? method_name
46
- raise UndefinedMethodError, "Endpoint '#{method_name}' not found in '#{@api.version}' API interface."
47
- end
48
-
49
- endpoint = @api.endpoint method_name
50
-
51
- unless endpoint.params_valid? (params)
52
- raise MissingParametersError, "Missed parameters #{endpoint.missing_params(params).to_s} required by endpoint '#{method_name}'."
53
- end
54
-
55
- result = @connection.public_send(endpoint.method, endpoint.uri, params, {})
56
-
57
- if @api.error? result.status
58
- raise ApiError, "#{@api.version} error: #{@api.error(result.status)}"
59
- end
60
-
61
- # noinspection RubyResolve
62
- JSON.parse result.body, object_class: OpenStruct
63
- end
64
- end
1
+ require 'faraday'
2
+
3
+ require 'json'
4
+ require 'ostruct'
5
+
6
+ require_relative 'versions/default'
7
+ require_relative '../../vt_api'
8
+
9
+ module VtApi
10
+ # API intercation class.
11
+ #
12
+ # Uses [Faraday] for HTTP interaction.
13
+ class ApiProvider
14
+
15
+ # Create new ApiProvider.
16
+ #
17
+ # @param [ApiVersion] api_version API interface to use.
18
+ # @param [Object] adapter Faraday HTTP adapter.
19
+ # @return [Object]
20
+ def initialize(api_version = Versions::DEFAULT, adapter = :net_http)
21
+ unless api_version.kind_of? ApiVersion
22
+ raise ArgumentError, "Invalid API interface supplied! Must be subclass of 'ApiVersion', got #{api_version.class} instead"
23
+ end
24
+
25
+ @api = api_version
26
+ @connection = Faraday.new @api.base_url do |conn|
27
+ conn.request :multipart
28
+ conn.request :url_encoded
29
+
30
+ conn.adapter adapter
31
+ end
32
+ end
33
+
34
+ # Performs a request to given API method.
35
+ # Requested method must be described in API interface.
36
+ #
37
+ # @raise [UndefinedMethodError]
38
+ # @raise [MissingParametersError]
39
+ # @raise [ApiError]
40
+ #
41
+ # @param [String] method_name
42
+ # @param [Hash] params
43
+ # @return [OpenStruct] Method result object.
44
+ def request(method_name, params = {})
45
+ unless @api.endpoint? method_name
46
+ raise UndefinedMethodError, "Endpoint '#{method_name}' not found in '#{@api.version}' API interface."
47
+ end
48
+
49
+ endpoint = @api.endpoint method_name
50
+
51
+ unless endpoint.params_valid? (params)
52
+ raise MissingParametersError, "Missed parameters #{endpoint.missing_params(params).to_s} required by endpoint '#{method_name}'."
53
+ end
54
+
55
+ result = @connection.public_send(endpoint.method, endpoint.uri, params, {})
56
+
57
+ if @api.error? result.status
58
+ raise ApiError, "#{@api.version} error: #{@api.error(result.status)}"
59
+ end
60
+
61
+ # noinspection RubyResolve
62
+ JSON.parse result.body, object_class: OpenStruct
63
+ end
64
+ end
65
65
  end
@@ -1,51 +1,51 @@
1
- module VtApi
2
-
3
- # @abstract Base class for API interfaces.
4
- class ApiVersion
5
- # Get API base URL.
6
- #
7
- # @return [String]
8
- def base_url
9
- ''
10
- end
11
-
12
- # API interface name/version.
13
- #
14
- # @return [String]
15
- def version
16
- nil
17
- end
18
-
19
- # Get API HTTP-code description.
20
- #
21
- # @param [Integer] http_code
22
- # @return [String] Error description.
23
- def error(http_code)
24
- nil
25
- end
26
-
27
- # Check whether API HTTP-code means error.
28
- #
29
- # @param [Integer] http_code
30
- # @return [Boolean] Error description.
31
- def error?(http_code)
32
- false
33
- end
34
-
35
- # Get API method endpoint interface.
36
- #
37
- # @param [String] method API method name to be called.
38
- # @return [VtApi::Endpoint]
39
- def endpoint(method)
40
- nil
41
- end
42
-
43
- # Check whether given method is defined in interface.
44
- #
45
- # @param [String] method Method name.
46
- # @return [Boolean]
47
- def endpoint?(method)
48
- false
49
- end
50
- end
1
+ module VtApi
2
+
3
+ # @abstract Base class for API interfaces.
4
+ class ApiVersion
5
+ # Get API base URL.
6
+ #
7
+ # @return [String]
8
+ def base_url
9
+ ''
10
+ end
11
+
12
+ # API interface name/version.
13
+ #
14
+ # @return [String]
15
+ def version
16
+ nil
17
+ end
18
+
19
+ # Get API HTTP-code description.
20
+ #
21
+ # @param [Integer] http_code
22
+ # @return [String] Error description.
23
+ def error(http_code)
24
+ nil
25
+ end
26
+
27
+ # Check whether API HTTP-code means error.
28
+ #
29
+ # @param [Integer] http_code
30
+ # @return [Boolean] Error description.
31
+ def error?(http_code)
32
+ false
33
+ end
34
+
35
+ # Get API method endpoint interface.
36
+ #
37
+ # @param [String] method API method name to be called.
38
+ # @return [VtApi::Endpoint]
39
+ def endpoint(method)
40
+ nil
41
+ end
42
+
43
+ # Check whether given method is defined in interface.
44
+ #
45
+ # @param [String] method Method name.
46
+ # @return [Boolean]
47
+ def endpoint?(method)
48
+ false
49
+ end
50
+ end
51
51
  end
@@ -1,69 +1,69 @@
1
- require 'faraday/connection'
2
-
3
- module VtApi
4
- # API endpoint interface class.
5
- class Endpoint
6
- attr_accessor :uri, :method
7
-
8
- # Initialize new endpoint.
9
- #
10
- # Interface parameters are described as in example below.
11
- # @example
12
- # {
13
- # foo: true, # this describes required parameter
14
- # bar: false # this describes a parameter that can be omitted
15
- # }
16
- #
17
- # @param [Object] uri Relative URL of the endpoint.
18
- # @param [Object] method HTTP method to be used for the endpoint.
19
- # @param [Object] params Endpoint parameters interface.
20
- def initialize(uri, method, params = {})
21
- @uri = uri
22
- @method = method
23
- @params = params
24
- end
25
-
26
-
27
- # Check whether given parameters match endpoint interface.
28
- #
29
- # @param [Hash] passed_params
30
- # @return [Boolean]
31
- def params_valid?(passed_params)
32
- @params.each do |param, required|
33
- if required and passed_params[param].nil?
34
- return false
35
- end
36
- end
37
-
38
- true
39
- end
40
-
41
- # Get a list of missing parameters.
42
- #
43
- # @param [Hash] passed_params
44
- # @return [Array]
45
- def missing_params(passed_params)
46
- missing = []
47
-
48
- @params.each do |param, required|
49
- if required and passed_params[param].nil?
50
- missing << param
51
- end
52
- end
53
-
54
- missing
55
- end
56
-
57
- private
58
-
59
- # Remove redundant slashes from URL.
60
- # @example
61
- # clean_url 'http://google.com////search' # => 'http://google.com/search'
62
- #
63
- # @param [String] uri
64
- # @return [String]
65
- def clean_uri(uri)
66
- uri.gsub %r{([^:])[\/]+}, '\1/'
67
- end
68
- end
1
+ require 'faraday/connection'
2
+
3
+ module VtApi
4
+ # API endpoint interface class.
5
+ class Endpoint
6
+ attr_accessor :uri, :method
7
+
8
+ # Initialize new endpoint.
9
+ #
10
+ # Interface parameters are described as in example below.
11
+ # @example
12
+ # {
13
+ # foo: true, # this describes required parameter
14
+ # bar: false # this describes a parameter that can be omitted
15
+ # }
16
+ #
17
+ # @param [Object] uri Relative URL of the endpoint.
18
+ # @param [Object] method HTTP method to be used for the endpoint.
19
+ # @param [Object] params Endpoint parameters interface.
20
+ def initialize(uri, method, params = {})
21
+ @uri = uri
22
+ @method = method
23
+ @params = params
24
+ end
25
+
26
+
27
+ # Check whether given parameters match endpoint interface.
28
+ #
29
+ # @param [Hash] passed_params
30
+ # @return [Boolean]
31
+ def params_valid?(passed_params)
32
+ @params.each do |param, required|
33
+ if required and passed_params[param].nil?
34
+ return false
35
+ end
36
+ end
37
+
38
+ true
39
+ end
40
+
41
+ # Get a list of missing parameters.
42
+ #
43
+ # @param [Hash] passed_params
44
+ # @return [Array]
45
+ def missing_params(passed_params)
46
+ missing = []
47
+
48
+ @params.each do |param, required|
49
+ if required and passed_params[param].nil?
50
+ missing << param
51
+ end
52
+ end
53
+
54
+ missing
55
+ end
56
+
57
+ private
58
+
59
+ # Remove redundant slashes from URL.
60
+ # @example
61
+ # clean_url 'http://google.com////search' # => 'http://google.com/search'
62
+ #
63
+ # @param [String] uri
64
+ # @return [String]
65
+ def clean_uri(uri)
66
+ uri.gsub %r{([^:])[\/]+}, '\1/'
67
+ end
68
+ end
69
69
  end
@@ -1,130 +1,130 @@
1
- require_relative '../api_version'
2
- require_relative '../endpoint'
3
-
4
- module VtApi
5
- module Versions
6
-
7
- # VirusTotal Public API 2.0 singleton interface.
8
- # Describes all available API methods (endpoints).
9
- class ApiV2 < ApiVersion
10
-
11
- # Root URI for all endpoints.
12
- BASE_URI = 'https://virustotal.com/vtapi/v2/'
13
-
14
- # VirusTotal Public API v2.0 interface description.
15
- ENDPOINTS = {
16
- 'file.report': Endpoint.new('file/report', :get, {
17
- apikey: true,
18
- resource: true
19
- }),
20
- 'file.scan': Endpoint.new('file/scan', :post, {
21
- apikey: true,
22
- file: true
23
- }),
24
- 'file.rescan': Endpoint.new('file/rescan', :post, {
25
- apikey: true,
26
- resource: true
27
- }),
28
- 'url.report': Endpoint.new('url/report', :get, {
29
- apikey: true,
30
- resource: true,
31
- schedule_scan: false
32
- }),
33
- 'url.scan': Endpoint.new('url/scan', :post, {
34
- apikey: true,
35
- url: true
36
- }),
37
- 'domain.report': Endpoint.new('domain/report', :get, {
38
- apikey: true,
39
- domain: true
40
- }),
41
- 'ip-address.report': Endpoint.new('ip-address/report', :get, {
42
- apikey: true,
43
- domain: true
44
- }),
45
- 'comments.get': Endpoint.new('comments/get', :get, {
46
- apikey: true,
47
- resource: true,
48
- before: false
49
- }),
50
- 'comments.put': Endpoint.new('comments/put', :post, {
51
- apikey: true,
52
- resource: true,
53
- comment: true
54
- }),
55
- }
56
-
57
- # List of possible HTTP error codes with descriptions
58
- ERRORS = {
59
- 204 => "Request rate limit exceeded.",
60
- 400 => "Invalid arguments.",
61
- 403 => "Access denied.",
62
- 404 => "Endpoint not found."
63
- }
64
-
65
- class << self
66
- # Get interface instance.
67
- #
68
- # @return [VtApi::Versions::ApiV2]
69
- def instance
70
- @instance ||= self.new
71
- end
72
- end
73
-
74
- # Get API method endpoint interface.
75
- #
76
- # @param [String] method API method name to be called.
77
- # @return [VtApi::Endpoint]
78
- def endpoint(method)
79
- ENDPOINTS[method.to_sym] unless ENDPOINTS[method.to_sym].nil?
80
- end
81
-
82
- # Check whether given method is defined in interface.
83
- #
84
- # @param [String] method Method name.
85
- # @return [Boolean]
86
- def endpoint?(method)
87
- not endpoint(method).nil?
88
- end
89
-
90
- # Get API HTTP-code description.
91
- #
92
- # @param [Integer] http_code
93
- # @return [String] Error description.
94
- def error(http_code)
95
- ERRORS[http_code] unless ERRORS[http_code].nil?
96
- end
97
-
98
- # Check whether API HTTP-code means error.
99
- #
100
- # @param [Integer] http_code
101
- # @return [Boolean] Error description.
102
- def error?(http_code)
103
- not error(http_code).nil?
104
- end
105
-
106
- # Get API base URL.
107
- #
108
- # @return [String]
109
- def base_url
110
- BASE_URI
111
- end
112
-
113
- # API interface name/version.
114
- #
115
- # @return [String]
116
- def version
117
- 'VTAPI-2.0'
118
- end
119
-
120
- private
121
-
122
- # Locked constructor.
123
- def initialize
124
- end
125
- end
126
-
127
- # Shorthand for <code>ApiV2.instance</code>
128
- API_V2 = ApiV2.instance
129
- end
1
+ require_relative '../api_version'
2
+ require_relative '../endpoint'
3
+
4
+ module VtApi
5
+ module Versions
6
+
7
+ # VirusTotal Public API 2.0 singleton interface.
8
+ # Describes all available API methods (endpoints).
9
+ class ApiV2 < ApiVersion
10
+
11
+ # Root URI for all endpoints.
12
+ BASE_URI = 'https://virustotal.com/vtapi/v2/'
13
+
14
+ # VirusTotal Public API v2.0 interface description.
15
+ ENDPOINTS = {
16
+ 'file.report': Endpoint.new('file/report', :get, {
17
+ apikey: true,
18
+ resource: true
19
+ }),
20
+ 'file.scan': Endpoint.new('file/scan', :post, {
21
+ apikey: true,
22
+ file: true
23
+ }),
24
+ 'file.rescan': Endpoint.new('file/rescan', :post, {
25
+ apikey: true,
26
+ resource: true
27
+ }),
28
+ 'url.report': Endpoint.new('url/report', :get, {
29
+ apikey: true,
30
+ resource: true,
31
+ schedule_scan: false
32
+ }),
33
+ 'url.scan': Endpoint.new('url/scan', :post, {
34
+ apikey: true,
35
+ url: true
36
+ }),
37
+ 'domain.report': Endpoint.new('domain/report', :get, {
38
+ apikey: true,
39
+ domain: true
40
+ }),
41
+ 'ip-address.report': Endpoint.new('ip-address/report', :get, {
42
+ apikey: true,
43
+ domain: true
44
+ }),
45
+ 'comments.get': Endpoint.new('comments/get', :get, {
46
+ apikey: true,
47
+ resource: true,
48
+ before: false
49
+ }),
50
+ 'comments.put': Endpoint.new('comments/put', :post, {
51
+ apikey: true,
52
+ resource: true,
53
+ comment: true
54
+ }),
55
+ }
56
+
57
+ # List of possible HTTP error codes with descriptions
58
+ ERRORS = {
59
+ 204 => "Request rate limit exceeded.",
60
+ 400 => "Invalid arguments.",
61
+ 403 => "Access denied.",
62
+ 404 => "Endpoint not found."
63
+ }
64
+
65
+ class << self
66
+ # Get interface instance.
67
+ #
68
+ # @return [VtApi::Versions::ApiV2]
69
+ def instance
70
+ @instance ||= self.new
71
+ end
72
+ end
73
+
74
+ # Get API method endpoint interface.
75
+ #
76
+ # @param [String] method API method name to be called.
77
+ # @return [VtApi::Endpoint]
78
+ def endpoint(method)
79
+ ENDPOINTS[method.to_sym] unless ENDPOINTS[method.to_sym].nil?
80
+ end
81
+
82
+ # Check whether given method is defined in interface.
83
+ #
84
+ # @param [String] method Method name.
85
+ # @return [Boolean]
86
+ def endpoint?(method)
87
+ not endpoint(method).nil?
88
+ end
89
+
90
+ # Get API HTTP-code description.
91
+ #
92
+ # @param [Integer] http_code
93
+ # @return [String] Error description.
94
+ def error(http_code)
95
+ ERRORS[http_code] unless ERRORS[http_code].nil?
96
+ end
97
+
98
+ # Check whether API HTTP-code means error.
99
+ #
100
+ # @param [Integer] http_code
101
+ # @return [Boolean] Error description.
102
+ def error?(http_code)
103
+ not error(http_code).nil?
104
+ end
105
+
106
+ # Get API base URL.
107
+ #
108
+ # @return [String]
109
+ def base_url
110
+ BASE_URI
111
+ end
112
+
113
+ # API interface name/version.
114
+ #
115
+ # @return [String]
116
+ def version
117
+ 'VTAPI-2.0'
118
+ end
119
+
120
+ private
121
+
122
+ # Locked constructor.
123
+ def initialize
124
+ end
125
+ end
126
+
127
+ # Shorthand for <code>ApiV2.instance</code>
128
+ API_V2 = ApiV2.instance
129
+ end
130
130
  end
@@ -1,11 +1,11 @@
1
- require_relative 'api_v2'
2
-
3
- module VtApi
4
- module Versions
5
-
6
- # Default API version used by ApiProvider
7
- #
8
- # @see VtApi::ApiProvider
9
- DEFAULT = API_V2
10
- end
1
+ require_relative 'api_v2'
2
+
3
+ module VtApi
4
+ module Versions
5
+
6
+ # Default API version used by ApiProvider
7
+ #
8
+ # @see VtApi::ApiProvider
9
+ DEFAULT = API_V2
10
+ end
11
11
  end
@@ -1,2 +1,2 @@
1
- require_relative 'versions/api_v2'
1
+ require_relative 'versions/api_v2'
2
2
  require_relative 'versions/default'
@@ -1,4 +1,4 @@
1
- require_relative 'internal/api_provider'
2
- require_relative 'internal/api_version'
3
- require_relative 'internal/endpoint'
1
+ require_relative 'internal/api_provider'
2
+ require_relative 'internal/api_version'
3
+ require_relative 'internal/endpoint'
4
4
  require_relative 'internal/versions'
@@ -1,3 +1,3 @@
1
- module VtApi
2
- VERSION = "0.1.2"
3
- end
1
+ module VtApi
2
+ VERSION = "0.1.2.1"
3
+ end