Skip to content

Commit

Permalink
Add basic OAuth client
Browse files Browse the repository at this point in the history
  • Loading branch information
waynegraham committed Feb 7, 2017
1 parent 2c1ab8f commit 5058fa0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/zotero.rb
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
35 changes: 35 additions & 0 deletions lib/zotero/public_oauth_access_token.rb
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

0 comments on commit 5058fa0

Please sign in to comment.