Skip to content

Commit

Permalink
Turnitin gem and delayed job skeleton
Browse files Browse the repository at this point in the history
Fixes PLAT-1168

Test Plan:
run the tests :D

Change-Id: Ica5967b249c3b58b73070c75ac53f188e91fea7e
Reviewed-on: https://gerrit.instructure.com/59453
Tested-by: Jenkins
Reviewed-by: Nathan Mills <[email protected]>
Product-Review: Nathan Mills <[email protected]>
QA-Review: Nathan Mills <[email protected]>
  • Loading branch information
Brad Horrocks authored and rivernate committed Jul 30, 2015
1 parent 8f76480 commit 7fa0113
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gems/turnitin_api/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
4 changes: 4 additions & 0 deletions gems/turnitin_api/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in turnitin_api.gemspec
gemspec
21 changes: 21 additions & 0 deletions gems/turnitin_api/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Brad Horrocks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
41 changes: 41 additions & 0 deletions gems/turnitin_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# TurnitinApi

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/turnitin`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'turnitin'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install turnitin

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/turnitin.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

6 changes: 6 additions & 0 deletions gems/turnitin_api/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions gems/turnitin_api/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "turnitin_api"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
7 changes: 7 additions & 0 deletions gems/turnitin_api/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
12 changes: 12 additions & 0 deletions gems/turnitin_api/lib/turnitin_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'json'
require 'securerandom'
require 'simple_oauth'
require 'faraday'
require 'faraday_middleware'

require 'turnitin_api/version'
require 'turnitin_api/outcomes_response_transformer'

module TurnitinApi

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "turnitin_api/version"

module TurnitinApi
class OutcomesResponseTransformer

# key
# secret
# turnitin_api response

attr_accessor :outcomes_response_json, :key
def initialize(key, secret, outcomes_response_json)
@key = key
@secret = secret
@outcomes_response_json = outcomes_response_json
end

def response
@response ||= make_call(outcomes_response_json['outcomes_tool_placement_url'])
end

# download original
def original_submission
yield make_call(response.body["outcome_originalfile"]["launch_url"])
end

# store link to report
def originality_report_url
response.body["outcome_originalityreport"]["launch_url"]
end

def originality_data
response.body['outcome_originalityreport'].select {|k, _| %w(breakdown numeric).include?(k)}
end

private

def connection
@connection ||= Faraday.new do |conn|
conn.request :multipart
conn.request :url_encoded
conn.response :json, :content_type => /\bjson$/
conn.adapter :net_http
end
end

def make_call(url)
header = SimpleOAuth::Header.new(:post, url, {}, consumer_key: @key, consumer_secret: @secret, callback: 'about:blank')
connection.post url, header.signed_attributes
end

end
end
3 changes: 3 additions & 0 deletions gems/turnitin_api/lib/turnitin_api/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module TurnitinApi
VERSION = "0.1.0"
end
57 changes: 57 additions & 0 deletions gems/turnitin_api/spec/fixtures/outcome_detailed_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"outcome_pdffile":{
"roles":[
"Learner",
"Instructor"
],
"launch_url":"https://turnitin.com/api/lti/1p0/dow...72874634?lang=",
"text":null,
"label":"Download File in PDF Format"
},
"outcome_originalfile":{
"roles":[
"Learner",
"Instructor"
],
"launch_url":"https://turnitin.com/api/lti/1p0/dow...72874634?lang=",
"text":null,
"label":"Download File in Original Format"
},
"outcome_resubmit":{
"roles":[
"Learner"
],
"launch_url":"https://turnitin.com/api/lti/1p0/upl...72874634?lang=",
"text":null,
"label":"Resubmit File"
},
"outcome_originalityreport":{
"roles":[
"Instructor"
],
"breakdown":{
"submitted_works_score":100,
"publications_score":0,
"internet_score":0
},
"launch_url":"https://turnitin.com/api/lti/1p0/dv/...72874634?lang=",
"text":"100%",
"numeric":{
"max":100,
"score":100
},
"label":"Open Originality Report"
},
"outcome_grademark":{
"roles":[
"Instructor"
],
"launch_url":"https://turnitin.com/api/lti/1p0/dv/...72874634?lang=",
"text":"--",
"numeric":{
"max":100,
"score":null
},
"label":"Open GradeMark"
}
}
90 changes: 90 additions & 0 deletions gems/turnitin_api/spec/outcomes_response_transformer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'spec_helper'

describe TurnitinApi::OutcomesResponseTransformer do
let(:oauth_key) { 'key' }
let(:oauth_secret) { 'secret' }
let(:outcomes_response_json) do
{
'lis_result_sourcedid' => "6",
'paperid' => "7",
'outcomes_tool_placement_url' => "http://turnitin.com/api/lti/1p0/outcome_tool_data/4321"
}
end
subject { described_class.new(oauth_key, oauth_secret, outcomes_response_json) }

describe 'initialize' do

it 'initializes properly' do
expect(subject.key).to eq oauth_key
expect(subject.outcomes_response_json).to eq outcomes_response_json
end
end

describe 'response' do
before(:each) do
stub_request(:post, "http://turnitin.com/api/lti/1p0/outcome_tool_data/4321").
to_return(:status => 200, :body => fixture('outcome_detailed_response.json'), :headers => {'Content-Type' => 'application/json'})
end

it 'returns expected json response' do
expect(subject.response.body["outcome_originalfile"]["launch_url"]).to eq "https://turnitin.com/api/lti/1p0/dow...72874634?lang="
end

end

describe 'original_submission' do
before(:each) do
stub_request(:post, "http://turnitin.com/api/lti/1p0/outcome_tool_data/4321").
to_return(:status => 200, :body => fixture('outcome_detailed_response.json'), :headers => {'Content-Type' => 'application/json'})

stub_request(:post, "https://turnitin.com/api/lti/1p0/dow...72874634?lang=").
to_return(:status => 200, :body => "I am an awesome text file", :headers => {'Content-Type' => 'text/plain', 'Content-Disposition' => 'attachment; filename="myfile.txt"'})

end

it 'returns a File' do
subject.original_submission do |response|
expect(response.body).to eq "I am an awesome text file"
end
end

end

describe 'originality report' do
before(:each) do
stub_request(:post, "http://turnitin.com/api/lti/1p0/outcome_tool_data/4321").
to_return(:status => 200, :body => fixture('outcome_detailed_response.json'), :headers => {'Content-Type' => 'application/json'})

end

it 'returns a url' do
expect(subject.originality_report_url).to eq "https://turnitin.com/api/lti/1p0/dv/...72874634?lang="
end

end


describe 'originality data' do
before(:each) do
stub_request(:post, "http://turnitin.com/api/lti/1p0/outcome_tool_data/4321").
to_return(:status => 200, :body => fixture('outcome_detailed_response.json'), :headers => {'Content-Type' => 'application/json'})

end

it 'returns proper keys' do
expect(subject.originality_data['breakdown']).to_not be_nil
expect(subject.originality_data['numeric']).to_not be_nil
end

it 'breakdown is set correctly' do
expect(subject.originality_data['breakdown']['submitted_works_score']).to eq 100
expect(subject.originality_data['breakdown']['publications_score']).to eq 0
expect(subject.originality_data['breakdown']['internet_score']).to eq 0
end

it 'numeric is set correctly' do
expect(subject.originality_data['numeric']['max']).to eq 100
expect(subject.originality_data['numeric']['score']).to eq 100
end
end
end
15 changes: 15 additions & 0 deletions gems/turnitin_api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'turnitin_api'
require 'webmock/rspec'


WebMock.disable_net_connect!(allow_localhost: true)


def fixture(*file)
File.new(File.join(File.expand_path("../fixtures", __FILE__), *file))
end

def json_fixture(*file)
JSON.parse(fixture(*file).read)
end
8 changes: 8 additions & 0 deletions gems/turnitin_api/spec/turnitin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'spec_helper'

describe TurnitinApi do
it 'has a version number' do
expect(TurnitinApi::VERSION).not_to be nil
end

end
15 changes: 15 additions & 0 deletions gems/turnitin_api/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
result=0

echo "################ turnitin_api ################"
bundle check || bundle install
bundle exec rspec spec
result+=$?

if [ $result -eq 0 ]; then
echo "SUCCESS"
else
echo "FAILURE"
fi

exit $result
30 changes: 30 additions & 0 deletions gems/turnitin_api/turnitin_api.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'turnitin_api/version'

Gem::Specification.new do |spec|
spec.name = "turnitin_api"
spec.version = TurnitinApi::VERSION
spec.authors = ["Brad Horrocks"]
spec.email = ["[email protected]"]

spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"

spec.files = Dir['{lib}/**/*'] + ['LICENSE.txt', 'README.md', 'Changelog.txt']
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency 'simple_oauth', '0.2'
spec.add_dependency 'webmock', '1.21'
spec.add_dependency 'faraday', '~> 0.8'
spec.add_dependency 'faraday_middleware', '~> 0.8'

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
end
Loading

0 comments on commit 7fa0113

Please sign in to comment.