Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize global required method #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/asana.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'asana/ruby2_0_0_compatibility'
require 'asana/compatibility_helper'
require 'asana/authentication'
require 'asana/resources'
require 'asana/client'
Expand Down
4 changes: 2 additions & 2 deletions lib/asana/authentication/oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module OAuth2
#
# Note: This function reads from STDIN and writes to STDOUT. It is meant
# to be used only within the context of a CLI application.
def offline_flow(client_id: required('client_id'),
client_secret: required('client_secret'))
def offline_flow(client_id: CompatibilityHelper.required('client_id'),
client_secret: CompatibilityHelper.required('client_secret'))
client = Client.new(client_id: client_id,
client_secret: client_secret,
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class AccessTokenAuthentication
# Returns an [AccessTokenAuthentication] instance with a refreshed
# access token.
def self.from_refresh_token(refresh_token,
client_id: required('client_id'),
client_secret: required('client_secret'),
redirect_uri: required('redirect_uri'))
client_id: CompatibilityHelper.required('client_id'),
client_secret: CompatibilityHelper.required('client_secret'),
redirect_uri: CompatibilityHelper.required('redirect_uri'))
client = Client.new(client_id: client_id,
client_secret: client_secret,
redirect_uri: redirect_uri)
Expand Down
6 changes: 3 additions & 3 deletions lib/asana/authentication/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Client
# application
# redirect_uri - [String] a redirect uri from the registered
# application
def initialize(client_id: required('client_id'),
client_secret: required('client_secret'),
redirect_uri: required('redirect_uri'))
def initialize(client_id: CompatibilityHelper.required('client_id'),
client_secret: CompatibilityHelper.required('client_secret'),
redirect_uri: CompatibilityHelper.required('redirect_uri'))
@client = ::OAuth2::Client.new(client_id, client_secret,
site: 'https://app.asana.com',
authorize_url: '/-/oauth_authorize',
Expand Down
2 changes: 1 addition & 1 deletion lib/asana/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Client
# Internal: Proxies Resource classes to implement a fluent API on the Client
# instances.
class ResourceProxy
def initialize(client: required('client'), resource: required('resource'))
def initialize(client: CompatibilityHelper.required('client'), resource: CompatibilityHelper.required('resource'))
@client = client
@resource = resource
end
Expand Down
11 changes: 11 additions & 0 deletions lib/asana/compatibility_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Asana
module CompatibilityHelper
def required(name)
raise(ArgumentError, "#{name} is a required keyword argument")
end

extend self
end
end
2 changes: 1 addition & 1 deletion lib/asana/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HttpClient
# user_agent - [String] The user agent. Defaults to "ruby-asana vX.Y.Z".
# config - [Proc] An optional block that yields the Faraday builder
# object for customization.
def initialize(authentication: required('authentication'),
def initialize(authentication: CompatibilityHelper.required('authentication'),
adapter: nil,
user_agent: nil,
debug_mode: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/asana/resource_includes/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Collection
# client - [Asana::Client] the client to perform requests.
def initialize((elements, extra),
type: Resource,
client: required('client'))
client: CompatibilityHelper.required('client'))
@elements = elements.map { |elem| type.new(elem, client: client) }
@type = type
@next_page_data = extra['next_page']
Expand Down
4 changes: 2 additions & 2 deletions lib/asana/resource_includes/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Events
# client - [Asana::Client] a client to perform the requests.
# wait - [Integer] the number of seconds to wait between each poll.
# options - [Hash] the request I/O options
def initialize(resource: required('resource'),
client: required('client'),
def initialize(resource: CompatibilityHelper.required('resource'),
client: CompatibilityHelper.required('client'),
wait: 1, options: {})
@resource = resource
@client = client
Expand Down
3 changes: 3 additions & 0 deletions lib/asana/resource_includes/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative 'registry'
require_relative 'response_helper'
require_relative '../compatibility_helper'

module Asana
module Resources
Expand All @@ -10,6 +11,8 @@ module Resources
class Resource
include ResponseHelper
extend ResponseHelper
include CompatibilityHelper
extend CompatibilityHelper

def initialize(data, client: required('client'))
@_client = client
Expand Down
5 changes: 0 additions & 5 deletions lib/asana/ruby2_0_0_compatibility.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# frozen_string_literal: true

RSpec.describe Asana::Authentication::OAuth2::AccessTokenAuthentication do
describe '#from_refresh_token' do
let(:token) do
instance_double(OAuth2::AccessToken, expired?: false, token: 'TOKEN')
end

it 'raises an ArgumentError when required fields are missing' do
expect do
described_class.from_refresh_token(token)
end.to raise_error(ArgumentError)
end
end

describe described_class do
let(:auth) { described_class.new(token) }

Expand Down
6 changes: 6 additions & 0 deletions spec/asana/authentication/oauth2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
redirect_uri: 'http://redirect_uri.com')
end

describe '#initialize' do
it 'raises an ArgumentError when required fields are missing' do
expect { described_class.new }.to raise_error(ArgumentError)
end
end

describe '#authorize_url' do
it 'returns the OAuth2 authorize url' do
expect(client.authorize_url)
Expand Down
6 changes: 6 additions & 0 deletions spec/asana/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
described_class.new(authentication: auth, adapter: api.to_proc)
end

describe '#initialize' do
it "raises an ArgumentError when required fields are missing" do
expect { described_class.new }.to raise_error(ArgumentError)
end
end

describe '#get' do
it 'performs a GET request against the Asana API' do
api.on(:get, '/users/me') do |response|
Expand Down
6 changes: 6 additions & 0 deletions spec/asana/resources/attachment_uploading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ def self.plural_name
expect(attachment.gid).to eq('10')
end
end

context 'without filename or mime' do
it 'raises an error' do
expect { unicorn.attach }.to raise_error(ArgumentError)
end
end
end
end
10 changes: 10 additions & 0 deletions spec/asana/resources/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@

include ResourcesHelper

describe '#initialize' do
context 'no client provided' do
it 'raises an ArgumentError' do
expect do
described_class.new([unicorns.take(5), {}], type: unicorn_class)
end.to raise_error(ArgumentError)
end
end
end

describe '#each' do
context 'if there is more than one page' do
it 'transparently iterates over all of them' do
Expand Down
8 changes: 8 additions & 0 deletions spec/asana/resources/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@

include ResourcesHelper

describe '#initialize' do
it 'raises an ArgumentError when required fields are missing' do
expect do
described_class.new(wait: 0).take(3)
end.to raise_error(ArgumentError)
end
end

it 'is an infinite collection of events on a resource' do
api.on(:get, '/events', resource: '1') do |response|
response.body = { sync: 'firstsynctoken',
Expand Down