-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c1ab8f
commit 5058fa0
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
require "zotero/version" | ||
|
||
gem 'oauth', '>= 0.5.1' | ||
require oauth | ||
|
||
module Zotero | ||
# Your code goes here... | ||
@default_site = 'https://api.zotero.org' # Version can be appended here, or passed in by the user | ||
|
||
# Will create an OAuth Consumer for you. | ||
# | ||
# You have to register your application on Zotero.org to get a consumer token and secret. | ||
# | ||
# Default provider site is http://api.zotero.org | ||
def self.consumer(consumer_token, consumer_secret, site = @default_site) | ||
return OAuth::Consumer.new(consumer_token, consumer_secret, { | ||
:site => site, | ||
:request_token_path => "/oauth/request_token", | ||
:access_token_path => "/oauth/access_token", | ||
:authorize_path => "/oauth/authorize" | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Zotero | ||
|
||
# Subclass to force API users to pass a consumer key with each request. | ||
class PublicOAuthAccessToken < OAuthActiveResource::FakeOAuthAccessToken | ||
|
||
attr_accessor :token, :secret | ||
def initialize(key) | ||
@key = key | ||
@token = "public #{key}" | ||
@secret = 'Anonymous' | ||
|
||
# ensure that keys are symbols | ||
@options = @@default_options | ||
end | ||
def request(http_method, path, token = nil, request_options = {}, *arguments) | ||
# Force a relative path from an absolute path | ||
if path !~ /^\// | ||
@http = create_http(path) | ||
end | ||
_uri = URI.parse(path) | ||
|
||
# Append the consumer key to the request | ||
if _uri.query.nil? | ||
_uri.query = "consumer_key=#{@key}" | ||
else | ||
_uri.query += "&consumer_key=#{@key}" | ||
end | ||
|
||
path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}" | ||
rsp = http.request(create_http_request(http_method, path, token, request_options, *arguments )) | ||
|
||
rsp | ||
end | ||
end | ||
end |