diff --git a/.codeclimate.yml b/.codeclimate.yml index cf96041..f70091f 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -6,4 +6,8 @@ plugins: reek: enabled: true rubocop: - enabled: true \ No newline at end of file + enabled: true +exclude_patterns: + - "Gemfile" + - "ubw-client.gemspec" + - "spec/" \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 498412d..ff3b4b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/LICENSE.txt b/LICENSE.txt index a2171de..3423a8d 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -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 diff --git a/Rakefile b/Rakefile index b7e9ed5..4c774a2 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/ubw/client.rb b/lib/ubw/client.rb index 3b71424..e841361 100644 --- a/lib/ubw/client.rb +++ b/lib/ubw/client.rb @@ -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 diff --git a/lib/ubw/client/version.rb b/lib/ubw/client/version.rb index 1c1850a..8bf2fb7 100644 --- a/lib/ubw/client/version.rb +++ b/lib/ubw/client/version.rb @@ -1,5 +1,5 @@ module Ubw module Client - VERSION = "0.1.0" + VERSION = "0.1.0".freeze end end diff --git a/lib/ubw/connection.rb b/lib/ubw/connection.rb index 67a93c3..864af63 100644 --- a/lib/ubw/connection.rb +++ b/lib/ubw/connection.rb @@ -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 diff --git a/lib/ubw/errors.rb b/lib/ubw/errors.rb index 0e3e221..ee55d9c 100644 --- a/lib/ubw/errors.rb +++ b/lib/ubw/errors.rb @@ -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 \ No newline at end of file +end diff --git a/lib/ubw/middleware/status.rb b/lib/ubw/middleware/status.rb index c8cfe9f..0037d70 100644 --- a/lib/ubw/middleware/status.rb +++ b/lib/ubw/middleware/status.rb @@ -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 \ No newline at end of file +end diff --git a/lib/ubw/price.rb b/lib/ubw/price.rb index 7d6bc78..3cf5688 100644 --- a/lib/ubw/price.rb +++ b/lib/ubw/price.rb @@ -4,4 +4,4 @@ class Price < Resource include Ubw::Queries::All @endpoint = '/prices' end -end \ No newline at end of file +end diff --git a/lib/ubw/queries/all.rb b/lib/ubw/queries/all.rb index 6d2267f..b4e7e5c 100644 --- a/lib/ubw/queries/all.rb +++ b/lib/ubw/queries/all.rb @@ -2,11 +2,9 @@ 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) @@ -14,4 +12,4 @@ def self.included(receiver) end end end -end \ No newline at end of file +end diff --git a/lib/ubw/queries/find.rb b/lib/ubw/queries/find.rb index 9a55f55..8af27d4 100644 --- a/lib/ubw/queries/find.rb +++ b/lib/ubw/queries/find.rb @@ -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 \ No newline at end of file +end diff --git a/lib/ubw/queries/where.rb b/lib/ubw/queries/where.rb index 73d3be6..48f9a4e 100644 --- a/lib/ubw/queries/where.rb +++ b/lib/ubw/queries/where.rb @@ -2,21 +2,19 @@ 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) @@ -24,4 +22,4 @@ def self.included(receiver) end end end -end \ No newline at end of file +end diff --git a/lib/ubw/resource.rb b/lib/ubw/resource.rb index 6acd5fc..d1f664e 100644 --- a/lib/ubw/resource.rb +++ b/lib/ubw/resource.rb @@ -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 \ No newline at end of file +end diff --git a/lib/ubw/result_set.rb b/lib/ubw/result_set.rb index 9d65c2f..f2e6a4f 100644 --- a/lib/ubw/result_set.rb +++ b/lib/ubw/result_set.rb @@ -7,7 +7,7 @@ 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 @@ -15,5 +15,11 @@ def each 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 \ No newline at end of file diff --git a/lib/ubw/sub_project.rb b/lib/ubw/sub_project.rb index 64a08f9..81dd124 100644 --- a/lib/ubw/sub_project.rb +++ b/lib/ubw/sub_project.rb @@ -5,4 +5,4 @@ class SubProject < Resource include Ubw::Queries::All @endpoint = '/subprojects' end -end \ No newline at end of file +end diff --git a/lib/ubw/util.rb b/lib/ubw/util.rb index 3884a7d..5be3b89 100644 --- a/lib/ubw/util.rb +++ b/lib/ubw/util.rb @@ -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 @@ -28,6 +27,5 @@ def self.camelify(str) end new_str end - end -end \ No newline at end of file +end