plagscan 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fd408fb011126a3928ec92df17a63b8bb6added05c2428f07b47eca598b96dd
4
- data.tar.gz: fffe4c697ed70ba4b35be93a5d763345cfbf42c51e1a1903d6e86f723d45279b
3
+ metadata.gz: 2127487f273cb1dc5b45d7571465bfa129c284fd6e7952be9f849fe40af01961
4
+ data.tar.gz: e285ec1601bd3e3e0f05cc29b698b46620dfcbebdb39971f36fdb98a21e78224
5
5
  SHA512:
6
- metadata.gz: ce368219bb9309fc71a35d86c6f365c0ececf63e5aa919ccc28966a2b30ae6b8438f0fa27dfac59e957f4c5a8a1e9bd45cdbb665dfa24ff85f35fee9a0d38be8
7
- data.tar.gz: ad6a98a358d21cfca119451cc30c83e5fecacf0cac60deb93e35f338b2c9ee578e8f6e09affb8712a5b3b9d44b429ca4e71eccbaacb3acae41dad42f02ac4f90
6
+ metadata.gz: 770fab61fdd24d8ec9a7a76805fb939090858afdfd4237262cae40e43d0234c571363ffe5a99cadf926fe405afc006ab17a7f5e136acb1ccc0a06211b44a0378
7
+ data.tar.gz: 38e719eda4185a53180bfd8fbeef79da80662c333a9f49fbc5769d2acc20bd56e389cfcdd3f4a08421ffe678980d6bed213eee9c2440cdc8a01c86c0c7fe9b0b
@@ -1,5 +1,23 @@
1
+ require: rubocop-rspec
2
+
3
+ Layout/DotPosition:
4
+ EnforcedStyle: trailing
5
+
6
+ Metrics/BlockLength:
7
+ Exclude:
8
+ - "**/*_spec.rb"
9
+
1
10
  Metrics/LineLength:
2
11
  Max: 100
3
12
 
4
13
  Metrics/MethodLength:
5
- Max: 12
14
+ Max: 13
15
+
16
+ RSpec/ExampleLength:
17
+ Max: 16
18
+
19
+ RSpec/MessageSpies:
20
+ EnforcedStyle: receive
21
+
22
+ RSpec/MultipleExpectations:
23
+ Max: 2
@@ -1,3 +1,12 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+ - none
5
+
6
+ ## [0.0.2](releases/tag/v0.0.2) - 2019-08-14
7
+ - Added support for `token` API
8
+ - Added support for some `documents` APIs (create, check and retrieve)
9
+
1
10
  ## [0.0.1](releases/tag/v0.0.1) - 2019-08-13
2
11
  ### Added
3
12
  - Base implementation of gem along with `ping` API
@@ -1,5 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'uri'
4
+ require 'openssl'
5
+ require 'net/http'
6
+
7
+ require 'json'
8
+
3
9
  require 'plagscan/version'
4
10
 
5
11
  require 'plagscan/error'
@@ -7,6 +13,8 @@ require 'plagscan/request'
7
13
 
8
14
  # APIs
9
15
  require 'plagscan/ping'
16
+ require 'plagscan/token'
17
+ require 'plagscan/documents'
10
18
 
11
19
  #
