diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..60e7481 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,33 @@ +require: + - rubocop-performance + - rubocop-rspec + +AllCops: + NewCops: enable + +Gemspec/RequiredRubyVersion: + Enabled: false + +Layout/LineLength: + Max: 240 + +RSpec/ExampleLength: + Max: 15 + +RSpec/NamedSubject: + Enabled: false + +RSpec/InstanceVariable: + Enabled: false + +RSpec/NestedGroups: + Enabled: false + +RSpec/MultipleExpectations: + Enabled: false + +Metrics/BlockLength: + Enabled: false + +Style/Documentation: + Enabled: false \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a9a5895..d514aa8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,8 @@ rvm: - 2.3.3 - 2.4.2 - 2.5.0 + - 2.6.6 + - 2.7.3 matrix: fast_finish: true diff --git a/Gemfile b/Gemfile index 817f62a..b22b6ff 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,12 @@ +# frozen_string_literal: true + source 'http://rubygems.org' + +group :development do + gem 'rubocop' + gem 'rubocop-performance' + gem 'rubocop-rake' + gem 'rubocop-rspec' +end + gemspec diff --git a/Guardfile b/Guardfile index b9879c2..66634f7 100644 --- a/Guardfile +++ b/Guardfile @@ -1,5 +1,7 @@ +# frozen_string_literal: true + guard :rspec, cmd: 'bundle exec rspec' do - watch(/^spec\/.+_spec\.rb$/) - watch(/^lib\/(.+)\.rb$/) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { :rspec } end diff --git a/Rakefile b/Rakefile index 2922906..25864e6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'bundler/gem_tasks' require 'rspec/core/rake_task' diff --git a/dribbble.gemspec b/dribbble.gemspec index d2173d4..05c820f 100644 --- a/dribbble.gemspec +++ b/dribbble.gemspec @@ -1,4 +1,6 @@ -require File.expand_path('../lib/dribbble/version', __FILE__) +# frozen_string_literal: true + +require File.expand_path('lib/dribbble/version', __dir__) Gem::Specification.new do |s| s.name = 'dribbble' @@ -17,9 +19,9 @@ Gem::Specification.new do |s| s.required_ruby_version = ['>= 2.2.0', '< 3.0'] s.add_runtime_dependency 'rest-client', '~> 2.0' + s.add_development_dependency 'guard-rspec', '~> 4.3' s.add_development_dependency 'rake', '~> 10.3' s.add_development_dependency 'rspec', '~> 2.14' - s.add_development_dependency 'guard-rspec', '~> 4.3' s.add_development_dependency 'sinatra', '~> 1.4' s.add_development_dependency 'webmock', '~> 2.3' end diff --git a/lib/dribbble.rb b/lib/dribbble.rb index ee618fe..3e69d10 100644 --- a/lib/dribbble.rb +++ b/lib/dribbble.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble API_URI = ENV.fetch('DRIBBBLE_API_URI', 'https://api.dribbble.com/v2') end diff --git a/lib/dribbble/attachment.rb b/lib/dribbble/attachment.rb index 53feaaf..fc093b4 100644 --- a/lib/dribbble/attachment.rb +++ b/lib/dribbble/attachment.rb @@ -1,7 +1,15 @@ +# frozen_string_literal: true + +require 'dribbble/utils/creatable' +require 'dribbble/utils/deletable' + module Dribbble class Attachment < Dribbble::Base + include Dribbble::Utils::Creatable + include Dribbble::Utils::Deletable + def self.available_fields - %i(file) + %i[file] end end end diff --git a/lib/dribbble/base.rb b/lib/dribbble/base.rb index 45ee01b..6ffabcb 100644 --- a/lib/dribbble/base.rb +++ b/lib/dribbble/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'dribbble/utils' require 'dribbble/utils/has_children' @@ -17,7 +19,7 @@ def initialize(token, json, dribbble_url = '') @dribbble_url = build_dribbble_url(@raw['id'].to_s, dribbble_url) @raw.each do |k, _v| - define_singleton_method(k) { @raw[k] } unless self.respond_to?(k) + define_singleton_method(k) { @raw[k] } unless respond_to?(k) end end diff --git a/lib/dribbble/bucket.rb b/lib/dribbble/bucket.rb index e3bac88..62c018b 100644 --- a/lib/dribbble/bucket.rb +++ b/lib/dribbble/bucket.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'dribbble/utils/findable' require 'dribbble/utils/creatable' require 'dribbble/utils/updatable' @@ -17,17 +19,17 @@ def add_shot(shot) res = html_put("/buckets/#{id}/shots") do |payload| payload[:shot_id] = shot_id end - res.code == 204 ? true : false + res.code == 204 end def remove_shot(shot) shot_id = shot.is_a?(Dribbble::Shot) ? shot.id : shot res = html_delete "/buckets/#{id}/shots", shot_id: shot_id - res.code == 204 ? true : false + res.code == 204 end def self.available_fields - %i(name description) + %i[name description] end end end diff --git a/lib/dribbble/client.rb b/lib/dribbble/client.rb index 2d72df6..e7efacb 100644 --- a/lib/dribbble/client.rb +++ b/lib/dribbble/client.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'dribbble/base' require 'dribbble/shot' require 'dribbble/user' @@ -16,7 +18,8 @@ class Client < Dribbble::Base def initialize(token = nil) token = token.is_a?(Hash) ? token[:token] : token @token = token - fail Dribbble::Error::MissingToken if @token.nil? + super(token, {}) + raise Dribbble::Error::MissingToken if @token.nil? end # Get authenticated user's buckets diff --git a/lib/dribbble/comment.rb b/lib/dribbble/comment.rb index a63286c..69e7e63 100644 --- a/lib/dribbble/comment.rb +++ b/lib/dribbble/comment.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module Dribbble class Comment < Dribbble::Base def self.available_fields - %i(body) + %i[body] end def likes @@ -18,12 +20,12 @@ def like? def like! res = html_post "#{dribbble_url}/like" - res.code == 201 ? true : false + res.code == 201 end def unlike! res = html_delete "#{dribbble_url}/like" - res.code == 204 ? true : false + res.code == 204 end end end diff --git a/lib/dribbble/errors.rb b/lib/dribbble/errors.rb index c7e0578..988b6e8 100644 --- a/lib/dribbble/errors.rb +++ b/lib/dribbble/errors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble module Error ISSUES_URL = 'https://github.com/Calyhre/dribbble/issues/new' @@ -5,7 +7,7 @@ module Error # Standard error we will inherit class Standard < StandardError def initialize(message = nil) - if message && message.response + if message&.response super message.response else super(message || self.message) diff --git a/lib/dribbble/like.rb b/lib/dribbble/like.rb index 6709624..25a2417 100644 --- a/lib/dribbble/like.rb +++ b/lib/dribbble/like.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble class Like < Dribbble::Base def user diff --git a/lib/dribbble/project.rb b/lib/dribbble/project.rb index b21e9e7..def9df2 100644 --- a/lib/dribbble/project.rb +++ b/lib/dribbble/project.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'dribbble/utils/findable' module Dribbble diff --git a/lib/dribbble/shot.rb b/lib/dribbble/shot.rb index fa68d3d..57f68dd 100644 --- a/lib/dribbble/shot.rb +++ b/lib/dribbble/shot.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'dribbble/utils/findable' require 'dribbble/utils/creatable' require 'dribbble/utils/updatable' @@ -17,7 +19,7 @@ class Shot < Dribbble::Base has_many :rebounds, as: Dribbble::Shot def self.available_fields - %i(title image description tags team_id rebound_source_id low_profile) + %i[title image description tags team_id rebound_source_id low_profile] end def self.after_create(res) @@ -33,12 +35,12 @@ def like? def like! res = html_post "/shots/#{id}/like" - res.code == 201 ? true : false + res.code == 201 end def unlike! res = html_delete "/shots/#{id}/like" - res.code == 204 ? true : false + res.code == 204 end def rebounds(attrs = {}) diff --git a/lib/dribbble/team.rb b/lib/dribbble/team.rb index b3517e1..09540d2 100644 --- a/lib/dribbble/team.rb +++ b/lib/dribbble/team.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble class Team < Dribbble::Base has_many :members, as: Dribbble::User diff --git a/lib/dribbble/user.rb b/lib/dribbble/user.rb index a61779c..dbe5b00 100644 --- a/lib/dribbble/user.rb +++ b/lib/dribbble/user.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'dribbble/utils/findable' module Dribbble @@ -22,12 +24,12 @@ def following?(other_user_id = nil) def follow! res = html_put "/users/#{id}/follow" - res.code == 204 ? true : false + res.code == 204 end def unfollow! res = html_delete "/users/#{id}/follow" - res.code == 204 ? true : false + res.code == 204 end end end diff --git a/lib/dribbble/utils.rb b/lib/dribbble/utils.rb index 21fa17b..7310abd 100644 --- a/lib/dribbble/utils.rb +++ b/lib/dribbble/utils.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'uri' module Dribbble @@ -5,14 +7,14 @@ module Utils DEFAULT_ATTRIBUTES = { page: 1, per_page: 100 - } + }.freeze def class_name - @_class_name ||= self.is_a?(Class) ? name.split('::').last.downcase : self.class.name.split('::').last.downcase + @class_name ||= is_a?(Class) ? name.split('::').last.downcase : self.class.name.split('::').last.downcase end def pluralized_class_name - @_pluralized_class_name ||= "#{class_name}s" + @pluralized_class_name ||= "#{class_name}s" end def full_url(path, attrs = {}) diff --git a/lib/dribbble/utils/creatable.rb b/lib/dribbble/utils/creatable.rb index 3b127df..d679167 100644 --- a/lib/dribbble/utils/creatable.rb +++ b/lib/dribbble/utils/creatable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble module Utils module Creatable @@ -17,7 +19,7 @@ def api_endpoint # Need to be redeclared in the model def available_fields - fail "You need to redeclare this methods in your model" + raise 'You need to redeclare this methods in your model' end def after_create(res) diff --git a/lib/dribbble/utils/deletable.rb b/lib/dribbble/utils/deletable.rb index c519e15..316f796 100644 --- a/lib/dribbble/utils/deletable.rb +++ b/lib/dribbble/utils/deletable.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + module Dribbble module Utils module Deletable def delete res = html_delete "/#{self.class.api_endpoint}/#{id}" - res.code == 204 ? true : false + res.code == 204 end module ClassMethods diff --git a/lib/dribbble/utils/findable.rb b/lib/dribbble/utils/findable.rb index 88a53bc..96547bb 100644 --- a/lib/dribbble/utils/findable.rb +++ b/lib/dribbble/utils/findable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble module Utils module Findable diff --git a/lib/dribbble/utils/has_children.rb b/lib/dribbble/utils/has_children.rb index f024e33..f197953 100644 --- a/lib/dribbble/utils/has_children.rb +++ b/lib/dribbble/utils/has_children.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + module Dribbble module Utils module HasChildren module ClassMethods - def has_many(*fields) + def has_many(*fields) # rubocop:disable Naming/PredicateName if fields[1].is_a? Hash generate_methods fields[0], fields[1][:as], fields[1][:key] else @@ -12,6 +14,7 @@ def has_many(*fields) end end + # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity def generate_methods(field, klass = nil, key = nil) singularized_field = field[0...-1] @@ -31,8 +34,8 @@ def generate_methods(field, klass = nil, key = nil) klass ||= Object.const_get "Dribbble::#{singularized_field.capitalize}" url = "/#{pluralized_class_name}/#{id}/#{field}" res = html_post url do |payload| - klass.available_fields.each do |field| - payload[field] = attrs[field] + klass.available_fields.each do |available_field| + payload[available_field] = attrs[available_field] end end case res.code @@ -49,8 +52,8 @@ def generate_methods(field, klass = nil, key = nil) klass ||= Object.const_get "Dribbble::#{__method__[0...-1].capitalize}" url = "/#{pluralized_class_name}/#{id}/#{klass.pluralized_class_name}/#{child_id}" res = html_put url do |payload| - klass.available_fields.each do |field| - payload[field] = attrs[field] + klass.available_fields.each do |available_field| + payload[available_field] = attrs[available_field] end end klass.new token, res, url @@ -59,10 +62,11 @@ def generate_methods(field, klass = nil, key = nil) define_method "delete_#{singularized_field}" do |child_id| klass ||= Object.const_get "Dribbble::#{__method__[0...-1].capitalize}" res = html_delete "/#{pluralized_class_name}/#{id}/#{klass.pluralized_class_name}/#{child_id}" - res.code == 204 ? true : false + res.code == 204 end end end + # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity def self.included(base) base.extend(ClassMethods) diff --git a/lib/dribbble/utils/updatable.rb b/lib/dribbble/utils/updatable.rb index dcf5ab5..5e448a0 100644 --- a/lib/dribbble/utils/updatable.rb +++ b/lib/dribbble/utils/updatable.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Dribbble module Utils module Updatable diff --git a/lib/dribbble/version.rb b/lib/dribbble/version.rb index 091a2e7..0a21004 100644 --- a/lib/dribbble/version.rb +++ b/lib/dribbble/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Dribbble Version module Dribbble VERSION = '1.2.0' diff --git a/spec/lib/dribbble/base_spec.rb b/spec/lib/dribbble/base_spec.rb index 3cbf1a6..bc0b267 100644 --- a/spec/lib/dribbble/base_spec.rb +++ b/spec/lib/dribbble/base_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require 'spec_helper' -describe Dribbble::Client do - before :all do - @base = Dribbble::Base.new 'valid_token', {} +describe Dribbble::Base do + before do + @base = described_class.new 'valid_token', {} end describe 'on #full_url_with_default_params' do diff --git a/spec/lib/dribbble/bucket_spec.rb b/spec/lib/dribbble/bucket_spec.rb index d04b9bf..9781716 100644 --- a/spec/lib/dribbble/bucket_spec.rb +++ b/spec/lib/dribbble/bucket_spec.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' RAW_BUCKET = data_from_json 'bucket_success.json' describe Dribbble::Bucket do describe 'on instance' do - before :all do - @bucket = Dribbble::Bucket.new 'valid_token', RAW_BUCKET + before do + @bucket = described_class.new 'valid_token', RAW_BUCKET end describe 'after initialization' do RAW_BUCKET.each do |field, value| it "respond to #{field}" do - expect(@bucket.send field).to eq(value) + expect(@bucket.send(field)).to eq(value) end end end @@ -85,11 +87,11 @@ name: 'Bucket title', description: 'Bucket description' } - Dribbble::Bucket.create 'valid_token', bucket + described_class.create 'valid_token', bucket end it 'create the shot' do - expect(subject).to be_a Dribbble::Bucket + expect(subject).to be_a described_class end end @@ -101,7 +103,7 @@ name: 'Bucket title', description: 'Bucket description' } - Dribbble::Bucket.update 'valid_token', 2754, bucket + described_class.update 'valid_token', 2754, bucket end it 'update bucket' do @@ -113,7 +115,7 @@ subject do stub_dribbble :get, '/buckets/2754', DribbbleAPI::BucketSuccess stub_dribbble :delete, '/buckets/2754', DribbbleAPI::BucketDeleted - Dribbble::Bucket.delete 'valid_token', 2754 + described_class.delete 'valid_token', 2754 end it 'return true' do @@ -124,11 +126,11 @@ describe 'on #find' do subject do stub_dribbble :get, '/buckets/2754', DribbbleAPI::BucketSuccess - Dribbble::Bucket.find 'valid_token', 2754 + described_class.find 'valid_token', 2754 end it 'return a bucket' do - expect(subject).to be_a Dribbble::Bucket + expect(subject).to be_a described_class expect(subject.id).to eq(2754) end end diff --git a/spec/lib/dribbble/client_spec.rb b/spec/lib/dribbble/client_spec.rb index dd91425..a77dc0d 100644 --- a/spec/lib/dribbble/client_spec.rb +++ b/spec/lib/dribbble/client_spec.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + require 'spec_helper' describe Dribbble::Client do - before :all do - @client = Dribbble::Client.new 'valid_token' + before do + @client = described_class.new 'valid_token' end describe 'without token' do it 'raises Dribbble::Error::MissingToken' do expect do - Dribbble::Client.new + described_class.new end.to raise_error(Dribbble::Error::MissingToken) end end @@ -17,7 +19,7 @@ describe 'with an invalid token' do subject do stub_dribbble :get, '/user', DribbbleAPI::Unauthorized - Dribbble::Client.new(token: 'fake_invalid_token') + described_class.new(token: 'fake_invalid_token') end it 'raise Dribbble::Error::Unauthorized' do @@ -30,7 +32,7 @@ describe 'with a valid token' do subject do stub_dribbble :get, '/user', DribbbleAPI::UserSuccess - Dribbble::Client.new(token: 'valid_token') + described_class.new(token: 'valid_token') end it 'return a Dribbble::User' do @@ -38,6 +40,18 @@ expect(subject.user.name).to be_a String end end + + describe 'with current user' do + subject do + stub_dribbble :get, '/user', DribbbleAPI::CurrentUserSuccess + @client.user + end + + it 'return current user' do + expect(subject).to be_a Dribbble::User + expect(subject.id).to eq(8_008_135) + end + end end describe 'on #buckets' do @@ -116,16 +130,4 @@ expect(subject.first).to be_a Dribbble::Team end end - - describe 'on #user' do - subject do - stub_dribbble :get, '/user', DribbbleAPI::CurrentUserSuccess - @client.user - end - - it 'return current user' do - expect(subject).to be_a Dribbble::User - expect(subject.id).to eq(8_008_135) - end - end end diff --git a/spec/lib/dribbble/comment_spec.rb b/spec/lib/dribbble/comment_spec.rb index d7682db..1547c6b 100644 --- a/spec/lib/dribbble/comment_spec.rb +++ b/spec/lib/dribbble/comment_spec.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' RAW_COMMENT = data_from_json 'comment_success.json' describe Dribbble::Comment do describe 'on instance' do - before :all do - @comment = Dribbble::Comment.new 'valid_token', RAW_COMMENT, '/shots/471756/comments' + before do + @comment = described_class.new 'valid_token', RAW_COMMENT, '/shots/471756/comments' end describe 'after initialization' do RAW_COMMENT.each do |field, value| it "respond to #{field}" do - expect(@comment.send field).to eq(value) + expect(@comment.send(field)).to eq(value) end end end diff --git a/spec/lib/dribbble/project_spec.rb b/spec/lib/dribbble/project_spec.rb index 3e557ba..00470e9 100644 --- a/spec/lib/dribbble/project_spec.rb +++ b/spec/lib/dribbble/project_spec.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' RAW_PROJECT = data_from_json 'project_success.json' describe Dribbble::Project do describe 'on instance' do - before :all do - @project = Dribbble::Project.new 'valid_token', RAW_PROJECT + before do + @project = described_class.new 'valid_token', RAW_PROJECT end describe 'after initialization' do RAW_PROJECT.each do |field, value| it "respond to #{field}" do - expect(@project.send field).to eq(value) + expect(@project.send(field)).to eq(value) end end end @@ -33,11 +35,11 @@ describe 'on #find' do subject do stub_dribbble :get, '/projects/3', DribbbleAPI::ProjectSuccess - Dribbble::Project.find 'valid_token', 3 + described_class.find 'valid_token', 3 end it 'return a project' do - expect(subject).to be_a Dribbble::Project + expect(subject).to be_a described_class expect(subject.id).to eq(3) end end diff --git a/spec/lib/dribbble/shot_spec.rb b/spec/lib/dribbble/shot_spec.rb index a342a76..c87440d 100644 --- a/spec/lib/dribbble/shot_spec.rb +++ b/spec/lib/dribbble/shot_spec.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' RAW_SHOT = data_from_json 'shot_success.json' describe Dribbble::Shot do describe 'on instance' do - before :all do - @shot = Dribbble::Shot.new 'valid_token', RAW_SHOT + before do + @shot = described_class.new 'valid_token', RAW_SHOT end describe 'after initialization' do RAW_SHOT.each do |field, value| it "respond to #{field}" do - expect(@shot.send field).to eq(value) + expect(@shot.send(field)).to eq(value) end end end @@ -152,12 +154,12 @@ describe 'update' do subject do stub_dribbble :put, '/shots/471756/comments/1145736', DribbbleAPI::CommentUpdated - @shot.update_comment 1_145_736, body: "New body" + @shot.update_comment 1_145_736, body: 'New body' end it 'return a comment' do expect(subject).to be_a Dribbble::Comment - expect(subject.body).to eq("New body") + expect(subject.body).to eq('New body') end end @@ -249,7 +251,7 @@ end it 'return a list of shots' do - expect(subject.first).to be_a Dribbble::Shot + expect(subject.first).to be_a described_class end end end @@ -262,9 +264,9 @@ title: 'Shot title', desciption: 'Shot description', image: File.new("#{Dir.pwd}/spec/support/fixtures/image.jpg", 'rb'), - tags: %w(tag1 tag2) + tags: %w[tag1 tag2] } - Dribbble::Shot.create 'valid_token', shot + described_class.create 'valid_token', shot end it 'create the shot' do @@ -281,7 +283,7 @@ name: 'Shot title', description: 'Shot description' } - Dribbble::Shot.update 'valid_token', 471_756, bucket + described_class.update 'valid_token', 471_756, bucket end it 'update bucket' do @@ -293,7 +295,7 @@ subject do stub_dribbble :get, '/shots/471756', DribbbleAPI::ShotSuccess stub_dribbble :delete, '/shots/471756', DribbbleAPI::ShotDeleted - Dribbble::Shot.delete 'valid_token', 471_756 + described_class.delete 'valid_token', 471_756 end it 'return true' do @@ -304,11 +306,11 @@ describe 'on #find' do subject do stub_dribbble :get, '/shots/471756', DribbbleAPI::ShotSuccess - Dribbble::Shot.find 'valid_token', 471_756 + described_class.find 'valid_token', 471_756 end it 'return a shot' do - expect(subject).to be_a Dribbble::Shot + expect(subject).to be_a described_class expect(subject.id).to eq(471_756) end end diff --git a/spec/lib/dribbble/team_spec.rb b/spec/lib/dribbble/team_spec.rb index 9fa1983..29615f8 100644 --- a/spec/lib/dribbble/team_spec.rb +++ b/spec/lib/dribbble/team_spec.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' RAW_TEAM = data_from_json 'team_success.json' describe Dribbble::Team do describe 'on instance' do - before :all do - @team = Dribbble::Team.new 'valid_token', RAW_TEAM, '/teams/39' + before do + @team = described_class.new 'valid_token', RAW_TEAM, '/teams/39' end describe 'after initialization' do RAW_TEAM.each do |field, value| it "respond to #{field}" do - expect(@team.send field).to eq(value) + expect(@team.send(field)).to eq(value) end end end diff --git a/spec/lib/dribbble/user_spec.rb b/spec/lib/dribbble/user_spec.rb index c8b031f..8361ec2 100644 --- a/spec/lib/dribbble/user_spec.rb +++ b/spec/lib/dribbble/user_spec.rb @@ -1,17 +1,19 @@ +# frozen_string_literal: true + require 'spec_helper' RAW_USER = data_from_json 'user_success.json' describe Dribbble::User do describe 'on instance' do - before :all do - @user = Dribbble::User.new 'valid_token', RAW_USER + before do + @user = described_class.new 'valid_token', RAW_USER end describe 'after initialization' do RAW_USER.each do |field, value| it "respond to #{field}" do - expect(@user.send field).to eq(value) + expect(@user.send(field)).to eq(value) end end end @@ -36,7 +38,7 @@ it 'responds with users' do expect(subject.size).to eq 1 - expect(subject.first).to be_a Dribbble::User + expect(subject.first).to be_a described_class expect(subject.first.location).to eq 'Salem, MA' end end @@ -49,7 +51,7 @@ it 'responds with users' do expect(subject.size).to eq 1 - expect(subject.first).to be_a Dribbble::User + expect(subject.first).to be_a described_class expect(subject.first.name).to eq('Dan Cederholm') end end @@ -180,11 +182,11 @@ describe 'on #find' do subject do stub_dribbble :get, '/users/483195', DribbbleAPI::UserSuccess - Dribbble::User.find 'valid_token', 483_195 + described_class.find 'valid_token', 483_195 end it 'return a user' do - expect(subject).to be_a Dribbble::User + expect(subject).to be_a described_class expect(subject.id).to eq(483_195) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d9f4805..952cfe9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,18 +1,20 @@ +# frozen_string_literal: true + require 'dribbble' require 'webmock/rspec' -Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f } +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f } # WebMock.disable_net_connect! allow_localhost: true, allow: 'codeclimate.com' RSpec.configure do |config| - config.before :all do + config.before do WebMock.reset! end end def stub_dribbble(method, path, response_class) - url = /api.dribbble.com\/v2#{Regexp.escape path}(\?.*)?$/ + url = %r{api.dribbble.com/v2#{Regexp.escape path}(\?.*)?$} stub_request(method, url).to_rack(response_class) end diff --git a/spec/support/dribbble_api.rb b/spec/support/dribbble_api.rb index cb33285..8fb0c41 100644 --- a/spec/support/dribbble_api.rb +++ b/spec/support/dribbble_api.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + require 'sinatra/base' class String def underscore - self.gsub(/::/, '/'). - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - tr("-", "_"). - downcase + gsub(/::/, '/') + .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + .gsub(/([a-z\d])([A-Z])/, '\1_\2') + .tr('-', '_') + .downcase end end @@ -105,9 +107,9 @@ def json_response headers response_headers { - message: "Validation failed.", + message: 'Validation failed.', errors: [ - { attribute: "user", message: "reached the daily limit of 5 shots" } + { attribute: 'user', message: 'reached the daily limit of 5 shots' } ] }.to_json end @@ -229,6 +231,9 @@ class ShotLikeDeleted < Deleted class ShotLikesSuccess < Found end + class ShotsSuccess < Found + end + class TeamsSuccess < Found end diff --git a/spec/support/fixtures/shots_success.json b/spec/support/fixtures/shots_success.json new file mode 100644 index 0000000..608579a --- /dev/null +++ b/spec/support/fixtures/shots_success.json @@ -0,0 +1,150 @@ +[ + { + "id" : 471756, + "title" : "Sasquatch", + "description" : "

