Skip to content

Commit

Permalink
fix: specs + syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocksy committed Apr 29, 2021
1 parent 2467378 commit 992995a
Show file tree
Hide file tree
Showing 36 changed files with 370 additions and 99 deletions.
33 changes: 33 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ rvm:
- 2.3.3
- 2.4.2
- 2.5.0
- 2.6.6
- 2.7.3

matrix:
fast_finish: true
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

Expand Down
6 changes: 4 additions & 2 deletions dribbble.gemspec
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
2 changes: 2 additions & 0 deletions lib/dribbble.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dribbble
API_URI = ENV.fetch('DRIBBBLE_API_URI', 'https://api.dribbble.com/v2')
end
Expand Down
10 changes: 9 additions & 1 deletion lib/dribbble/attachment.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/dribbble/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'dribbble/utils'
require 'dribbble/utils/has_children'

Expand All @@ -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

Expand Down
8 changes: 5 additions & 3 deletions lib/dribbble/bucket.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'dribbble/utils/findable'
require 'dribbble/utils/creatable'
require 'dribbble/utils/updatable'
Expand All @@ -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
5 changes: 4 additions & 1 deletion lib/dribbble/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'dribbble/base'
require 'dribbble/shot'
require 'dribbble/user'
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/dribbble/comment.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion lib/dribbble/errors.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

module Dribbble
module Error
ISSUES_URL = 'https://github.com/Calyhre/dribbble/issues/new'

# 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)
Expand Down
2 changes: 2 additions & 0 deletions lib/dribbble/like.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dribbble
class Like < Dribbble::Base
def user
Expand Down
2 changes: 2 additions & 0 deletions lib/dribbble/project.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'dribbble/utils/findable'

module Dribbble
Expand Down
8 changes: 5 additions & 3 deletions lib/dribbble/shot.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'dribbble/utils/findable'
require 'dribbble/utils/creatable'
require 'dribbble/utils/updatable'
Expand All @@ -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)
Expand All @@ -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 = {})
Expand Down
2 changes: 2 additions & 0 deletions lib/dribbble/team.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dribbble
class Team < Dribbble::Base
has_many :members, as: Dribbble::User
Expand Down
6 changes: 4 additions & 2 deletions lib/dribbble/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'dribbble/utils/findable'

module Dribbble
Expand All @@ -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
8 changes: 5 additions & 3 deletions lib/dribbble/utils.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# frozen_string_literal: true

require 'uri'

module Dribbble
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 = {})
Expand Down
4 changes: 3 additions & 1 deletion lib/dribbble/utils/creatable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dribbble
module Utils
module Creatable
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/dribbble/utils/deletable.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/dribbble/utils/findable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Dribbble
module Utils
module Findable
Expand Down
16 changes: 10 additions & 6 deletions lib/dribbble/utils/has_children.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 992995a

Please sign in to comment.