12
20
  # Basic configuration for PlagScan API
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plagscan
4
+ #
5
+ # PlagScan documents API
6
+ #
7
+ class Documents
8
+ #
9
+ # Document create REST API
10
+ # @param [String] access_token Access token from Token.fetch
11
+ # @param [File] file Document file
12
+ # @param [String] text Text from a document in plain text
13
+ # @param [named options] userID, textname, toRepository, saveOrig
14
+ # @return [Hash] containing document ID and URI location for created resource
15
+ #
16
+ # Note. you should provide fileUpload OR textdata
17
+ # For more details, see https://api.plagscan.com/v3docs/#api-Document-SubmitDocument
18
+ #
19
+ def self.create(access_token:, file: nil, text: nil, **options)
20
+ raise 'must specify file or text' if file.nil? && text.nil?
21
+
22
+ create_props = options.delete_if do |k, _|
23
+ !%i[userID textname toRepository saveOrig].include? k
24
+ end
25
+
26
+ Plagscan::Request.json_request(
27
+ 'documents',
28
+ method: :post, access_token: access_token, expected_result: Net::HTTPCreated,
29
+ body: create_props.merge(file ? { fileUpload: file } : { textdata: text })
30
+ )
31
+ end
32
+
33
+ #
34
+ # Document create REST API
35
+ # @param [String] access_token Access token from Token.fetch
36
+ # @param [Integer] document_id Document ID as returned from create action
37
+ # @return [Null]
38
+ # @raise [DocumentError] Various reasons generally based around invalid document state.
39
+ #
40
+ # For more details, see https://api.plagscan.com/v3docs/#api-Document-CheckDocument
41
+ #
42
+ def self.check(access_token:, document_id:)
43
+ response =
44
+ Plagscan::Request.request(
45
+ "documents/#{document_id}/check",
46
+ method: :put, access_token: access_token
47
+ )
48
+
49
+ return if response.is_a? Net::HTTPNoContent
50
+
51
+ error_message =
52
+ begin
53
+ JSON.parse(response.body)&.dig('error', 'message')
54
+ rescue JSON::ParserError
55
+ nil
56
+ end
57
+ raise DocumentError, error_message || response.body
58
+ end
59
+
60
+ #
61
+ # Document retrieve REST API
62
+ # @param [String] access_token Access token from Token.fetch
63
+ # @param [Integer] document_id Document ID as returned from create action
64
+ # @param [Integer] mode The retrieve mode of the report
65
+ # @param [Integer] user_id Identify the user who is accessing to the report
66
+ # (Only mandatory for mode 10). If not set it will get the user
67
+ # ID associated with the access token or the organization admin ID.
68
+ # @return [Hash] Various different values depending on the mode specified
69
+ #
70
+ # For more details, see https://api.plagscan.com/v3docs/#api-Document-RetrieveDocumentReport
71
+ #
72
+ def self.retrieve(access_token:, document_id:, mode:, user_id: nil)
73
+ params = { mode: mode }
74
+ params[:userID] = user_id if user_id
75
+
76
+ Plagscan::Request.json_request(
77
+ "documents/#{document_id}/retrieve",
78
+ access_token: access_token, body: params
79
+ )
80
+ end
81
+ end
82
+ end
@@ -4,4 +4,6 @@ module Plagscan
4
4
  class Error < StandardError; end
5
5
  class HTTPError < Error; end
6
6
  class InvalidMethodError < HTTPError; end
7
+ class JsonParseError < Error; end
8
+ class DocumentError < Error; end
7
9
  end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  #
4
- # PlagScan module
4
+ # PlagScan ping API
5
5
  #
6
6
  module Plagscan
7
7
  #
8
- # PlagScan ping API
8
+ # ping REST API
9
+ # @return [Boolean]
9
10
  #
10
11
  def self.ping
11
12
  Plagscan::Request.request('ping').is_a? Net::HTTPOK
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uri'
4
- require 'openssl'
5
- require 'net/http'
6
-
7
3
  module Plagscan
8
4
  #
9
5
  # PlagScan HTTP request service
@@ -22,16 +18,29 @@ module Plagscan
22
18
  Plagscan.api_base + path
23
19
  end
24
20
 
21
+ def user_agent
22
+ "PlagScan-Ruby/#{Plagscan::VERSION}"
23
+ end
24
+
25
25
  def request(path, options = {})
26
26
  options = DEFAULT_REQUEST_OPTIONS.merge(options)
27
-
28
- raise Plagscan::InvalidMethodError unless %i[get post].include? options[:method]
29
-
30
27
  http = create_http(options)
31
28
  req = create_request(path, options)
32
29
  http.start { http.request(req) }
33
30
  end
34
31
 
32
+ def json_request(path, options = {})
33
+ response = Plagscan::Request.request(path, options)
34
+
35
+ unless response.is_a?(options[:expected_result] || Net::HTTPSuccess)
36
+ raise Plagscan::HTTPError, "Invalid http response code: #{response.code}"
37
+ end
38
+
39
+ JSON.parse response.body
40
+ rescue JSON::ParserError
41
+ raise Plagscan::JsonParseError, "PlagScan response parse error: #{response.body}"
42
+ end
43
+
35
44
  private
36
45
 
37
46
  def create_http(options)
