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

Replace utility methods with ActiveSupport #15

Open
wants to merge 67 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
d6319d6
Explicitly specify ruby version
amilligan May 13, 2018
00407c6
Include rubocop for style analysis
amilligan May 13, 2018
0682175
Place dot operator consistently on multiline calls
amilligan May 13, 2018
cd1055b
Remove extraneous whitespace
amilligan May 13, 2018
919f9fc
Indent consistently
amilligan May 13, 2018
b293021
Space parameters and operator consistently
amilligan May 13, 2018
93d2ddd
Include trailing line consistently in all files
amilligan May 13, 2018
348e28c
Fix ambiguous regexp literal
amilligan May 13, 2018
ad197e1
Clarify assignment in conditional expressions
amilligan May 13, 2018
e502a76
Remove duplicate accessor method definition
amilligan May 13, 2018
cd48ba8
Make private methods actually private
amilligan May 13, 2018
97cb0f8
Replace obsolete method call
amilligan May 13, 2018
95feef9
Remove useless variable assignments
amilligan May 13, 2018
41d9e5b
Replace unnecessary hash merge
amilligan May 13, 2018
32f770f
Prefer boolean operator in condition
amilligan May 13, 2018
efc5b1f
Enforce consistent brace/paren/etc. style
amilligan May 13, 2018
718b1eb
Use interpolated strings only when required
amilligan May 13, 2018
43f892e
Remove unnecessary string interpolation
amilligan May 13, 2018
69ee0a9
Remove redundant uses of self
amilligan May 13, 2018
37a817e
Prefer parameterized method of raising exceptions
amilligan May 14, 2018
cbcf708
Use `next` to skip iteration
amilligan May 14, 2018
b8a1243
Avoid unnecessarily negated conditions
amilligan May 14, 2018
aba084a
Freeze mutable objects set as constants
amilligan May 14, 2018
f6237f0
Replace inverted use of `select` with `reject`
amilligan May 14, 2018
da65008
Use modifier conditional syntax when applicable
amilligan May 14, 2018
7e822a2
Add guard clause to method
amilligan May 14, 2018
382d668
Simplify file path expansion
amilligan May 14, 2018
2b64f88
Make rubocop accept the main gem filename
amilligan May 14, 2018
873cd93
Remove redunant variable assignment
amilligan May 14, 2018
ac57423
Consistently use #is_a? for class check
amilligan May 14, 2018
7b8dc0e
Remove rubocop requirement for JavaDoc-style docs
amilligan May 14, 2018
9564afe
Consistently use ruby-style variable names
amilligan May 14, 2018
d8c5fac
Use descriptive method parameter names
amilligan May 14, 2018
4814ab9
Rename method to avoid ambiguity with accessor
amilligan May 14, 2018
2b693e8
Creating TODO file for remaining rubocop issues
amilligan May 14, 2018
a079b93
Add rake task for rubocop, and include in default
amilligan May 14, 2018
f71e199
Specify old-ish ruby (2.2) for rubocop
amilligan May 14, 2018
24d32ab
Remove redundant gem dependency
amilligan May 14, 2018
fd5fe47
Add missing gem dependency
amilligan May 14, 2018
92610d2
Add rspec with default configuration
amilligan May 14, 2018
f69610d
Add rake task for specs, and add to default
amilligan May 14, 2018
ac52777
Require project files in specs
amilligan May 14, 2018
d5e3e89
Add webmock gem for network testing
amilligan May 14, 2018
f387096
Use URI.join to build URLs
amilligan May 14, 2018
42f2960
Add basic test for HTTP requests
amilligan May 14, 2018
0641421
Extract common HTTP request test behavior
amilligan May 14, 2018
f1ca55d
Simplify Organization creation methods
amilligan May 14, 2018
b604315
Add basic specs for Organization attributes
amilligan May 14, 2018
560498b
Specialize shared examples for different requests
amilligan May 14, 2018
86b0023
Add specs for API list behavior
amilligan May 14, 2018
00ba2e1
Add specs for API create behavior
amilligan May 14, 2018
8971fa7
Add specs for API delete behavior
amilligan May 14, 2018
3d0ca01
Add specs for API update behavior
amilligan May 14, 2018
9079ae0
Add specs for API query by metadata behavior
amilligan May 14, 2018
2c120d6
Add specs for API save behavior
amilligan May 14, 2018
d464079
Add rspec helper dependency
amilligan May 15, 2018
90aedd7
Add specs for Destination object
amilligan May 15, 2018
597a067
Properly camelize request keys in expectations
amilligan May 15, 2018
08ec9dd
Add specs for Recipient object
amilligan May 15, 2018
d897ac1
Add specs for API find behavior
amilligan May 14, 2018
eb3cfd7
Add specs for Task object
amilligan May 15, 2018
726407a
Add specs for Team object
amilligan May 15, 2018
f37610c
Add specs for Webhook object
amilligan May 15, 2018
cb77a2e
Add specs for Worker object
amilligan May 15, 2018
0f10434
Remove unnecessary utility method
amilligan May 15, 2018
2cbfd93
Replace utility methods with existing methods
amilligan May 15, 2018
80cad85
Move global function to class where it is used
amilligan May 15, 2018
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
# Ignore bundler config
/.bundle
Gemfile.lock

