intercom 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/changes.txt CHANGED
@@ -1,3 +1,6 @@
1
+ 0.1.8
2
+ - change tag response to match the new api response. add support to query users that are tagged with a specific tag
3
+
1
4
  0.1.7
2
5
  - add support for creating, updating, and fetching tags
3
6
 
@@ -0,0 +1,17 @@
1
+ module Intercom
2
+ module HashableObject
3
+ def from_hash(hash)
4
+ hash.each do |key,value|
5
+ setter_method = "#{key.to_s}="
6
+ self.send(setter_method, value) if self.respond_to?(setter_method)
7
+ end
8
+ end
9
+
10
+ def to_hash
11
+ instance_variables.inject({}) do |hash, var|
12
+ hash[var.to_s.delete("@").to_sym] = instance_variable_get(var)
13
+ hash
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ module Intercom
2
+ module RequiresParameters
3
+
4
+ def requires_parameters(parameters, required)
5
+ missing = Array(required) - parameters.keys.map(&:to_s)
6
+ raise ArgumentError.new("Missing required parameters (#{missing.join(', ')}).") unless missing.empty?
7
+ end
8
+
9
+ end
10
+ end
data/lib/intercom/tag.rb CHANGED
@@ -1,11 +1,12 @@
1
- require 'intercom/user_resource'
1
+ require 'intercom/requires_parameters'
2
+ require 'intercom/hashable_object'
2
3
 
3
4
  module Intercom
4
5
 
5
6
  ##
6
7
  # Represents a tag
7
8
  #
8
- # A tag consists of a name, and (optionally) a color and users that you would like to tag.
9
+ # A tag consists of a name, and (optionally) a color and users that you would like to tag. Returns details about the tag and a count of the number of users currently tagged.
9
10
  #
10
11
  # == Examples
11
12
  #
@@ -27,17 +28,27 @@ module Intercom
27
28
  # tag.tag_or_untag = "untag"
28
29
  # tag.save
29
30
 
30
- class Tag < UserResource
31
+ class Tag
32
+ extend RequiresParameters
33
+ include HashableObject
34
+
35
+ attr_accessor :name, :color, :user_ids, :emails, :tag_or_untag, :segment, :tagged_user_count, :id
31
36
 
32
37
  def initialize(attributes={})
33
- @attributes = attributes
38
+ from_hash(attributes)
34
39
  end
35
40
 
36
41
  ##
37
42
  # Finds a Tag using params
38
43
  def self.find(params)
39
44
  response = Intercom.get("/v1/tags", params)
40
- Tag.from_api(response)
45
+ from_api(response)
46
+ end
47
+
48
+ def self.from_api(api_response)
49
+ tag = Tag.new
50
+ tag.from_hash(api_response)
51
+ tag
41
52
  end
42
53
 
43
54
  ##
@@ -58,51 +69,8 @@ module Intercom
58
69
  # Saves a Tag on your application
59
70
  def save
60
71
  response = Intercom.post("/v1/tags", to_hash)
61
- self.update_from_api_response(response)
72
+ self.from_hash(response)
73
+ self
62
74
  end
63
-
64
- ##
65
- # The name of the tag
66
- def name=(name)
67
- @attributes["name"] = name
68
- end
69
-
70
- ##
71
- # The color of the tag
72
- def color=(color)
73
- @attributes["color"] = color
74
- end
75
-
76
- ##
77
- # An array of user_ids of the users you'd like to tag or untag
78
- def user_ids
79
- @attributes["user_ids"] ||= []
80
- end
81
-
82
- ##
83
- # An array of user_ids of the users you'd like to tag or untag
84
- def emails
85
- @attributes["emails"] ||= []
86
- end
87
-
88
- ##
89
- # An array of user_ids of the users you'd like to tag or untag
90
- def user_ids=(user_ids)
91
- @attributes["user_ids"] = user_ids
92
- end
93
-
94
- ##
95
- # An array of emails of the users you'd like to tag or untag
96
- def emails=(emails)
97
- @attributes["emails"] = emails
98
- end
99
-
100
- ##
101
- # A string to specify whether to tag or untag the specified users, can be left out if only creating a new tag.
102
- def tag_or_untag=(tag_or_untag)
103
- return unless ["tag", "untag"].include?(tag_or_untag)
104
- @attributes["tag_or_untag"] = tag_or_untag
105
- end
106
-
107
75
  end
108
76
  end
data/lib/intercom/user.rb CHANGED
@@ -21,6 +21,8 @@ module Intercom
21
21
  # user.custom_data["number_of_applications"] = 11
22
22
  # user.save
23
23
  class User < UserResource
24
+
25
+
24
26
  ##
25
27
  # Fetches an Intercom::User from our API.
26
28
  #
@@ -80,6 +82,22 @@ module Intercom
80
82
  UserCollectionProxy.new
81
83
  end
82
84
 
85
+ # Retrieve all the users that match a query
86
+ # Examples:
87
+ # Intercom::User.where(:tag_name => 'Free Trial').each do |user|
88
+ # puts user.inspect
89
+ # end
90
+ # > ["user1@example.com" ,"user2@example.com" ,....]
91
+ # Intercom::User.where(:tag_name => 'Free Trial').map(&:email)
92
+ # > ["user1@example.com" ,"user2@example.com" ,....]
93
+ #
94
+ # Currently only supports tag_name and tag_id querying
95
+ #
96
+ # @return [UserCollectionProxy]
97
+ def self.where(params)
98
+ UserCollectionProxy.new(params)
99
+ end
100
+
83
101
  # Fetches a count of all Users tracked on Intercom.
