Skip to content

Commit c6b972e

Browse files
Chocksyjosler
authored andcommitted
Add Ruby 1.9.3 support
- we are using the constantize method instead of Object.const_get as ruby 1.9.3 doesn't allow namespaced constants
1 parent 900c624 commit c6b972e

File tree

9 files changed

+35
-19
lines changed

9 files changed

+35
-19
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
language: ruby
22
rvm:
3+
- 1.9.3
34
- 2.0.0
5+
- 2.1.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ For generating Intercom javascript script tags for Rails, please see https://git
1111
## Upgrading information
1212
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).
1313

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

1616
## Installation
1717

intercom.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "fakeweb", ["~> 1.3"]
2626

2727
spec.add_dependency 'json'
28-
spec.required_ruby_version = '~> 2.0'
28+
spec.required_ruby_version = '>= 1.9.3'
2929
end

lib/intercom/errors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Intercom
33
# Base class exception from which all public Intercom exceptions will be derived
44
class IntercomError < StandardError
55
attr_reader :http_code, :application_error_code
6-
def initialize(message, http_code: nil, application_error_code: application_error_code)
6+
def initialize(message, http_code = nil, application_error_code = application_error_code)
77
@http_code = http_code
88
@application_error_code = application_error_code
99
super(message)

lib/intercom/utils.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ def pluralize(str)
1010
"#{str}s"
1111
end
1212

13+
# the constantize method that exists in rails to allow for ruby 1.9 to get namespaced constants
14+
def constantize(camel_cased_word)
15+
names = camel_cased_word.split('::')
16+
names.shift if names.empty? || names.first.empty?
17+
18+
constant = Object
19+
names.each do |name|
20+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
21+
end
22+
constant
23+
end
24+
1325
def resource_class_to_singular_name(resource_class)
1426
resource_class.to_s.split('::')[-1].downcase
1527
end
@@ -22,14 +34,14 @@ def constantize_resource_name(resource_name)
2234
class_name = Utils.singularize(resource_name.capitalize)
2335
define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false)
2436
namespaced_class_name = "Intercom::#{class_name}"
25-
Object.const_get(namespaced_class_name)
37+
constantize namespaced_class_name
2638
end
2739

2840
def constantize_singular_resource_name(resource_name)
2941
class_name = resource_name.split('_').map(&:capitalize).join
3042
define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false)
3143
namespaced_class_name = "Intercom::#{class_name}"
32-
Object.const_get(namespaced_class_name )
44+
constantize namespaced_class_name
3345
end
3446

3547
def define_lightweight_class(class_name)

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_message
107107
}
108108
end
109109

110-
def page_of_users(include_next_link: false)
110+
def page_of_users(include_next_link= false)
111111
{
112112
"type"=>"user.list",
113113
"pages"=>

spec/unit/intercom/collection_proxy_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
describe Intercom::CollectionProxy do
44

55
it "stops iterating if no next link" do
6-
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
6+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(false))
77
emails = []
88
Intercom::User.all.each { |user| emails << user.email }
99
1010
end
1111

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

1919
it "supports indexed array access" do
20-
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
20+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(false))
2121
Intercom::User.all[0].email.must_equal '[email protected]'
2222
end
2323

2424
it "supports map" do
25-
Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
25+
Intercom.expects(:get).with("/users", {}).returns(page_of_users(false))
2626
emails = Intercom::User.all.map { |user| user.email }
2727
2828
end
2929

3030
it "supports querying" do
31-
Intercom.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(include_next_link:false))
31+
Intercom.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(false))
3232
Intercom::User.find_all(:tag_name => 'Taggart J').map(&:email).must_equal %W([email protected] [email protected] [email protected])
3333
end
3434
end

spec/unit/intercom/event_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
describe "Intercom::Event" do
44

5-
let (:user) {Intercom::User.new("email" => "[email protected]", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")}
6-
let (:created_time) {Time.now - 300}
5+
let(:user) {Intercom::User.new("email" => "[email protected]", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")}
6+
let(:created_time) {Time.now - 300}
77

88
it "creates an event with metadata" do
99
Intercom.expects(:post).with('/events', {'event_name' => 'Eventful 1', 'created_at' => created_time.to_i, 'email' => '[email protected]', 'metadata' => {'invitee_email' => '[email protected]', :invite_code => 'ADDAFRIEND', 'found_date' => 12909364407}}).returns(:status => 202)

spec/unit/intercom/user_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@
9999

100100
it "rejects nested data structures in custom_attributes" do
101101
user = Intercom::User.new()
102-
proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
103-
proc { user.custom_attributes["thing"] = {1 => 2} }.must_raise ArgumentError
104-
proc { user.custom_attributes = {1 => {2 => 3}} }.must_raise ArgumentError
102+
103+
proc { user.custom_attributes["thing"] = [1] }.must_raise(ArgumentError)
104+
proc { user.custom_attributes["thing"] = {1 => 2} }.must_raise(ArgumentError)
105+
proc { user.custom_attributes["thing"] = {1 => {2 => 3}} }.must_raise(ArgumentError)
105106

106107
user = Intercom::User.from_api(test_user)
107-
proc { user.custom_attributes["thing"] = [1] }.must_raise ArgumentError
108+
proc { user.custom_attributes["thing"] = [1] }.must_raise(ArgumentError)
108109
end
109110

110111
describe "incrementing custom_attributes fields" do
@@ -199,7 +200,8 @@
199200
it "sets/gets rw keys" do
200201
params = {"email" => "[email protected]", :user_id => "abc123", "name" => "Bob Smith", "last_seen_ip" => "1.2.3.4", "last_seen_user_agent" => "ie6", "created_at" => Time.now}
201202
user = Intercom::User.new(params)
202-
user.to_hash.keys.sort.must_equal (params.keys + ['custom_attributes']).map(&:to_s).sort
203+
custom_attributes = (params.keys + ['custom_attributes']).map(&:to_s).sort
204+
user.to_hash.keys.sort.must_equal custom_attributes
203205
params.keys.each do |key|
204206
user.send(key).to_s.must_equal params[key].to_s
205207
end

0 commit comments

Comments
 (0)