bush_viper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0e1c9970a2274ff6e271875c26c851a8bd1981b9
4
+ data.tar.gz: ae42c62b5a8bfd539cfa7c44af05495d25c29a9f
5
+ SHA512:
6
+ metadata.gz: 90d63322bef5a1cbd9d57aabc850a09bc2fe9bae7608a7d7676491d45b5507a212cae0b34700ada6f0625e643180b45e015d3bb06365fa5b39044a978bddcd5b
7
+ data.tar.gz: b4059fa86188a4cf321170240b4a1978fd06860695c9a4569a5ff2094a56ebd17115786669a0d95b0b6ba954a15808b930ebbe07a5fe50bd779b175b0ddd383e
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ # custom
17
+ .env
18
+ /test/cassettes/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Kori Roys
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # BushViper
2
+
3
+ ![](https://github.com/koriroys/images/blob/master/iStock_000053428128_Medium_bush_viper.jpg)
4
+
5
+ Image credits: [Mark Kostich](http://www.istockphoto.com/photo/african-bush-viper-venomous-snake-53428128)
6
+
7
+ ## Installation
8
+
9
+ Add this line to the Gemfile of your application:
10
+
11
+ ```ruby
12
+ gem "bush_viper"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bush_viper
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/koriroys/bush_viper/fork )
30
+ 2. Create your feature branch (`git checkout -b your-initials/your-new-feature`)
31
+ 3. Commit your changes (`git commit -am "Reasoning for feature"`)
32
+ 4. Push to the branch (`git push origin your-initials/your-new-feature`)
33
+ 5. Create a new Pull Request
34
+
35
+ ## Testing
36
+
37
+ For running the tests you will need an access token. Since Mendeley offers no way to authenticate without a browser callback flow, the easiest way to get a token is to go to dev.mendeley.com and login. Copy the access_token from the url.
38
+
39
+ Create a .env file in the main directory, then make it look like this:
40
+
41
+ ACCESS_TOKEN=<access_token>
42
+
43
+ (<access_token> being the token you just copied)
44
+
45
+ Then, to run the tests:
46
+
47
+ $ rake
48
+
49
+ The first time you run the tests, it will make calls to the Mendeley API.
50
+ After that, VCR will kick in and the recorded cassetes will make the tests
51
+ much faster.
52
+
53
+ I recommend using a throwaway Mendeley account since the tests will create
54
+ quite a few dummy objects using the account.
@@ -0,0 +1,20 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ dir = File.dirname(__FILE__)
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "lib" << "test"
10
+ t.test_files = Dir.glob("#{dir}/test/**/*_test.rb")
11
+ t.warning = true
12
+ t.verbose = true
13
+ end
14
+
15
+ namespace :test do
16
+ desc "Clear all test VCR recordings"
17
+ task :eject do
18
+ system "rm -rf test/cassettes"
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bush_viper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bush_viper"
8
+ spec.version = BushViper::VERSION
9
+ spec.authors = ["Kori Roys"]
10
+ spec.email = ["kori@koriroys.com"]
11
+ spec.summary = %q{Ruby wrapper for Mendeley API}
12
+ spec.description = %q{Ruby wrapper for Mendeley API with a really cool name}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday", "~> 0.9"
22
+ spec.add_dependency "multi_json", "~> 1.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "dotenv", "~> 2.0"
27
+ spec.add_development_dependency "vcr", "~> 2.9"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ spec.add_development_dependency "minitest-vcr", "~> 1.0"
30
+ spec.add_development_dependency "webmock", "~> 1.0"
31
+ end
@@ -0,0 +1,26 @@
1
+ require "bush_viper/version"
2
+ require "bush_viper/documents"
3
+ require "bush_viper/connection"
4
+
5
+ module BushViper
6
+ module Mendeley
7
+ class API
8
+ def initialize(token)
9
+ self.token = token
10
+ end
11
+
12
+ def documents
13
+ Documents.new(connection)
14
+ end
15
+
16
+ def connection
17
+ @connection ||= Connection.new(BASE_URL, token)
18
+ end
19
+
20
+ private
21
+
22
+ attr_accessor :token
23
+ BASE_URL = "https://api.mendeley.com/"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ require "faraday"
2
+ require "multi_json"
3
+
4
+ class Connection
5
+ def initialize(url, token)
6
+ self.connection = Faraday.new(url: url)
7
+ self.token = token
8
+ end
9
+
10
+ def get(endpoint)
11
+ result = connection.get do |request|
12
+ request.url endpoint
13
+ request.headers["Authorization"] = "Bearer #{token}"
14
+ end
15
+ MultiJson.load(result.body)
16
+ end
17
+
18
+ def post(endpoint, file, filename)
19
+ result = connection.post do |request|
20
+ request.url endpoint
21
+ request.headers["Content-Disposition"] = %Q{attachment; filename="#{filename}"}
22
+ request.headers["Authorization"] = "Bearer #{token}"
23
+ request.headers["Content-Type"] = "application/pdf"
24
+ request.body = file
25
+ end
26
+ MultiJson.load(result.body)
27
+ end
28
+
29
+ def patch(endpoint, params)
30
+ params = MultiJson.dump(params)
31
+ result = connection.patch do |request|
32
+ request.url endpoint
33
+ request.headers["Authorization"] = "Bearer #{token}"
34
+ request.headers['Content-Type'] = "application/vnd.mendeley-document.1+json"
35
+ request.body = params
36
+ end
37
+ MultiJson.load(result.body)
38
+ end
39
+
40
+ private
41
+
42
+ attr_accessor :connection, :token
43
+ end
@@ -0,0 +1,31 @@
1
+ class Documents
2
+ def initialize(connection)
3
+ self.connection = connection
4
+ end
5
+
6
+ def all
7
+ connection.get("documents")
8
+ end
9
+
10
+ def types
11
+ connection.get("document_types")
12
+ end
13
+
14
+ def create(params, filepath: nil, url: nil, filename: nil)
15
+ if filepath
16
+ File.open(filepath, "rb") do |file|
17
+ result = connection.post("documents/", file.read, filename || File.basename(filepath))
18
+ connection.patch(%Q{documents/#{result["id"]}}, params)
19
+ end
20
+ elsif url
21
+ uri = URI.parse(url)
22
+ content = Net::HTTP.get(uri)
23
+ result = connection.post("documents/", content, filename || File.basename(uri.path))
24
+ connection.patch(%Q{documents/#{result["id"]}}, params)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ attr_accessor :connection
31
+ end
@@ -0,0 +1,3 @@
1
+ module BushViper
2
+ VERSION = "0.0.1"
3
+ end
data/test.rb ADDED
@@ -0,0 +1,8 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require "bush_viper"
5
+
6
+ m = BushViper::Mendeley::API.new("MSwxNDM1MTY0NTU0NzIwLDMzNTAwOTI2MSw3MTIsYWxsLCwsOTRmMzA2NzM2MmU0MTM5NGRiMTM1ZmNhYmFiMTBiNGUzZDk0MS14eGEsRGZfdzNMeWR2ZDVNRkU1TmlDTUtWemtsODVN")
7
+
8
+ puts m.documents
@@ -0,0 +1,80 @@
1
+ require "test_helper"
2
+
3
+ describe "documents" do
4
+ attr_accessor :documents
5
+
6
+ before :all do
7
+ self.documents = BushViper::Mendeley::API.new(ENV["ACCESS_TOKEN"]).documents
8
+ end
9
+
10
+ it "returns empty array if no documents fetched", :vcr do
11
+ skip
12
+ assert_equal [], documents.all
13
+ end
14
+
15
+ it "returns a list of documents", :vcr do
16
+ skip
17
+ assert_equal ["hello"], documents.all
18
+ end
19
+
20
+ it "returns a list of possible document types", :vcr do
21
+ possible_document_types = [
22
+ { "name" => "journal", "description" => "Journal article" },
23
+ { "name" => "book", "description" => "Book" },
24
+ { "name" => "generic", "description" => "Generic" },
25
+ { "name" => "book_section", "description" => "Book section" },
26
+ { "name" => "conference_proceedings", "description" => "Conference proceedings" },
27
+ { "name" => "working_paper", "description" => "Working paper" },
28
+ { "name" => "report", "description" => "Report" },
29
+ { "name" => "web_page", "description" => "Web page" },
30
+ { "name" => "thesis", "description" => "Thesis" },
31
+ { "name" => "magazine_article", "description" => "Magazine article" },
32
+ { "name" => "statute", "description" => "Statute" },
33
+ { "name" => "patent", "description" => "Patent" },
34
+ { "name" => "newspaper_article", "description" => "Newspaper article" },
35
+ { "name" => "computer_program", "description" => "Computer program" },
36
+ { "name" => "hearing", "description" => "Hearing" },
37
+ { "name" => "television_broadcast", "description" => "Television broadcast" },
38
+ { "name" => "encyclopedia_article", "description" => "Encyclopedia article" },
39
+ { "name" => "case", "description" => "Case" },
40
+ { "name" => "film", "description" => "Film" },
41
+ { "name" => "bill", "description" => "Bill" }
42
+ ]
43
+ assert_equal documents.types, possible_document_types
44
+ end
45
+
46
+ it "can upload a document given a filepath", :vcr do
47
+ params = {
48
+ title: "Test Title",
49
+ type: "Generic"
50
+ }
51
+ document = documents.create(params, filepath: "test/resources/bush_viper.pdf")
52
+ assert_equal "Test Title", document["title"]
53
+ assert_equal "generic", document["type"]
54
+ assert_equal true, document["file_attached"]
55
+ end
56
+
57
+ it "can upload a document given a url", :vcr do
58
+ params = {
59
+ title: "Super Test Title",
60
+ type: "Journal"
61
+ }
62
+ document = documents.create(params, url: "https://raw.githubusercontent.com/koriroys/bush_viper/master/test/resources/bush_viper.pdf")
63
+ assert_equal "Super Test Title", document["title"]
64
+ assert_equal "journal", document["type"]
65
+ assert_equal true, document["file_attached"]
66
+ end
67
+
68
+ it "sets the document title correctly, even with query params", :vcr do
69
+ params = {
70
+ type: "Journal"
71
+ }
72
+ document = documents.create(params, url: "https://raw.githubusercontent.com/koriroys/bush_viper/master/test/resources/bush_viper.pdf?idontknowwhatImdoing=true/false")
73
+ puts document["title"]
74
+ # mendeley api strips off the file extension and makes that the title
75
+ assert_equal "bush_viper", document["title"]
76
+ assert_equal "journal", document["type"]
77
+ assert_equal true, document["file_attached"]
78
+ end
79
+ end
80
+
@@ -0,0 +1,11 @@
1
+ require "test_helper"
2
+
3
+ describe BushViper::Mendeley::API do
4
+ it "defines the base url constant" do
5
+ BushViper::Mendeley::API::BASE_URL.must_equal "https://api.mendeley.com/"
6
+ end
7
+
8
+ it "initializes with an access_token" do
9
+ BushViper::Mendeley::API.new("token").send(:token).must_equal "token"
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require "minitest/autorun"
2
+ require "minitest/pride"
3
+ require "vcr"
4
+ require "minitest-vcr"
5
+ require "dotenv"
6
+ require "webmock" # because faraday 0.9 and vcr 2.9.3 don't play nice together
7
+
8
+ lib = File.expand_path("../lib", __FILE__)
9
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
10
+
11
+ require "bush_viper"
12
+
13
+ VCR.configure do |c|
14
+ c.cassette_library_dir = 'test/cassettes'
15
+ c.hook_into :webmock
16
+ c.allow_http_connections_when_no_cassette = true
17
+ end
18
+
19
+ MinitestVcr::Spec.configure!
20
+ Dotenv.load
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bush_viper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kori Roys
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: dotenv
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '5.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '5.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest-vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.0'
139
+ description: Ruby wrapper for Mendeley API with a really cool name
140
+ email:
141
+ - kori@koriroys.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - bush_viper.gemspec
152
+ - lib/bush_viper.rb
153
+ - lib/bush_viper/connection.rb
154
+ - lib/bush_viper/documents.rb
155
+ - lib/bush_viper/version.rb
156
+ - test.rb
157
+ - test/lib/bush_viper/documents_test.rb
158
+ - test/lib/bush_viper_test.rb
159
+ - test/resources/bush_viper.pdf
160
+ - test/test_helper.rb
161
+ homepage: ''
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.4.5
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Ruby wrapper for Mendeley API
185
+ test_files:
186
+ - test/lib/bush_viper/documents_test.rb
187
+ - test/lib/bush_viper_test.rb
188
+ - test/resources/bush_viper.pdf
189
+ - test/test_helper.rb