Quick, messy, five minute sketch of something that might become a fictional something.

", + "width" : 400, + "height" : 300, + "images" : { + "hidpi" : null, + "normal" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/471756/sasquatch.png", + "teaser" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/471756/sasquatch_teaser.png" + }, + "published_at" : "2012-03-15T01:52:33Z", + "updated_at" : "2012-03-15T02:12:57Z", + "html_url" : "https://dribbble.com/shots/471756-Sasquatch", + "animated" : false, + "tags" : [ + "fiction", + "sasquatch", + "sketch", + "wip" + ], + "attachments" : [ + { + "id" : 206165, + "url" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/1412410/attachments/206165/weathered-ball-detail.jpg", + "thumbnail_url" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/1412410/attachments/206165/thumbnail/weathered-ball-detail.jpg", + "size" : 116375, + "content_type" : "image/jpeg", + "created_at" : "2014-02-07T16:35:09Z" + } + ], + "projects" : [ + { + "id" : 3, + "name" : "Web Standards Sherpa", + "description" : "I did visual design and art direction for this project, working with the Web Standards Project and Microsoft.", + "shots_count" : 4, + "created_at" : "2011-04-14T03:43:47Z", + "updated_at" : "2012-04-04T22:39:53Z" + } + ], + "team" : { + "id" : 39, + "name" : "Dribbble", + "login" : "dribbble", + "html_url" : "https://dribbble.com/dribbble", + "avatar_url" : "https://d13yacurqjgara.cloudfront.net/users/39/avatars/normal/apple-flat-precomposed.png?1388527574", + "bio" : "Show and tell for designers. This is Dribbble on Dribbble.", + "location" : "Salem, MA", + "links" : { + "web" : "http://dribbble.com", + "twitter" : "https://twitter.com/dribbble" + }, + "type" : "Team", + "created_at" : "2009-08-18T18:34:31Z", + "updated_at" : "2014-02-14T22:32:11Z" + }, + "video" : { + "id" : 10542, + "duration" : 17, + "video_file_name" : "ratatouille.webm", + "video_file_size" : 12173472, + "width" : 1600, + "height" : 1200, + "silent" : true, + "created_at" : "2019-01-28T15:53:23Z", + "updated_at" : "2019-01-28T15:56:04Z", + "url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille.webm", + "small_preview_url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille_small_preview.webm", + "large_preview_url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille_large_preview.webm", + "xlarge_preview_url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille_xlarge_preview.webm" + }, + "low_profile" : false + }, + { + "id" : 471756, + "title" : "Sasquatch", + "description" : "