spec/examples.txt

3 changes: 1 addition & 2 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--color
--format documentation
--require spec_helper
26 changes: 26 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.2

# Keep diffs clean
Layout/TrailingBlankLines:
EnforcedStyle: final_blank_line

# No way to avoid large blocks in RSpec
Metrics/BlockLength:
Exclude:
- spec/**/*

# Allow the gem to have a non-snakecase name
Naming/FileName:
Exclude:
- 'lib/onfleet-ruby.rb'

Style/Documentation:
Enabled: false

Style/StringLiterals:
Exclude:
- spec/**/*

38 changes: 38 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-05-13 20:30:59 -0400 using RuboCop version 0.55.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
Metrics/AbcSize:
Max: 26

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 7

# Offense count: 4
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 22

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 8

# Offense count: 29
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: when_needed, always, never
Style/FrozenStringLiteralComment:
Enabled: false

# Offense count: 9
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 138

2 changes: 2 additions & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2.5.1

6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gemspec
gem 'rest-client', '~> 1.6.8'
ruby '2.5.1'

gemspec

10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'bundler/gem_tasks'

require 'rubocop/rake_task'
RuboCop::RakeTask.new

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task default: %i[spec rubocop]

79 changes: 38 additions & 41 deletions lib/onfleet-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
require 'base64'
require 'uri'

# Utils
require 'onfleet-ruby/util'

# Errors
require 'onfleet-ruby/errors/onfleet_error'
require 'onfleet-ruby/errors/authentication_error'
Expand Down Expand Up @@ -35,68 +32,68 @@
require 'onfleet-ruby/worker'
require 'onfleet-ruby/webhook'


module Onfleet
@base_url = "https://onfleet.com/api/v2"
@base_url = 'https://onfleet.com/api/v2/'

class << self
attr_accessor :api_key, :base_url, :encoded_api_key
end
attr_accessor :api_key, :base_url

def self.request api_url, method, params={}
raise AuthenticationError.new("Set your API Key using Onfleet.api_key = <API_KEY>") unless @api_key
def request(api_url, method, params = {})
raise(AuthenticationError, 'Set your API Key using Onfleet.api_key = <API_KEY>') unless api_key

begin
response = RestClient::Request.execute(method: method, url: self.base_url+api_url, payload: params.to_json, headers: self.request_headers)

if response != ''
JSON.parse(response)
end
rescue RestClient::ExceptionWithResponse => e
if response_code = e.http_code and response_body = e.http_body
handle_api_error(response_code, JSON.parse(response_body))
else
begin
url = URI.join(base_url, api_url).to_s
response = RestClient::Request.execute(method: method, url: url, payload: params.to_json, headers: request_headers)
JSON.parse(response) unless response.empty?
rescue RestClient::ExceptionWithResponse => e
if (response_code = e.http_code) && (response_body = e.http_body)
handle_api_error(response_code, JSON.parse(response_body))
else
handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
handle_restclient_error(e)
end
rescue RestClient::Exception, Errno::ECONNREFUSED => e
handle_restclient_error(e)
end
end

private
def self.request_headers
private

def request_headers
{
Authorization: "Basic #{self.encoded_api_key}",
Authorization: "Basic #{encoded_api_key}",
content_type: :json,
accept: :json
}
end

def self.encoded_api_key
@encoded_api_key ||= Base64.urlsafe_encode64(@api_key)
def encoded_api_key
@encoded_api_key ||= Base64.urlsafe_encode64(api_key)
end

def self.handle_api_error code, body
def handle_api_error(code, body)
case code
when 400, 404
raise InvalidRequestError.new(body["message"])
raise InvalidRequestError, body['message']
when 401
raise AuthenticationError.new(body["message"])
raise AuthenticationError, body['message']
else
raise OnfleetError.new(body["message"])
raise OnfleetError, body['message']
end
end