84
102
  # Example:
85
103
  # Intercom::User.all.count
@@ -14,13 +14,22 @@ module Intercom
14
14
  # end
15
15
  #
16
16
  class UserCollectionProxy
17
+
18
+ QUERYABLE_ATTRIBUTES = [:tag_id, :tag_name]
19
+
20
+ attr_accessor :query
21
+
22
+ def initialize(attributes={})
23
+ self.query = attributes.reject{ |key, value| !QUERYABLE_ATTRIBUTES.include?(key) }
24
+ end
25
+
17
26
  # yields each {User} to the block provided
18
27
  # @return [void]
19
28
  def each(&block)
20
29
  page = 1
21
30
  fetch_another_page = true
22
31
  while fetch_another_page
23
- current_page = Intercom.get("/v1/users", {:page => page})
32
+ current_page = Intercom.get("/v1/users", query.merge({:page => page}))
24
33
  current_page["users"].each do |user|
25
34
  block.call User.from_api(user)
26
35
  end
@@ -1,8 +1,10 @@
1
1
  require 'intercom/unix_timestamp_unwrapper'
2
+ require 'intercom/requires_parameters'
2
3
 
3
4
  module Intercom
4
5
  # Base class for resources tied off a {User}, all of which are scoped by either the users :email or :user_id.
5
6
  class UserResource
7
+ extend RequiresParameters
6
8
  include UnixTimestampUnwrapper
7
9
 
8
10
  def initialize(attributes={})
@@ -76,10 +78,5 @@ module Intercom
76
78
  return @attributes[method.to_s] if @attributes.has_key?(method.to_s)
77
79
  super
78
80
  end
79
-
80
- def self.requires_parameters(parameters, required)
81
- missing = Array(required) - parameters.keys.map(&:to_s)
82
- raise ArgumentError.new("Missing required parameters (#{missing.join(', ')}).") unless missing.empty?
83
- end
84
81
  end
85
82
  end
@@ -1,3 +1,3 @@
1
1
  module Intercom #:nodoc:
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -90,12 +90,11 @@ end
90
90
 
91
91
  def test_tag
92
92
  {
93
+ "id" => "4f73428b5e4dfc000b000112",
93
94
  "name" => "Test Tag",
94
95
  "color" => "red",
95
- "users" => [
96
- {"email" => "bob@example.com", "user_id" => "abc123"},
97
- {"email" => "tom@example.com", "user_id" => "def456"}
98
- ]
96
+ "segment" => false,
97
+ "tagged_user_count" => 2
99
98
  }
100
99
  end
101
100
 
@@ -27,6 +27,7 @@ describe "Intercom::Tag" do
27
27
  tag = Intercom::Tag.create(:name => "Test Tag", :color => "red", :user_ids => ["abc123", "def456"], :tag_or_untag => "tag")
28
28
  tag.name.must_equal "Test Tag"
29
29
  tag.color.must_equal "red"
30
- tag.users.must_equal [{"email" => "bob@example.com", "user_id" => "abc123"}, {"email" => "tom@example.com", "user_id" => "def456"}]
30
+ tag.tagged_user_count.must_equal 2
31
31
  end
32
+
32
33
  end
@@ -12,6 +12,20 @@ describe Intercom::UserCollectionProxy do
12
12
  emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
13
13
  end
14
14
 
15
+ it "supports querying for tagged users" do
16
+ Intercom.expects(:get).with("/v1/users", {:tag_id => "1234", :page => 1}).returns(page_of_users)
17
+ emails = []
18
+ Intercom::User.where(:tag_id => '1234').each { |user| emails << user.email }
19
+ emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
20
+ end
21
+
22
+ it "should only pass whitelisted attributes to collection query" do
23
+ Intercom.expects(:get).with("/v1/users", {:tag_name => '1234', :page => 1}).returns(page_of_users)
24
+ emails = []
25
+ Intercom::User.where(:danger_zone => 'BOOM', :tag_name => '1234').each { |user| emails << user.email }
26
+ emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
27
+ end
28
+
15
29
  it "supports map" do
16
30
  Intercom.expects(:get).with("/v1/users", {:page => 1}).returns(page_of_users).twice
17
31
  Intercom::User.all.map { |user| user.email }.must_equal %W(user1@example.com user2@example.com user3@example.com)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intercom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-04-03 00:00:00.000000000 Z
16
+ date: 2013-04-09 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: minitest
@@ -87,10 +87,12 @@ files:
87
87
  - lib/data/cacert.pem
88
88
  - lib/intercom.rb
89
89
  - lib/intercom/flat_store.rb
90
+ - lib/intercom/hashable_object.rb
90
91
  - lib/intercom/impression.rb
91
92
  - lib/intercom/message_thread.rb
92
93
  - lib/intercom/note.rb
93
94
  - lib/intercom/request.rb
95
+ - lib/intercom/requires_parameters.rb
94
96
  - lib/intercom/social_profile.rb
95
97
  - lib/intercom/tag.rb
96
98
  - lib/intercom/unix_timestamp_unwrapper.rb