Skip to content

Commit

Permalink
Tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed May 12, 2013
1 parent f7222b2 commit b10d295
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/capsule_crm.rb
Expand Up @@ -3,13 +3,15 @@
require 'faraday_middleware'
require 'virtus'
require 'capsule_crm/capsule_jsonable'
require 'capsule_crm/taggable'
require 'capsule_crm/associations'
require 'capsule_crm/address'
require 'capsule_crm/case'
require 'capsule_crm/connection'
require 'capsule_crm/email'
require 'capsule_crm/party'
require 'capsule_crm/phone'
require 'capsule_crm/tag'
require 'capsule_crm/website'
require 'capsule_crm/hash_helper'
require 'capsule_crm/results_proxy'
Expand Down
1 change: 1 addition & 0 deletions lib/capsule_crm/case.rb
Expand Up @@ -7,6 +7,7 @@ class Case
include ActiveModel::Validations

include CapsuleCRM::Associations::BelongsTo
include CapsuleCRM::Taggable

attribute :id, Integer
attribute :name, String
Expand Down
9 changes: 6 additions & 3 deletions lib/capsule_crm/connection.rb
Expand Up @@ -14,13 +14,16 @@ def self.get(path, params = {})
JSON.parse response.body
end

def self.post(path, params)
def self.post(path, params = {})
response = faraday.post(path, params.to_json) do |request|
request.headers.update default_request_headers
end
if response.success?
id = response.headers['Location'].match(/\/(?<id>\d+)$/)[:id]
{ id: id }
if match = response.headers['Location'].match(/\/(?<id>\d+)$/)
{ id: match[:id] }
else
true
end
else
false
end
Expand Down
1 change: 1 addition & 0 deletions lib/capsule_crm/organization.rb
Expand Up @@ -11,6 +11,7 @@ class Organization < CapsuleCRM::Party
include ActiveModel::Validations::Callbacks

include CapsuleCRM::Associations::HasMany
include CapsuleCRM::Taggable

attribute :id, Integer
attribute :name, String
Expand Down
1 change: 1 addition & 0 deletions lib/capsule_crm/party.rb
@@ -1,4 +1,5 @@
class CapsuleCRM::Party
include CapsuleCRM::Taggable

def self.all(options = {})
attributes = CapsuleCRM::Connection.get('/api/party', options)
Expand Down
13 changes: 13 additions & 0 deletions lib/capsule_crm/tag.rb
@@ -0,0 +1,13 @@
module CapsuleCRM
class Tag
include Virtus

extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations

attribute :name

validates :name, presence: true
end
end
23 changes: 23 additions & 0 deletions lib/capsule_crm/taggable.rb
@@ -0,0 +1,23 @@
module CapsuleCRM
module Taggable
extend ActiveSupport::Concern

def tags
CapsuleCRM::Connection.get(
"/api/#{api_singular_name}/#{id}/tag"
)['tags']['tag'].map { |item| CapsuleCRM::Tag.new(item) }
end

def add_tag(tag_name)
if id
CapsuleCRM::Connection.post(
"/api/#{api_singular_name}/#{id}/#{URI.encode(tag_name)}"
)
end
end

def api_singular_name
self.class.to_s.demodulize.downcase.singularize
end
end
end
6 changes: 6 additions & 0 deletions spec/lib/capsule_crm/tag_spec.rb
@@ -0,0 +1,6 @@
require 'spec_helper'

describe CapsuleCRM::Tag do

it { should validate_presence_of(:name) }
end
59 changes: 59 additions & 0 deletions spec/lib/capsule_crm/taggable_spec.rb
@@ -0,0 +1,59 @@
require 'spec_helper'

class TaggableItem
include CapsuleCRM::Taggable
include Virtus

attribute :id
end

describe CapsuleCRM::Taggable do
before { configure }

describe '#tags' do
before do
stub_request(:get, /\/api\/taggableitem\/1\/tag$/).
to_return(body: File.read('spec/support/all_tags.json'))
end

let(:taggable_item) { TaggableItem.new(id: 1) }

subject { taggable_item.tags }

it { should be_a(Array) }

it { subject.length.should eql(2) }

it do
subject.all? { |item| item.is_a?(CapsuleCRM::Tag) }.should be_true
end

it { subject.first.name.should eql('Customer') }

it { subject.last.name.should eql('VIP') }
end

describe '#add_tag' do
context 'when the taggable item has an id' do
let(:taggable_item) { TaggableItem.new(id: 1) }

before do
loc = 'https://sample.capsulecrm.com/api/party/1000/tag/A%20Test%20Tag'
stub_request(:post, /\/api\/taggableitem\/1\/A%20Test%20Tag$/).
to_return(headers: { 'Location' => loc })
end

subject { taggable_item.add_tag 'A Test Tag' }

it { subject.should be_true }
end

context 'when the taggable item has no id' do
let(:taggable_item) { TaggableItem.new }

subject { taggable_item.add_tag 'A Test Tag' }

it { subject.should be_nil }
end
end
end
12 changes: 12 additions & 0 deletions spec/support/all_tags.json
@@ -0,0 +1,12 @@
{
"tags": {
"tag": [
{
"name": "Customer"
},
{
"name": "VIP"
}
]
}
}

0 comments on commit b10d295

Please sign in to comment.