intercom 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - rbx-18mode
9
+ - rbx-19mode
data/Gemfile CHANGED
@@ -1,4 +1,13 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in intercom.gemspec
4
3
  gemspec
4
+
5
+ group :development, :test do
6
+ platforms :jruby do
7
+ gem 'json-jruby'
8
+ gem 'jruby-openssl'
9
+ end
10
+ platforms :ruby_18 do
11
+ gem 'json_pure'
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2011- Intercom App, Inc. (https://www.intercom.io)
3
+ Copyright (c) 2011- Intercom, Inc. (https://www.intercom.io)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -2,6 +2,8 @@ Ruby bindings for the Intercom API (https://api.intercom.io). See http://docs.in
2
2
 
3
3
  Yardoc is available at http://rubydoc.info/github/intercom/intercom-ruby/master/frames
4
4
 
5
+ For generating Intercom javascript script tags, please see https://github.com/intercom/intercom-rails
6
+
5
7
  == Installation
6
8
  gem install intercom
7
9
 
@@ -24,12 +26,20 @@ The API supports:
24
26
  POST,PUT,GET https://api.intercom.io/v1/users/messages
25
27
  POST https://api.intercom.io/v1/users/impressions
26
28
 
27
- ==== Users
29
+ === Examples:
28
30
 
29
- user = Intercom::User.find(:email => "bob@example.com")
31
+ ==== Users
32
+ user = Intercom::User.find_by_email("bob@example.com")
33
+ user.custom_data["average_monthly_spend"] = 1234.56
34
+ user.save
35
+ user = Intercom::User.find_by_user_id("bob@example.com")
30
36
  user = Intercom::User.create(:email => "bob@example.com", :name => "Bob Smith")
31
37
  user = Intercom::User.new(params)
32
38
  user.save
39
+ Intercom::User.all.count
40
+ Intercom::User.all.each {|user| puts %Q(#{user.email} - #{user.custom_data["average_monthly_spend"]}) }
41
+ Intercom::User.all.map {|user| user.email }
42
+
33
43
 
34
44
  ==== Messages
35
45
 
@@ -1,3 +1,6 @@
1
+ 0.0.6
2
+ - drop rest-client, use pure net/http instead
3
+
1
4
  0.0.5
2
5
  - added Intercom::User.find_by_email, Intercom::User.find_by_user_id
3
6
  - add support for Intercom::User.all.each, Intercom::User.all.map, Intercom::User.all.count
@@ -1,25 +1,25 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
+
3
4
  require "intercom/version"
4
5
 
5
- Gem::Specification.new do |s|
6
- s.name = "intercom"
7
- s.version = Intercom::VERSION
8
- s.authors = ["Ben McRedmond", "Ciaran Lee", "Darragh Curran",]
9
- s.email = ["ben@intercom.io", "ciaran@intercom.io", "darragh@intercom.io"]
10
- s.homepage = "http://www.intercom.io"
11
- s.summary = %q{Ruby bindings for the Intercom API}
12
- s.description = %Q{Intercom (https://www.intercom.io) is a customer relationship management and messaging tool for web app owners. This library wraps the api provided by Intercom. See http://docs.intercom.io/api for more details. }
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "intercom"
8
+ spec.version = Intercom::VERSION
9
+ spec.authors = ["Ben McRedmond", "Ciaran Lee", "Darragh Curran",]
10
+ spec.email = ["ben@intercom.io", "ciaran@intercom.io", "darragh@intercom.io"]
11
+ spec.homepage = "http://www.intercom.io"
12
+ spec.summary = %q{Ruby bindings for the Intercom API}
13
+ spec.description = %Q{Intercom (https://www.intercom.io) is a customer relationship management and messaging tool for web app owners. This library wraps the api provided by Intercom. See http://docs.intercom.io/api for more details. }
13
14
 
14
- s.rubyforge_project = "intercom"
15
+ spec.rubyforge_project = "intercom"
15
16
 
16
- s.files = `git ls-files`.split("\n")
17
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
20
21
 
21
- s.add_runtime_dependency "rest-client"
22
- s.add_development_dependency 'minitest'
23
- s.add_development_dependency 'rake'
24
- s.add_development_dependency 'mocha'
22
+ spec.add_development_dependency 'minitest'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'mocha'
25
25
  end
@@ -3,7 +3,7 @@ require "intercom/user_resource"
3
3
  require "intercom/user"
4
4
  require "intercom/message_thread"
5
5
  require "intercom/impression"
6
- require "rest_client"
6
+ require "intercom/request"
7
7
  require "json"
8
8
 
9
9
  ##
@@ -42,7 +42,6 @@ module Intercom
42
42
  @api_key = api_key
43
43
  end
44
44
 
45
-
46
45
  private
47
46
  def self.url_for_path(path)
48
47
  raise ArgumentError, "You must set both Intercom.app_id and Intercom.api_key to use this client. See https://github.com/intercom/intercom for usage examples." if [@app_id, @api_key].any?(&:nil?)
@@ -50,51 +49,23 @@ module Intercom
50
49
  end
51
50
 
52
51
  def self.post(path, payload_hash)
53
- execute_request(:post, path, {}, {:content_type => :json, :accept => :json}, payload_hash)
52
+ Intercom::Request.post(url_for_path(path), payload_hash).execute
54
53
  end
55
54
 
56
55
  def self.put(path, payload_hash)
57
- execute_request(:put, path, {}, {:content_type => :json, :accept => :json}, payload_hash)
56
+ Intercom::Request.put(url_for_path(path), payload_hash).execute
58
57
  end
59
58
 
60
59
  def self.get(path, params)
61
- execute_request(:get, path, params)
60
+ Intercom::Request.get(url_for_path(path), params).execute
62
61
  end
63
62
 
64
- def self.require_email_or_user_id(params)
63
+ def self.check_required_params(params, path=nil)
64
+ return if path.eql?("users")
65
65
  raise ArgumentError.new("Expected params Hash, got #{params.class}") unless params.is_a?(Hash)
66
66
  raise ArgumentError.new("Either email or user_id must be specified") unless params.keys.any? { |key| %W(email user_id).include?(key.to_s) }
67
67
  end
68
68
 
69
- def self.execute_request(method, path, params = {}, headers = {}, payload = nil)
70
- method.eql?(:get) ? require_email_or_user_id(params) : require_email_or_user_id(payload) unless path.eql?("users")
71
- args =rest_client_args(method, path, params, headers, payload)
72
- begin
73
- response = RestClient::Request.execute(args)
74
- JSON.parse(response.body)
75
- rescue RestClient::ResourceNotFound
76
- raise ResourceNotFound.new
77
- rescue RestClient::Unauthorized
78
- raise AuthenticationError.new
79
- rescue RestClient::InternalServerError
80
- raise ServerError.new
81
- end
82
- end
83
-
84
- def self.rest_client_args(method, path, params, headers, payload)
85
- url = url_for_path(path)
86
- {
87
- :method => method,
88
- :url => url,
89
- :headers => {:params => params}.merge(headers).merge(:accept => :json),
90
- :open_timeout => 10,
91
- :payload => payload.nil? ? nil : payload.to_json,
92
- :timeout => 30,
93
- :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
94
- :ssl_ca_file => File.join(File.dirname(__FILE__), 'data/cacert.pem')
95
- }
96
- end
97
-
98
69
  def self.protocol #nodoc
99
70
  @protocol
100
71
  end
@@ -54,6 +54,11 @@ module Intercom
54
54
  self.update_from_api_response(response)
55
55
  end
56
56
 
57
+ # @return [Message]
58
+ def mark_as_read!
59
+ Intercom::MessageThread.mark_as_read(:thread_id => thread_id, :email => email)
60
+ end
61
+
57
62
  # Set the content of the message for new message creation.
58
63
  # @param [String] body of the message. Supports markdown syntax
59
64
  # @return [String]
@@ -0,0 +1,89 @@
1
+ require 'cgi'
2
+ require 'net/https'
3
+
4
+ module Intercom
5
+ class Request
6
+ attr_accessor :uri, :net_http_method
7
+
8
+ def initialize(uri, net_http_method)
9
+ set_common_headers(net_http_method, uri)
10
+
11
+ self.uri = uri
12
+ self.net_http_method = net_http_method
13
+ end
14
+
15
+ def set_common_headers(method, uri)
16
+ method.basic_auth(CGI.unescape(uri.user), CGI.unescape(uri.password))
17
+ method.add_field('Accept', 'application/json')
18
+ method.add_field('AcceptEncoding', 'gzip, deflate')
19
+ end
20
+
21
+ def self.get(url, params)
22
+ uri = URI.parse(url)
23
+ new(uri, Net::HTTP::Get.new(append_query_string_to_url(uri.path, params), default_headers))
24
+ end
25
+
26
+ def self.post(url, form_data)
27
+ uri = URI.parse(url)
28
+ new(uri, method_with_body(Net::HTTP::Post, uri, form_data))
29
+ end
30
+
31
+ def self.put(url, form_data)
32
+ uri = URI.parse(url)
33
+ new(uri, method_with_body(Net::HTTP::Put, uri, form_data))
34
+ end
35
+
36
+ def self.method_with_body(http_method, uri, params)
37
+ request = http_method.send(:new, uri.request_uri, default_headers)
38
+ request.body = params.to_json
39
+ request["Content-Type"] = "application/json"
40
+ request
41
+ end
42
+
43
+ def self.default_headers
44
+ {'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/json'}
45
+ end
46
+
47
+ def client
48
+ net = Net::HTTP.new(uri.host, uri.port)
49
+ if uri.is_a?(URI::HTTPS)
50
+ net.use_ssl = uri.is_a?(URI::HTTPS)
51
+ net.verify_mode = OpenSSL::SSL::VERIFY_PEER
52
+ net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
53
+ end
54
+ net.read_timeout = 30
55
+ net.open_timeout = 10
56
+ net
57
+ end
58
+
59
+ def execute
60
+ client.start do |http|
61
+ response = http.request(net_http_method)
62
+ raise_errors_on_failure(response)
63
+ decoded = decode(response['content-encoding'], response.body)
64
+ JSON.parse(decoded)
65
+ end
66
+ end
67
+
68
+ def decode(content_encoding, body)
69
+ return body if (!body) || body.empty? || content_encoding != 'gzip'
70
+ Zlib::GzipReader.new(StringIO.new(body)).read
71
+ end
72
+
73
+ def raise_errors_on_failure(res)
74
+ if res.code.to_i.eql?(404)
75
+ raise Intercom::ResourceNotFound
76
+ elsif res.code.to_i.eql?(401)
77
+ raise Intercom::AuthenticationError
78
+ elsif res.code.to_i.eql?(500)
79
+ raise Intercom::ServerError
80
+ end
81
+ end
82
+
83
+ def self.append_query_string_to_url(url, params)
84
+ return url if params.empty?
85
+ query_string = params.map { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
86
+ url + "?#{query_string}"
87
+ end
88
+ end
89
+ end
@@ -20,7 +20,7 @@ module Intercom
20
20
  # user = Intercom::User.find_by_email("bob@example.com")
21
21
  # user.custom_data["number_of_applications"] = 11
22
22
  # user.save
23
- class User < UserResource
23
+ class User < UserResource
24
24
  ##
25
25
  # Fetches an Intercom::User from our API.
26
26
  #
@@ -202,12 +202,12 @@ class User < UserResource
202
202
  end
203
203
 
204
204
  protected
205
- def social_profiles=(social_profiles) #:nodoc:
206
- @social_profiles = social_profiles.map { |account| SocialProfile.new(account) }.freeze
207
- end
205
+ def social_profiles=(social_profiles) #:nodoc:
206
+ @social_profiles = social_profiles.map { |account| SocialProfile.new(account) }.freeze
207
+ end
208
208
 
209
- def location_data=(hash) #:nodoc:
210
- @location_data = hash.freeze
211
- end
209
+ def location_data=(hash) #:nodoc:
210
+ @location_data = hash.freeze
211
+ end
212
212
  end
213
213
  end
@@ -25,7 +25,6 @@ module Intercom
25
25
  @attributes["email"] = email
26
26
  end
27
27
 
28
-
29
28
  # @return [String] user_id
30
29
  def user_id
31
30
  @attributes["user_id"]
@@ -81,6 +80,5 @@ module Intercom
81
80
  missing = Array(required) - parameters.keys.map(&:to_s)
82
81
  raise ArgumentError.new("Missing required parameters (#{missing.join(', ')}).") unless missing.empty?
83
82
  end
84
-
85
83
  end
86
84
  end
@@ -1,3 +1,3 @@
1
1
  module Intercom #:nodoc:
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -5,6 +5,12 @@ describe "api.intercom.io dummy data requests" do
5
5
  before :each do
6
6
  Intercom.app_id = "dummy-app-id"
7
7
  Intercom.api_key = "dummy-secret-key"
8
+ Intercom.protocol = "http"
9
+ Intercom.hostname = "intercom.dev"
10
+ end
11
+
12
+ it "should get all user" do
13
+ Intercom::User.all.count.must_be :>, 0
8
14
  end
9
15
 
10
16
  it "should get a user" do
@@ -27,6 +33,20 @@ describe "api.intercom.io dummy data requests" do
27
33
  end
28
34
 
29
35
  it "should find_all messages for a user" do
30
- Intercom::MessageThread.find_all(:email => "somebody@example.com")
36
+ message_thread = Intercom::MessageThread.find_all(:email => "somebody@example.com").first
37
+ %w(thread_id read messages created_at updated_at created_by_user).each do |expected|
38
+ message_thread.send(expected).wont_be_nil
39
+ end
40
+ end
41
+
42
+ it "should mark message_thread as read" do
43
+ message_thread = Intercom::MessageThread.find_all(:email => "somebody@example.com").first
44
+ message_thread.mark_as_read!
45
+ end
46
+
47
+ it "should create some impression" do
48
+ impression = Intercom::Impression.create(:email => 'somebody@example.com')
49
+ impression.unread_messages.must_be :>, 0
50
+ impression.email.must_equal 'somebody@example.com'
31
51
  end
32
52
  end
@@ -52,15 +52,15 @@ describe "Intercom::User" do
52
52
  twitter.url.must_equal "http://twitter.com/abc"
53
53
  twitter.username.must_equal "abc"
54
54
  user.to_hash["social_profiles"].must_equal nil
55
- proc { user.social_profiles << "a" }.must_raise RuntimeError, "can't modify frozen array"
56
- proc { Intercom::User.new.social_profiles << "a" }.must_raise RuntimeError, "can't modify frozen array"
55
+ proc { user.social_profiles << "a" }.must_raise error_on_modify_frozen, "can't modify frozen array"
56
+ proc { Intercom::User.new.social_profiles << "a" }.must_raise error_on_modify_frozen, "can't modify frozen array"
57
57
  end
58
58
 
59
59
  it "has read-only location data" do
60
60
  Intercom::User.new.location_data.must_equal({})
61
61
  user = Intercom::User.new(:location_data => {"city" => "Dublin"})
62
62
  user.location_data.must_equal({"city" => "Dublin"})
63
- proc { user.location_data["change"] = "123" }.must_raise RuntimeError, "can't modify frozen hash"
63
+ proc { user.location_data["change"] = "123" }.must_raise error_on_modify_frozen, "can't modify frozen hash"
64
64
  user.to_hash["location_data"].must_equal nil
65
65
  end
66
66
 
@@ -45,13 +45,13 @@ describe Intercom do
45
45
  end
46
46
 
47
47
  it "checks for email or user id" do
48
- proc { Intercom.require_email_or_user_id("else") }.must_raise ArgumentError, "Expected params Hash, got String"
49
- proc { Intercom.require_email_or_user_id(:something => "else") }.must_raise ArgumentError, "Either email or user_id must be specified"
48
+ proc { Intercom.check_required_params("else") }.must_raise ArgumentError, "Expected params Hash, got String"
49
+ proc { Intercom.check_required_params(:something => "else") }.must_raise ArgumentError, "Either email or user_id must be specified"
50
50
  proc { Intercom.get("messages", :something => "else") }.must_raise ArgumentError, "Either email or user_id must be specified"
51
51
  proc { Intercom.put("messages", :something => "else") }.must_raise ArgumentError, "Either email or user_id must be specified"
52
52
  proc { Intercom.post("messages", :something => "else") }.must_raise ArgumentError, "Either email or user_id must be specified"
53
- Intercom.require_email_or_user_id(:email => "bob@example.com", :something => "else")
54
- Intercom.require_email_or_user_id("email" => "bob@example.com", :something => "else")
55
- Intercom.require_email_or_user_id(:user_id => "123")
53
+ Intercom.check_required_params(:email => "bob@example.com", :something => "else")
54
+ Intercom.check_required_params("email" => "bob@example.com", :something => "else")
55
+ Intercom.check_required_params(:user_id => "123")
56
56
  end
57
57
  end
@@ -87,6 +87,10 @@ def page_of_users(page=1, per_page=10)
87
87
  }
88
88
  end
89
89
 
90
+ def error_on_modify_frozen
91
+ RUBY_VERSION =~ /1.8/ ? TypeError : RuntimeError
92
+ end
93
+
90
94
  def capture_exception(&block)
91
95
  begin
92
96
  block.call
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.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,33 +11,27 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-03-13 00:00:00.000000000Z
14
+ date: 2012-06-27 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
- name: rest-client
18
- requirement: &70170925085820 !ruby/object:Gem::Requirement
17
+ name: minitest
18
+ requirement: !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
22
22
  - !ruby/object:Gem::Version
23
23
  version: '0'
24
- type: :runtime
24
+ type: :development
25
25
  prerelease: false
26
- version_requirements: *70170925085820
27
- - !ruby/object:Gem::Dependency
28
- name: minitest
29
- requirement: &70170925084400 !ruby/object:Gem::Requirement
26
+ version_requirements: !ruby/object:Gem::Requirement
30
27
  none: false
31
28
  requirements:
32
29
  - - ! '>='
33
30
  - !ruby/object:Gem::Version
34
31
  version: '0'
35
- type: :development
36
- prerelease: false
37
- version_requirements: *70170925084400
38
32
  - !ruby/object:Gem::Dependency
39
33
  name: rake
40
- requirement: &70170925083440 !ruby/object:Gem::Requirement
34
+ requirement: !ruby/object:Gem::Requirement
41
35
  none: false
42
36
  requirements:
43
37
  - - ! '>='
@@ -45,10 +39,15 @@ dependencies:
45
39
  version: '0'
46
40
  type: :development
47
41
  prerelease: false
48
- version_requirements: *70170925083440
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
49
48
  - !ruby/object:Gem::Dependency
50
49
  name: mocha
51
- requirement: &70170925082380 !ruby/object:Gem::Requirement
50
+ requirement: !ruby/object:Gem::Requirement
52
51
  none: false
53
52
  requirements:
54
53
  - - ! '>='
@@ -56,7 +55,12 @@ dependencies:
56
55
  version: '0'
57
56
  type: :development
58
57
  prerelease: false
59
- version_requirements: *70170925082380
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
60
64
  description: ! 'Intercom (https://www.intercom.io) is a customer relationship management
61
65
  and messaging tool for web app owners. This library wraps the api provided by Intercom.
62
66
  See http://docs.intercom.io/api for more details. '
@@ -69,8 +73,9 @@ extensions: []
69
73
  extra_rdoc_files: []
70
74
  files:
71
75
  - .gitignore
76
+ - .travis.yml
72
77
  - Gemfile
73
- - LICENSE
78
+ - MIT-LICENSE
74
79
  - README.rdoc
75
80
  - Rakefile
76
81
  - changes.txt
@@ -79,6 +84,7 @@ files:
79
84
  - lib/intercom.rb
80
85
  - lib/intercom/impression.rb
81
86
  - lib/intercom/message_thread.rb
87
+ - lib/intercom/request.rb
82
88
  - lib/intercom/social_profile.rb
83
89
  - lib/intercom/unix_timestamp_unwrapper.rb
84
90
  - lib/intercom/user.rb
@@ -115,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
121
  version: '0'
116
122
  requirements: []
117
123
  rubyforge_project: intercom
118
- rubygems_version: 1.8.10
124
+ rubygems_version: 1.8.23
119
125
  signing_key:
120
126
  specification_version: 3
121
127
  summary: Ruby bindings for the Intercom API
@@ -129,4 +135,3 @@ test_files:
129
135
  - spec/unit/intercom/user_spec.rb
130
136
  - spec/unit/intercom_spec.rb
131
137
  - spec/unit/spec_helper.rb
132
- has_rdoc: