lib/intercom.rb in intercom-0.0.1 vs lib/intercom.rb in intercom-0.0.2
- old
+ new
@@ -1,53 +1,118 @@
require "intercom/version"
+require "intercom/user_resource"
+require "intercom/user"
+require "intercom/message_thread"
+require "intercom/impression"
require "rest_client"
-require 'json'
+require "json"
+##
+# Intercom is a customer relationship management and messaging tool for web app owners
+#
+# This library provides ruby bindings for the Intercom API (https://api.intercom.io)
+#
+# == Basic Usage
+# === Configure Intercom with your access credentials
+# Intercom.app_id = "my_app_id"
+# Intercom.secret_key = "my_secret_key"
+# === Make requests to the API
+# Intercom::User.find(:email => "bob@example.com")
+#
module Intercom
+ @hostname = "api.intercom.io"
+ @protocol = "https"
+ @app_id = nil
+ @secret_key = nil
+
+ ##
+ # Set the id of the application you want to interact with.
+ # When logged into your intercom console, the app_id is in the url after /apps (eg https://www.intercom.io/apps/<app-id>)
def self.app_id=(app_id)
@app_id = app_id
end
+ ##
+ # Set the secret key to gain access to your application data.
+ # When logged into your intercom console, you can view/create api keys in the settings menu
def self.secret_key=(secret_key)
@secret_key = secret_key
end
- def self.protocol
- "https"
+
+ private
+ def self.url_for_path(path)
+ raise ArgumentError, "You must set both Intercom.app_id and Intercom.secret_key to use this client. See https://github.com/intercom/intercom for usage examples." if [@app_id, @secret_key].any?(&:nil?)
+ "#{protocol}://#{@app_id}:#{@secret_key}@#{hostname}/v1/#{path}"
end
- def self.hostname
- "api.intercom.io"
+ def self.post(path, payload_hash)
+ execute_request(:post, path, {}, {:content_type => :json, :accept => :json}, payload_hash)
end
+ def self.put(path, payload_hash)
+ execute_request(:put, path, {}, {:content_type => :json, :accept => :json}, payload_hash)
+ end
+
+ def self.get(path, params)
+ execute_request(:get, path, params)
+ end
+
+ def self.require_email_or_user_id(params)
+ raise ArgumentError.new("Expected params Hash, got #{params.class}") unless params.is_a?(Hash)
+ raise ArgumentError.new("Either email or user_id must be specified") unless params.keys.any? { |key| %W(email user_id).include?(key.to_s) }
+ end
+
def self.execute_request(method, path, params = {}, headers = {}, payload = nil)
- url = "https://#{@app_id}:#{@secret_key}@api.intercom.io/v1/#{path}"
- args = {
+ method.eql?(:get) ? require_email_or_user_id(params) : require_email_or_user_id(payload)
+ args =rest_client_args(method, path, params, headers, payload)
+ begin
+ response = RestClient::Request.execute(args)
+ JSON.parse(response.body)
+ rescue RestClient::ResourceNotFound
+ raise ResourceNotFound.new
+ rescue RestClient::Unauthorized
+ raise AuthenticationError.new
+ rescue RestClient::InternalServerError
+ raise ServerError.new
+ end
+ end
+
+ def self.rest_client_args(method, path, params, headers, payload)
+ url = url_for_path(path)
+ {
:method => method,
:url => url,
- :headers => {:params => params}.merge(headers),
+ :headers => {:params => params}.merge(headers).merge(:accept => :json),
:open_timeout => 10,
- :payload => payload,
- :timeout => 30
+ :payload => payload.nil? ? nil : payload.to_json,
+ :timeout => 30,
+ :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
+ :ssl_ca_file => File.join(File.dirname(__FILE__), 'data/cacert.pem')
}
- RestClient::Request.execute(args)
end
- class User
- def initialize(response)
- @response = response
- end
+ def self.protocol #nodoc
+ @protocol
+ end
- def self.find(params)
- response = Intercom.execute_request(:get, "users", params)
- User.new(response)
- end
+ def self.protocol=(override)
+ @protocol = override
+ end
- def save
- Intercom.execute_request(:post, "users", {}, {:content_type => :json}, {"email" => "bo@example.com"}.to_json)
- end
+ def self.hostname
+ @hostname
+ end
- def method_missing(method, *args, &block)
- @response[method.to_sym]
- end
+ def self.hostname=(override)
+ @hostname = override
end
-end
+
+ class AuthenticationError < StandardError;
+ end
+
+ class ServerError < StandardError;
+ end
+
+ class ResourceNotFound < StandardError;
+ end
+end
\ No newline at end of file