Quick, messy, five minute sketch of something that might become a fictional something.

", + "width" : 400, + "height" : 300, + "images" : { + "hidpi" : null, + "normal" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/471756/sasquatch.png", + "teaser" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/471756/sasquatch_teaser.png" + }, + "published_at" : "2012-03-15T01:52:33Z", + "updated_at" : "2012-03-15T02:12:57Z", + "html_url" : "https://dribbble.com/shots/471756-Sasquatch", + "animated" : false, + "tags" : [ + "fiction", + "sasquatch", + "sketch", + "wip" + ], + "attachments" : [ + { + "id" : 206165, + "url" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/1412410/attachments/206165/weathered-ball-detail.jpg", + "thumbnail_url" : "https://d13yacurqjgara.cloudfront.net/users/1/screenshots/1412410/attachments/206165/thumbnail/weathered-ball-detail.jpg", + "size" : 116375, + "content_type" : "image/jpeg", + "created_at" : "2014-02-07T16:35:09Z" + } + ], + "projects" : [ + { + "id" : 3, + "name" : "Web Standards Sherpa", + "description" : "I did visual design and art direction for this project, working with the Web Standards Project and Microsoft.", + "shots_count" : 4, + "created_at" : "2011-04-14T03:43:47Z", + "updated_at" : "2012-04-04T22:39:53Z" + } + ], + "team" : { + "id" : 39, + "name" : "Dribbble", + "login" : "dribbble", + "html_url" : "https://dribbble.com/dribbble", + "avatar_url" : "https://d13yacurqjgara.cloudfront.net/users/39/avatars/normal/apple-flat-precomposed.png?1388527574", + "bio" : "Show and tell for designers. This is Dribbble on Dribbble.", + "location" : "Salem, MA", + "links" : { + "web" : "http://dribbble.com", + "twitter" : "https://twitter.com/dribbble" + }, + "type" : "Team", + "created_at" : "2009-08-18T18:34:31Z", + "updated_at" : "2014-02-14T22:32:11Z" + }, + "video" : { + "id" : 10542, + "duration" : 17, + "video_file_name" : "ratatouille.webm", + "video_file_size" : 12173472, + "width" : 1600, + "height" : 1200, + "silent" : true, + "created_at" : "2019-01-28T15:53:23Z", + "updated_at" : "2019-01-28T15:56:04Z", + "url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille.webm", + "small_preview_url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille_small_preview.webm", + "large_preview_url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille_large_preview.webm", + "xlarge_preview_url" : "https://cdn.dribbble.com/users/1/videos/10542/ratatouille_xlarge_preview.webm" + }, + "low_profile" : false + } +] \ No newline at end of file