Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
Do some of the easier CC fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Smith committed Oct 29, 2018
1 parent 496be2d commit 42bcea5
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 81 deletions.
6 changes: 5 additions & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ plugins:
reek:
enabled: true
rubocop:
enabled: true
enabled: true
exclude_patterns:
- "Gemfile"
- "ubw-client.gemspec"
- "spec/"
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rvm:
before_install: gem install bundler -v 1.16.4
env:
global:
- CC_TEST_REPORTER_ID=b07859ceb064fed550f1ce8eb913053ad9b99271a9a0837bf94491262516214e
- CC_TEST_REPORTER_ID=7dd95c30dd9787ebbe9155089f3b127c465e512c454f4618c114ab27d3cb4fd9
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 TODO: Write your name
Copyright (c) 2018 The Sanger Institue

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
31 changes: 16 additions & 15 deletions lib/ubw/client.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
# Version
require "ubw/client/version"
require 'ubw/client/version'

# Connection
require "ubw/connection"
require "ubw/middleware/status"
require 'ubw/connection'
require 'ubw/middleware/status'

# Util
require "ubw/util"
require 'ubw/util'

# Queries
require "ubw/queries/find"
require "ubw/queries/where"
require "ubw/queries/all"
require 'ubw/queries/find'
require 'ubw/queries/where'
require 'ubw/queries/all'

# Resources
require "ubw/resource"
require "ubw/price"
require "ubw/sub_project"
require "ubw/result_set"
require 'ubw/resource'
require 'ubw/price'
require 'ubw/sub_project'
require 'ubw/result_set'

# Errors
require "ubw/errors"
require 'ubw/errors'

module Ubw
module Client

@site = ENV['UBW_SITE']

class << self
attr_accessor :site

def connection(params = {})
@connection ||= Ubw::Connection.new(params.merge(url: site)) do |connection|
yield connection if block_given?
connection_params = params.merge(url: site)

@connection ||= Ubw::Connection.new(connection_params) do |conn|
yield conn if block_given?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ubw/client/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Ubw
module Client
VERSION = "0.1.0"
VERSION = "0.1.0".freeze
end
end
5 changes: 2 additions & 3 deletions lib/ubw/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ def initialize(options)

@faraday = Faraday.new(options) do |connection|
connection.request :json
connection.response :json, :content_type => /\bjson$/
connection.response :json, content_type: /\bjson$/
connection.use Ubw::Middleware::Status

yield connection if block_given?

connection.adapter Faraday.default_adapter
end
end

end
end
15 changes: 9 additions & 6 deletions lib/ubw/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,41 @@ class BadRequest < ClientError
class ConnectionError < ApiError
end


class ServerError < ApiError
def message
"Internal server error"
'Internal server error'
end
end

class Conflict < ServerError
def message
"Resource already exists"
'Resource already exists'
end
end

class NotFound < ServerError
attr_reader :uri

def initialize(uri)
@uri = uri
end

def message
"Couldn't find resource at: #{uri.to_s}"
"Couldn't find resource at: #{uri}"
end
end

class UnexpectedStatus < ServerError
attr_reader :code, :uri

def initialize(code, uri)
@code = code
@uri = uri
end

def message
"Unexpected response status: #{code} from: #{uri.to_s}"
"Unexpected response status: #{code} from: #{uri}"
end
end
end
end
end
37 changes: 19 additions & 18 deletions lib/ubw/middleware/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ def call(environment)

def handle_status(code, env)
case code
when 200..399
when 401
raise Errors::NotAuthorized, env
when 403
raise Errors::AccessDenied, env
when 404
raise Errors::NotFound, env[:url]
when 409
raise Errors::Conflict, env
when 422
raise Errors::UnprocessableEntity, env
when 400..499
raise Errors::BadRequest, env
when 500..599
raise Errors::ServerError, env
else
raise Errors::UnexpectedStatus.new(code, env[:url])
when 200..399
# Do Nothing
when 401
raise Errors::NotAuthorized, env
when 403
raise Errors::AccessDenied, env
when 404
raise Errors::NotFound, env[:url]
when 409
raise Errors::Conflict, env
when 422
raise Errors::UnprocessableEntity, env
when 400..499
raise Errors::BadRequest, env
when 500..599
raise Errors::ServerError, env
else
raise Errors::UnexpectedStatus.new(code, env[:url])
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ubw/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ class Price < Resource
include Ubw::Queries::All
@endpoint = '/prices'
end
end
end
4 changes: 1 addition & 3 deletions lib/ubw/queries/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ module Ubw
module Queries
module All
module ClassMethods

