Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For generating Intercom javascript script tags for Rails, please see https://git
## Upgrading information
Version 2 of intercom-ruby is not backwards compatible with previous versions. Be sure to test this new version before deploying to production. One other change you will need to make as part of the upgrade is to set `Intercom.app_api_key` and not set `Intercom.api_key` (you can continue to use your existing API key).

Additionally, the new version uses Ruby 2.
This version of the gem is compatible with `Ruby 2.1`, `Ruby 2.0` & `Ruby 1.9.3`

## Installation

Expand Down
2 changes: 1 addition & 1 deletion intercom.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "fakeweb", ["~> 1.3"]

spec.add_dependency 'json'
spec.required_ruby_version = '~> 2.0'
spec.required_ruby_version = '>= 1.9.3'
end
2 changes: 1 addition & 1 deletion lib/intercom/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Intercom
# Base class exception from which all public Intercom exceptions will be derived
class IntercomError < StandardError
attr_reader :http_code, :application_error_code
def initialize(message, http_code: nil, application_error_code: application_error_code)
def initialize(message, http_code = nil, application_error_code = application_error_code)
@http_code = http_code
@application_error_code = application_error_code
super(message)
Expand Down
16 changes: 14 additions & 2 deletions lib/intercom/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ def pluralize(str)
"#{str}s"
end

# the constantize method that exists in rails to allow for ruby 1.9 to get namespaced constants
def constantize(camel_cased_word)
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?

constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end

def resource_class_to_singular_name(resource_class)
resource_class.to_s.split('::')[-1].downcase
end
Expand All @@ -22,14 +34,14 @@ def constantize_resource_name(resource_name)
class_name = Utils.singularize(resource_name.capitalize)
define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false)
namespaced_class_name = "Intercom::#{class_name}"
Object.const_get(namespaced_class_name)
constantize namespaced_class_name
end

def constantize_singular_resource_name(resource_name)
class_name = resource_name.split('_').map(&:capitalize).join
define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false)
namespaced_class_name = "Intercom::#{class_name}"
Object.const_get(namespaced_class_name )
constantize namespaced_class_name
end

def define_lightweight_class(class_name)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_message
}
end

def page_of_users(include_next_link: false)
def page_of_users(include_next_link= false)
{
"type"=>"user.list",
"pages"=>
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/intercom/collection_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
describe Intercom::CollectionProxy do

it "stops iterating if no next link" do
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
Intercom.expects(:get).with("/users", {}).returns(page_of_users(false))
emails = []
Intercom::User.all.each { |user| emails << user.email }
emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
end

it "keeps iterating if next link" do
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:true))
Intercom.expects(:get).with('https://api.intercom.io/users?per_page=50&page=2', {}).returns(page_of_users(include_next_link:false))
Intercom.expects(:get).with("/users", {}).returns(page_of_users(true))
Intercom.expects(:get).with('https://api.intercom.io/users?per_page=50&page=2', {}).returns(page_of_users(false))
emails = []
Intercom::User.all.each { |user| emails << user.email }
end

it "supports indexed array access" do
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
Intercom.expects(:get).with("/users", {}).returns(page_of_users(false))
Intercom::User.all[0].email.must_equal 'user1@example.com'
end

it "supports map" do
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
Intercom.expects(:get).with("/users", {}).returns(page_of_users(false))
emails = Intercom::User.all.map { |user| user.email }
emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
end

it "supports querying" do
Intercom.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(include_next_link:false))
Intercom.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(false))
Intercom::User.find_all(:tag_name => 'Taggart J').map(&:email).must_equal %W(user1@example.com user2@example.com user3@example.com)
end
end
4 changes: 2 additions & 2 deletions spec/unit/intercom/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe "Intercom::Event" do

let (:user) {Intercom::User.new("email" => "jim@example.com", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")}
let (:created_time) {Time.now - 300}
let(:user) {Intercom::User.new("email" => "jim@example.com", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")}
let(:created_time) {Time.now - 300}

it "creates an event with metadata" do
Intercom.expects(:post).with('/events', {'event_name' => 'Eventful 1', 'created_at' => created_time.to_i, 'email' => 'joe@example.com', 'metadata' => {'invitee_email' => 'pi@example.org', :invite_code => 'ADDAFRIEND', 'found_date' => 12909364407}}).returns(:status => 202)
Expand Down
12 changes: 7 additions & 5 deletions spec/unit/intercom/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@

it "rejects nested data structures in custom_attributes" do
user = Intercom::User.new()
proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
proc { user.custom_attributes["thing"] = {1 => 2} }.must_raise ArgumentError
proc { user.custom_attributes = {1 => {2 => 3}} }.must_raise ArgumentError

proc { user.custom_attributes["thing"] = [1] }.must_raise(ArgumentError)
proc { user.custom_attributes["thing"] = {1 => 2} }.must_raise(ArgumentError)
proc { user.custom_attributes["thing"] = {1 => {2 => 3}} }.must_raise(ArgumentError)

user = Intercom::User.from_api(test_user)
proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
proc { user.custom_attributes["thing"] = [1] }.must_raise(ArgumentError)
end

describe "incrementing custom_attributes fields" do
Expand Down Expand Up @@ -199,7 +200,8 @@
it "sets/gets rw keys" do
params = {"email" => "me@example.com", :user_id => "abc123", "name" => "Bob Smith", "last_seen_ip" => "1.2.3.4", "last_seen_user_agent" => "ie6", "created_at" => Time.now}
user = Intercom::User.new(params)
user.to_hash.keys.sort.must_equal (params.keys + ['custom_attributes']).map(&:to_s).sort
custom_attributes = (params.keys + ['custom_attributes']).map(&:to_s).sort
user.to_hash.keys.sort.must_equal custom_attributes
params.keys.each do |key|
user.send(key).to_s.must_equal params[key].to_s
end
Expand Down