From 33e0070222db62368504808837e7d6608c7fa330 Mon Sep 17 00:00:00 2001 From: Matt Beedle Date: Mon, 30 Sep 2013 08:00:33 +0200 Subject: [PATCH] Format lastmodified param automatically on all GET requests --- lib/capsule_crm/connection.rb | 13 +++++++++++++ lib/capsule_crm/party.rb | 14 +++----------- spec/lib/capsule_crm/connection_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 spec/lib/capsule_crm/connection_spec.rb diff --git a/lib/capsule_crm/connection.rb b/lib/capsule_crm/connection.rb index b216ab7..53f5a23 100644 --- a/lib/capsule_crm/connection.rb +++ b/lib/capsule_crm/connection.rb @@ -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 @@ -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? diff --git a/lib/capsule_crm/party.rb b/lib/capsule_crm/party.rb index 19bea71..74901b5 100644 --- a/lib/capsule_crm/party.rb +++ b/lib/capsule_crm/party.rb @@ -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) @@ -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 diff --git a/spec/lib/capsule_crm/connection_spec.rb b/spec/lib/capsule_crm/connection_spec.rb new file mode 100644 index 0000000..435dab2 --- /dev/null +++ b/spec/lib/capsule_crm/connection_spec.rb @@ -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