def all
Ubw::ResultSet.new(Ubw::Client.connection.get(endpoint), self)
end

end

def self.included(receiver)
receiver.extend ClassMethods
end
end
end
end
end
4 changes: 1 addition & 3 deletions lib/ubw/queries/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ module Ubw
module Queries
module Find
module ClassMethods

def find(id)
new(Ubw::Client.connection.get("#{endpoint}/#{id}"))
end

end

def self.included(receiver)
receiver.extend ClassMethods
end
end
end
end
end
12 changes: 5 additions & 7 deletions lib/ubw/queries/where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@ module Ubw
module Queries
module Where
module ClassMethods

# params are a Hash where each value can be a String or an Array
# params keys should be snake_cased but get changed to camelCase
#
# e.g. where(cost_code: "S0001", module_name: ["WGS", "Library Prep"])
def where(params)
formatted_params = params.keys.reduce({}) do |memo, key|
value = params[key].kind_of?(Array) ? params[key].join(',') : params[key]
memo[Ubw::Util.camelify(key).to_sym] = value
memo
formatted_params = params.keys.each_with_object({}) do |key, obj|
value = params[key].is_a?(Array) ? params[key].join(',') : params[key]
obj[Ubw::Util.camelify(key).to_sym] = value
obj
end

Ubw::ResultSet.new(Ubw::Client.connection.get(endpoint, formatted_params), self)
end

end

def self.included(receiver)
receiver.extend ClassMethods
end
end
end
end
end
20 changes: 9 additions & 11 deletions lib/ubw/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ module Ubw
# Good: Means gem doesn't have to change when the API does
# Bad: Any nonsense could be passed in
class Resource < OpenStruct

def self.endpoint
@endpoint
class << self
attr_reader :endpoint
end

def initialize(resource = nil)
super(format_keys(resource))
end

private
private

# JSON resources usually have camelCase field names
# Want the methods on our resources to be snake_case
def format_keys(resource)
resource.keys.reduce({}) do |memo, key|
resource.keys.each_with_object({}) do |key, obj|
value = resource[key]

formatted_key = Ubw::Util.snakify(key)
formatted_key << '?' if !!value == value # Add a ? to the key if value is a Boolean

memo[formatted_key] = value
# Add a ? to the key if value is a Boolean
formatted_key << '?' if !!value == value

memo
obj[formatted_key] = value
obj
end
end

end
end
end
8 changes: 7 additions & 1 deletion lib/ubw/result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ class ResultSet

def initialize(result_set, item_class)
@result_count = result_set.fetch(:resultCount)
@items = result_set.fetch(:items).map { |item| item_class.new(item) }
@items = create_items(result_set.fetch(:items), item_class)
end

def each
return items.each unless block_given?
items.each { |item| yield item }
end

private

def create_items(result_set_items, item_class)
result_set_items.map { |item| item_class.new(item) }
end

end
end
2 changes: 1 addition & 1 deletion lib/ubw/sub_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ class SubProject < Resource
include Ubw::Queries::All
@endpoint = '/subprojects'
end
end
end
8 changes: 3 additions & 5 deletions lib/ubw/util.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module Ubw
class Util

# "costCode" => "cost_code"
def self.snakify(str)
str.to_s.each_char.with_index.reduce("") do |s, args|
str.to_s.each_char.with_index.reduce('') do |s, args|
char, index = args
if /[[:upper:]]/.match(char)
s << "_" unless index == 0
s << '_' unless index.zero?
s << char.downcase
else
s << char
Expand All @@ -28,6 +27,5 @@ def self.camelify(str)
end
new_str
end

end
end
end

0 comments on commit 42bcea5

Please sign in to comment.