Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #22 from intercom/user-tagging-refactored

User tagging refactored
  • Loading branch information...
commit 7e2d8f37e71d260da0fbee4dc5f306e3d9db1df0 2 parents c9177ef + 542b694
@erskingardner erskingardner authored
View
7 changes.txt
@@ -1,8 +1,11 @@
+0.1.7
+ - add support for creating, updating, and fetching tags
+
0.1.6
- - Add unsubscribed_from_emails and unsubscribed_from_emails= to user
+ - add unsubscribed_from_emails and unsubscribed_from_emails= to user
0.1.5
- - Always convert times to unix timestamps for the wire (thanks @jgwhite)
+ - always convert times to unix timestamps for the wire (thanks @jgwhite)
0.1.4
- Intercom::User.all now includes Enumerable
View
1  lib/intercom.rb
@@ -4,6 +4,7 @@
require "intercom/message_thread"
require "intercom/impression"
require "intercom/note"
+require "intercom/tag"
require "intercom/request"
require "json"
View
108 lib/intercom/tag.rb
@@ -0,0 +1,108 @@
+require 'intercom/user_resource'
+
+module Intercom
+
+ ##
+ # Represents a tag
+ #
+ # A tag consists of a name, and (optionally) a color and users that you would like to tag.
+ #
+ # == Examples
+ #
+ # tag = Intercom::Tag.create(:name => "Super Tag", :color => "red", :user_ids => ['abc123', 'def456'])
+ # tag = Intercom::Tag.create(:name => "Super Tag", :color => "red", :emails => ['bob@example.com', 'joe@example.com'])
+ #
+ # You can also create a tag and save it like this:
+ # tag = Intercom::Tag.new
+ # tag.name = "Super Tag"
+ # tag.color = "red"
+ # tag.user_ids = ['abc123', 'def456']
+ # tag.tag_or_untag = "tag"
+ # tag.save
+ #
+ # Or update a tag and save it like this:
+ # tag = Intercom::Tag.find_by_name "Super Tag"
+ # tag.color = "green"
+ # tag.user_ids << 'abc123'
+ # tag.tag_or_untag = "untag"
+ # tag.save
+
+ class Tag < UserResource
+
+ def initialize(attributes={})
+ @attributes = attributes
+ end
+
+ ##
+ # Finds a Tag using params
+ def self.find(params)
+ response = Intercom.get("/v1/tags", params)
+ Tag.from_api(response)
+ end
+
+ ##
+ # Finds a Tag using a name
+ def self.find_by_name(name)
+ find({:name => name})
+ end
+
+ ##
+ # Creates a new Tag using params and saves it
+ # @see #save
+ def self.create(params)
+ requires_parameters(params, %W(name))
+ Tag.new(params).save
+ end
+
+ ##
+ # Saves a Tag on your application
+ def save
+ response = Intercom.post("/v1/tags", to_hash)
+ self.update_from_api_response(response)
+ end
+
+ ##
+ # The name of the tag
+ def name=(name)
+ @attributes["name"] = name
+ end
+
+ ##
+ # The color of the tag
+ def color=(color)
+ @attributes["color"] = color
+ end
+
+ ##
+ # An array of user_ids of the users you'd like to tag or untag
+ def user_ids
+ @attributes["user_ids"] ||= []
+ end
+
+ ##
+ # An array of user_ids of the users you'd like to tag or untag
+ def emails
+ @attributes["emails"] ||= []
+ end
+
+ ##
+ # An array of user_ids of the users you'd like to tag or untag
+ def user_ids=(user_ids)
+ @attributes["user_ids"] = user_ids
+ end
+
+ ##
+ # An array of emails of the users you'd like to tag or untag
+ def emails=(emails)
+ @attributes["emails"] = emails
+ end
+
+ ##
+ # A string to specify whether to tag or untag the specified users, can be left out if only creating a new tag.
+ def tag_or_untag=(tag_or_untag)
+ return unless ["tag", "untag"].include?(tag_or_untag)
+ @attributes["tag_or_untag"] = tag_or_untag
+ end
+
+ end
+end
View
2  lib/intercom/version.rb
@@ -1,3 +1,3 @@
module Intercom #:nodoc:
- VERSION = "0.1.6"
+ VERSION = "0.1.7"
end
View
13 spec/spec_helper.rb
@@ -1,6 +1,6 @@
require 'intercom'
require 'minitest/autorun'
-require 'mocha/setup'
+require 'mocha'
def test_user(email="bob@example.com")
{
@@ -88,6 +88,17 @@ def page_of_users(page=1, per_page=10)
}
end
+def test_tag
+ {
+ "name" => "Test Tag",
+ "color" => "red",
+ "users" => [
+ {"email" => "bob@example.com", "user_id" => "abc123"},
+ {"email" => "tom@example.com", "user_id" => "def456"}
+ ]
+ }
+end
+
def error_on_modify_frozen
RUBY_VERSION =~ /1.8/ ? TypeError : RuntimeError
end
View
32 spec/unit/intercom/tag_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe "Intercom::Tag" do
+ it "gets a tag" do
+ Intercom.expects(:get).with("/v1/tags", {:name => "Test Tag"}).returns(test_tag)
+ tag = Intercom::Tag.find(:name => "Test Tag")
+ tag.name.must_equal "Test Tag"
+ tag.color.must_equal "red"
+ end
+
+ it "gets a tag by name" do
+ Intercom.expects(:get).with("/v1/tags", {:name => "Test Tag"}).returns(test_tag)
+ tag = Intercom::Tag.find_by_name "Test Tag"
+ tag.name.must_equal "Test Tag"
+ tag.color.must_equal "red"
+ end
+
+ it "creates a tag" do
+ Intercom.expects(:post).with("/v1/tags", {:name => "Test Tag"}).returns(test_tag)
+ tag = Intercom::Tag.create(:name => "Test Tag")
+ tag.name.must_equal "Test Tag"
+ tag.color.must_equal "red"
+ end
+
+ it "tags users" do
+ Intercom.expects(:post).with("/v1/tags", {:name => "Test Tag", :color => "red", :user_ids => ["abc123", "def456"], :tag_or_untag => "tag"}).returns(test_tag)
+ tag = Intercom::Tag.create(:name => "Test Tag", :color => "red", :user_ids => ["abc123", "def456"], :tag_or_untag => "tag")
+ tag.name.must_equal "Test Tag"
+ tag.color.must_equal "red"
+ tag.users.must_equal [{"email" => "bob@example.com", "user_id" => "abc123"}, {"email" => "tom@example.com", "user_id" => "def456"}]
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.