def self.handle_restclient_error e
case e
when RestClient::RequestTimeout
message = "Could not connect to Onfleet. Check your internet connection and try again."
when RestClient::ServerBrokeConnection
message = "The connetion with onfleet terminated before the request completed. Please try again."
else
message = "There was a problem connection with Onfleet. Please try again. If the problem persists contact [email protected]"
end
def handle_restclient_error(exception)
message =
case exception
when RestClient::RequestTimeout
'Could not connect to Onfleet. Check your internet connection and try again.'
when RestClient::ServerBrokeConnection
'The connetion with onfleet terminated before the request completed. Please try again.'
else
'There was a problem connection with Onfleet. Please try again. If the problem persists contact [email protected]'
end

raise ConnectionError.new(message)
raise ConnectionError, message
end
end
end

9 changes: 6 additions & 3 deletions lib/onfleet-ruby/actions/create.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
require 'active_support/core_ext/hash'

module Onfleet
module Actions
module Create
module ClassMethods
def create params={}
self.new(params).save
def create(params = {})
new(params.symbolize_keys.except(:id)).save
end
end

def self.included base
def self.included(base)
base.extend(ClassMethods)
end
end
end
end

8 changes: 4 additions & 4 deletions lib/onfleet-ruby/actions/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module Onfleet
module Actions
module Delete
module ClassMethods
def delete id
api_url = "#{self.api_url}/#{id}"
response = Onfleet.request(api_url, :delete)
def delete(id)
Onfleet.request("#{api_url}/#{id}", :delete)
true
end
end

def self.included base
def self.included(base)
base.extend(ClassMethods)
end
end
end
end

14 changes: 8 additions & 6 deletions lib/onfleet-ruby/actions/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ module Onfleet
module Actions
module Find
module ClassMethods
def find field, search_term
encoded_term = URI::encode(search_term)
api_url = "#{self.api_url}/#{field}/#{encoded_term}"
response = Onfleet.request(api_url, :get, search_term)
Util.constantize("#{self}").new(response)
def find(field, search_term)
encoded_term = URI.encode_www_form_component(search_term)
url = "#{api_url}/#{field}/#{encoded_term}"

response = Onfleet.request(url, :get)
new(response)
end
end

def self.included base
def self.included(base)
base.extend(ClassMethods)
end
end
end
end

10 changes: 5 additions & 5 deletions lib/onfleet-ruby/actions/get.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module Onfleet
module Actions
module Get
module ClassMethods
def get id
api_url = "#{self.api_url}/#{id}"
response = Onfleet.request(api_url, :get)
Util.constantize("#{self}").new(response)
def get(id)
url = "#{api_url}/#{id}"
new(Onfleet.request(url, :get))
end
end

def self.included base
def self.included(base)
base.extend(ClassMethods)
end
end
end
end

28 changes: 15 additions & 13 deletions lib/onfleet-ruby/actions/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ module Onfleet
module Actions
module List
module ClassMethods
def list query_params={}
api_url = "#{self.api_url}"
def list(filters = {})
response = Onfleet.request(list_url_for(filters), :get)
response.compact.map { |item| new(item) }
end

private

if !query_params.empty?
api_url += "?"
query_params.each do |key, value|
api_url += "#{key}=#{value}&"
end
end
def list_url_for(filters)
[api_url, query_params(filters)].compact.join('?')
end

response = Onfleet.request(api_url, :get)
response.compact.map do |listObj|
Util.constantize("#{self}").new(listObj)
end
def query_params(filters)
filters && filters
.collect { |key, value| "#{key}=#{URI.encode_www_form_component(value)}" }
.join('&')
end
end

def self.included base
def self.included(base)
base.extend(ClassMethods)
end
end
end
end

10 changes: 5 additions & 5 deletions lib/onfleet-ruby/actions/query_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module Onfleet
module Actions
module QueryMetadata
module ClassMethods
def query_by_metadata metadata
api_url = "#{self.api_url}/metadata"
response = Onfleet.request(api_url, :post, metadata)
response.map { |item| Util.constantize("#{self}").new(item) } if response.is_a? Array
def query_by_metadata(metadata)
response = Onfleet.request("#{api_url}/metadata", :post, metadata)
[*response].compact.map { |item| new(item) }
end
end

def self.included base
def self.included(base)
base.extend(ClassMethods)
end
end
end
end

Loading