@@ -48,35 +57,28 @@ module Plagscan
48
57
  end
49
58
 
50
59
  def create_request(path, options)
51
- headers = extract_headers(options)
52
- body = options[:body]
60
+ headers = { 'User-Agent' => user_agent }
61
+ uri = api_url path
53
62
 
54
- if options[:method] == :post
55
- req = Net::HTTP::Post.new(path, headers)
56
- add_body(req, body) if body
57
- req
63
+ if %i[post put patch].include? options[:method]
64
+ body_request uri, headers, options
58
65
  else
59
- uri = api_url path
60
- uri += '?' + body.map { |k, v| "#{k}=#{v}" }.join('&') if body
61
- Net::HTTP::Get.new(uri, headers)
66
+ uri_request uri, headers, options
62
67
  end
63
68
  end
64
69
 
65
- def extract_headers(options)
66
- headers = options[:headers]
67
-
68
- token = options.delete :token
69
- if token
70
- headers ||= {}
71
-
72
- headers['X-Auth-Token'] = token.auth_token
73
- headers['X-User-Id'] = token.user_id
74
- end
75
-
76
- return unless headers
70
+ def body_request(uri, headers, options)
71
+ uri += '?access_token=' + options[:access_token] if options[:access_token]
72
+ req = http_method(options).new(uri, headers)
73
+ add_body(req, options[:body]) if options[:body]
74
+ req
75
+ end
77
76
 
78
- headers = Util.stringify_hash_keys headers
79
- headers.delete_if { |key, value| key.nil? || value.nil? }
77
+ def uri_request(uri, headers, options)
78
+ body = options[:body] || {}
79
+ body[:access_token] = options[:access_token] if options[:access_token]
80
+ uri += '?' + body.map { |k, v| "#{k}=#{v}" }.join('&') unless body.empty?
81
+ http_method(options).new(uri, headers)
80
82
  end
81
83
 
82
84
  def add_body(request, body)
@@ -87,6 +89,14 @@ module Plagscan
87
89
  request.body = body.to_s
88
90
  end
89
91
  end
92
+
93
+ def http_method(options)
94
+ method = options[:method].to_s.downcase
95
+ method = method[0].upcase.concat(method[1..-1])
96
+ Net::HTTP.const_get(method)
97
+ rescue NameError
98
+ raise Plagscan::InvalidMethodError, "`#{options[:method]}` is not a valid HTTP method"
99
+ end
90
100
  end
91
101
  end
92
102
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plagscan
4
+ #
5
+ # PlagScan access token API
6
+ #
7
+ class Token
8
+ #
9
+ # token REST API
10
+ # @param [String] client_id your organisation ID
11
+ # @param [String] client_secret the API key from https://www.plagscan.com/apisetup
12
+ # @return [Hash] containing access_token and expires_in
13
+ #
14
+ def self.fetch(client_id:, client_secret:)
15
+ Plagscan::Request.json_request(
16
+ 'token',
17
+ method: :post, body: { client_id: client_id, client_secret: client_secret }
18
+ )
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plagscan
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
@@ -31,5 +31,6 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency 'rake', '~> 10.0'
32
32
  spec.add_development_dependency 'rspec', '~> 3.0'
33
33
  spec.add_development_dependency 'rubocop', '~> 0.74'
34
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.35'
34
35
  spec.add_development_dependency 'webmock', '~> 3.0'
35
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plagscan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-13 00:00:00.000000000 Z
11
+ date: 2019-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -86,6 +86,20 @@ dependencies:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0.74'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop-rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.35'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.35'
89
103
  - !ruby/object:Gem::Dependency
90
104
  name: webmock
91
105
  requirement: !ruby/object:Gem::Requirement
@@ -118,9 +132,11 @@ files:
118
132
  - Rakefile
119
133
  - bin/console
120
134
  - lib/plagscan.rb
135
+ - lib/plagscan/documents.rb
121
136
  - lib/plagscan/error.rb
122
137
  - lib/plagscan/ping.rb
123
138
  - lib/plagscan/request.rb
139
+ - lib/plagscan/token.rb
124
140
  - lib/plagscan/version.rb
125
141
  - plagscan.gemspec
126
142
  homepage: https://github.com/Studiosity/plagscan-ruby