diff --git a/README.md b/README.md index 0a2b7d1..d63f992 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,31 @@ client.get_user('') ``` +## Authentication + +You can generate access tokens through the link above, or you can use OAuth +Authentication + +In your app, make sure you have the omniauth gem installed. Add the following +to your ```intializers/omniauth.rb``` file + +```ruby + +Rails.application.config.middleware.use OmniAuth::Builder do + provider :pinterest, ENV['PINTEREST_APP_ID'], ENV['PINTEREST_APP_SECRET'] +end + +``` + +Direct your users to ```/auth/pinterest``` + +Once they approve your app, they'll be redirect to your callback URL, which +should be something like ```auth/pinterest/callback``` with a hash of +OAuth values from Pinterest in ```request.env['omniauth.auth']``` + +For more details, check out "Integrating OmniAuth Into Your Application" +https://github.com/intridea/omniauth + ## Known Issues The gem is currently under active development. diff --git a/lib/pinterest-api.rb b/lib/pinterest-api.rb index 7c86316..b50f0d6 100644 --- a/lib/pinterest-api.rb +++ b/lib/pinterest-api.rb @@ -1,5 +1,6 @@ require "pinterest/version" require "pinterest/client" +require "pinterest/omniauth" module Pinterest diff --git a/lib/pinterest/omniauth.rb b/lib/pinterest/omniauth.rb new file mode 100644 index 0000000..e354e5e --- /dev/null +++ b/lib/pinterest/omniauth.rb @@ -0,0 +1,28 @@ +require 'omniauth-oauth2' + +module OmniAuth + module Strategies + class Pinterest < OmniAuth::Strategies::OAuth2 + option :client_options, { + :site => 'https://api.pinterest.com/', + :authorize_url => 'https://api.pinterest.com/oauth/', + :token_url => 'https://api.pinterest.com/v1/oauth/token' + } + + def request_phase + options[:scope] ||= 'read_public,write_public' + options[:response_type] ||= 'token' + #options[:state] ||= '22' + super + end + + uid { raw_info['id'] } + + info { raw_info } + + def raw_info + @raw_info ||= access_token.get('/v1/me/').parsed['data'] + end + end + end +end diff --git a/lib/pinterest/version.rb b/lib/pinterest/version.rb index dcea343..bab6368 100644 --- a/lib/pinterest/version.rb +++ b/lib/pinterest/version.rb @@ -1,3 +1,3 @@ module Pinterest - VERSION = "0.1.1" + VERSION = "0.2.0" end diff --git a/pinterest-api-0.2.0.gem b/pinterest-api-0.2.0.gem new file mode 100644 index 0000000..92cab04 Binary files /dev/null and b/pinterest-api-0.2.0.gem differ diff --git a/pinterest.gemspec b/pinterest.gemspec index 5fe2698..0240696 100644 --- a/pinterest.gemspec +++ b/pinterest.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |spec| spec.summary = %q{Ruby gem to interact with the Pinterest REST API} spec.description = %q{This gem makes it simple to interact with the official Pinterest REST API} - spec.homepage = "http://github.com/realadeel" + spec.homepage = "http://github.com/realadeel/pinterest-api" spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } @@ -24,8 +24,11 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rspec", "~> 3.3" spec.add_development_dependency "vcr", "~> 2.9" spec.add_development_dependency "dotenv", "~> 2.0" + spec.add_development_dependency "webmock", "~> 1.0" spec.add_dependency 'faraday', "~> 0.9" - spec.add_dependency 'faraday_middleware', ">= 0.9.0" + spec.add_dependency 'faraday_middleware', "~> 0.9" spec.add_dependency 'hashie', "~> 3.0" + spec.add_dependency 'omniauth', '~> 1.0' + spec.add_dependency 'omniauth-oauth2', '~> 1.0' end diff --git a/spec/omniauth_spec.rb b/spec/omniauth_spec.rb new file mode 100644 index 0000000..d2e3e9c --- /dev/null +++ b/spec/omniauth_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe "Omniauth::Strategies::Pinterest" do + context 'client options' do + before :each do + @auth = OmniAuth::Strategies::Pinterest.new({}) + end + + it 'should call correct base url' do + expect(@auth.options.client_options.site).to eq('https://api.pinterest.com/') + end + + it 'should call correct authorization url' do + expect(@auth.options.client_options.authorize_url).to eq('https://api.pinterest.com/oauth/') + end + + it 'should call correct token url' do + expect(@auth.options.client_options.token_url).to eq('https://api.pinterest.com/v1/oauth/token') + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9259d42..78c63f5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,5 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'pinterest' +require 'pinterest-api' require 'vcr' require 'webmock/rspec' @@ -14,7 +14,7 @@ VCR.configure do |c| c.cassette_library_dir = "spec/fixtures/cassettes" c.hook_into :webmock - c.default_cassette_options = { record: :once } + c.default_cassette_options = { record: :none } c.filter_sensitive_data('') do |interaction| ENV['ACCESS_TOKEN'] end