Skip to content

Commit

Permalink
Merge pull request #36 from mattbeedle/lastmodified
Browse files Browse the repository at this point in the history
Format lastmodified param automatically on all GET requests
  • Loading branch information
mattbeedle committed Sep 30, 2013
2 parents aa472a6 + 33e0070 commit d0217ea
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/capsule_crm/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Connection
#
# Returns a Hash from the JSON response
def self.get(path, params = {})
preprocess_params(params)
response = faraday.get(path, params) do |req|
req.headers.update default_request_headers
end
Expand Down Expand Up @@ -35,6 +36,18 @@ def self.delete(path)

private

def self.preprocess_params(params)
params.symbolize_keys!
if params_contains_lastmodified(params)
params[:lastmodified] = params[:lastmodified].strftime("%Y%m%dT%H%M%S")
end
end

def self.params_contains_lastmodified(params)
params.keys.include?(:lastmodified) &&
params[:lastmodified].respond_to?(:strftime)
end

# TODO clean this shit up
def self.process_post_response(response)
if response.success?
Expand Down
14 changes: 3 additions & 11 deletions lib/capsule_crm/party.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class CapsuleCRM::Party
has_many :tasks, class_name: 'CapsuleCRM::Task', source: :party

def self.all(options = {})
process_options(options)
attributes = CapsuleCRM::Connection.get('/api/party', options)

init_collection(attributes['parties'])
init_collection(
CapsuleCRM::Connection.get('/api/party', options)['parties']
)
end

def self.find(id)
Expand All @@ -39,11 +38,4 @@ def self.party_classes
{ person: 'CapsuleCRM::Person', organisation: 'CapsuleCRM::Organization' }.
stringify_keys
end

def self.process_options(options)
if options[:lastmodified] && options[:lastmodified].respond_to?(:strftime)
options[:lastmodified] = options[:lastmodified].
strftime("%Y%m%dT%H%M%SZ")
end
end
end
21 changes: 21 additions & 0 deletions spec/lib/capsule_crm/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe CapsuleCRM::Connection do
before { configure }

describe '.get' do
context 'when lastmodified is in the supplied params' do
before do
stub_request(:get, /\/api\/v1\/foo/).to_return(body: '{}')
CapsuleCRM::Connection.
get('/api/v1/foo', lastmodified: Time.new(2013, 10, 1, 13, 31, 56))
end

it 'should make sure that the lastmodified is formatted in YYYYMMDDTHHMMSS' do
WebMock.should have_requested(
:get, 'https://1234:@company.capsulecrm.com/api/v1/foo'
).with(query: { lastmodified: "20131001T133156" })
end
end
end
end

0 comments on commit d0217ea

Please sign in to comment.