Skip to content

Commit

Permalink
Merge pull request #510 from intercom/mmurray/ruby_v4
Browse files Browse the repository at this point in the history
Release version 4.0
  • Loading branch information
conorkeating authored Jan 30, 2020
2 parents d827e13 + 8409f89 commit de12dfc
Show file tree
Hide file tree
Showing 77 changed files with 2,710 additions and 1,747 deletions.
457 changes: 235 additions & 222 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Rake::TestTask.new("spec:integration") do |spec|
end

task :spec => "spec:unit"
task :default => :spec
task :default => :spec
49 changes: 27 additions & 22 deletions lib/intercom.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require "intercom/version"
# frozen_string_literal: true

require 'intercom/version'
require 'intercom/service/admin'
require 'intercom/service/company'
require 'intercom/service/contact'
require 'intercom/service/conversation'
require 'intercom/service/count'
require 'intercom/service/customer'
require 'intercom/service/event'
require 'intercom/service/message'
require 'intercom/service/note'
Expand All @@ -13,29 +14,33 @@
require 'intercom/service/segment'
require 'intercom/service/tag'
require 'intercom/service/team'
require 'intercom/service/user'
require 'intercom/service/visitor'
require 'intercom/service/user'
require 'intercom/service/lead'
require 'intercom/deprecated_resources.rb'
require 'intercom/options'
require 'intercom/client'
require "intercom/contact"
require "intercom/count"
require "intercom/customer"
require "intercom/user"
require "intercom/company"
require "intercom/note"
require "intercom/job"
require "intercom/tag"
require "intercom/segment"
require "intercom/event"
require "intercom/conversation"
require "intercom/message"
require "intercom/admin"
require "intercom/request"
require "intercom/subscription"
require "intercom/team"
require "intercom/errors"
require "intercom/visitor"
require "json"
require 'intercom/contact'
require 'intercom/user'
require 'intercom/lead'
require 'intercom/count'
require 'intercom/company'
require 'intercom/service/data_attribute'
require 'intercom/note'
require 'intercom/job'
require 'intercom/tag'
require 'intercom/segment'
require 'intercom/event'
require 'intercom/conversation'
require 'intercom/message'
require 'intercom/admin'
require 'intercom/request'
require 'intercom/subscription'
require 'intercom/team'
require 'intercom/errors'
require 'intercom/visitor'
require 'intercom/data_attribute'
require 'json'

##
# Intercom is a customer relationship management and messaging tool for web app owners
Expand Down
3 changes: 2 additions & 1 deletion lib/intercom/api_operations/archive.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require 'intercom/utils'

module Intercom
module ApiOperations
module Archive
def archive(object)
collection_name = Utils.resource_class_to_collection_name(collection_class)
@client.delete("/#{collection_name}/#{object.id}", {})
object
end
Expand Down
16 changes: 16 additions & 0 deletions lib/intercom/api_operations/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'intercom/utils'

module Intercom
module ApiOperations
module Delete
def delete(object)
@client.delete("/#{collection_name}/#{object.id}", {})
object
end

alias_method 'archive', 'delete'
end
end
end
7 changes: 5 additions & 2 deletions lib/intercom/api_operations/find.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# frozen_string_literal: true

require 'intercom/utils'

module Intercom
module ApiOperations
module Find
def find(params)
raise BadRequestError, "#{self}#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
collection_name = Utils.resource_class_to_collection_name(collection_class)

if params[:id]
id = params.delete(:id)
response = @client.get("/#{collection_name}/#{id}", params)
else
response = @client.get("/#{collection_name}", params)
end
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
raise Intercom::HttpError, 'Http Error - No response entity returned' unless response

from_api(response)
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/intercom/api_operations/find_all.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require 'intercom/client_collection_proxy'
require 'intercom/utils'

module Intercom
module ApiOperations
module FindAll

def find_all(params)
raise BadRequestError, "#find takes a hash as its parameter but you supplied #{params.inspect}" unless params.is_a? Hash
collection_name = Utils.resource_class_to_collection_name(collection_class)

finder_details = {}
if params[:id] && !type_switched_finder?(params)
finder_details[:url] = "/#{collection_name}/#{params[:id]}"
Expand All @@ -16,7 +17,7 @@ def find_all(params)
finder_details[:url] = "/#{collection_name}"
finder_details[:params] = params
end
collection_proxy_class.new(collection_name, finder_details: finder_details, client: @client)
collection_proxy_class.new(collection_name, collection_class, details: finder_details, client: @client)
end

private
Expand Down
5 changes: 4 additions & 1 deletion lib/intercom/api_operations/list.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

require 'intercom/client_collection_proxy'
require 'intercom/base_collection_proxy'
require 'intercom/utils'

module Intercom
module ApiOperations
module List
def all
ClientCollectionProxy.new(Utils.resource_class_to_collection_name(collection_class), client: @client)
collection_proxy_class.new(collection_name, collection_class, client: @client)
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/intercom/api_operations/load.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# frozen_string_literal: true

require 'intercom/utils'

module Intercom
module ApiOperations
module Load
def load(object)
collection_name = Utils.resource_class_to_collection_name(collection_class)
if object.id
response = @client.get("/#{collection_name}/#{object.id}", {})
else
raise "Cannot load #{collection_class} as it does not have a valid id."
end
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
raise Intercom::HttpError, 'Http Error - No response entity returned' unless response

object.from_response(response)
end
end
Expand Down
70 changes: 70 additions & 0 deletions lib/intercom/api_operations/nested_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

module Intercom
module ApiOperations
module NestedResource
module ClassMethods
def nested_resource_methods(resource,
path: nil,
operations: nil,
resource_plural: nil)
resource_plural ||= Utils.pluralize(resource.to_s)
path ||= resource_plural
raise ArgumentError, 'operations array required' if operations.nil?

resource_url_method = :"#{resource_plural}_url"

resource_name = Utils.resource_class_to_collection_name(self)

define_method(resource_url_method.to_sym) do |id, nested_id = nil|
url = "/#{resource_name}/#{id}/#{path}"
url += "/#{nested_id}" unless nested_id.nil?
url
end

operations.each do |operation|
case operation
when :create
define_method(:"create_#{resource}") do |params|
url = send(resource_url_method, self.id)
response = client.post(url, params)
raise_no_response_error unless response
self.class.from_api(response)
end
when :add
define_method(:"add_#{resource}") do |params|
url = send(resource_url_method, self.id)
response = client.post(url, params)
raise_no_response_error unless response
self.class.from_api(response)
end
when :delete
define_method(:"remove_#{resource}") do |params|
url = send(resource_url_method, self.id, params[:id])
response = client.delete(url, params)
raise_no_response_error unless response
self.class.from_api(response)
end
when :list
define_method(resource_plural.to_sym) do
url = send(resource_url_method, self.id)
resource_class = Utils.constantize_resource_name(resource.to_s)
resource_class.collection_proxy_class.new(resource_plural, resource_class, details: { url: url }, client: client)
end
else
raise ArgumentError, "Unknown operation: #{operation.inspect}"
end
end
end
end

def self.included(base)
base.extend(ClassMethods)
end

private def raise_no_response_error
raise Intercom::HttpError, 'Http Error - No response entity returned'
end
end
end
end
9 changes: 5 additions & 4 deletions lib/intercom/api_operations/save.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'intercom/utils'
require 'ext/sliceable_hash'

Expand All @@ -8,8 +10,8 @@ module Save
private_constant :PARAMS_NOT_PROVIDED

def create(params = PARAMS_NOT_PROVIDED)
if collection_class.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
params = Hash.new
if collection_class.ancestors.include?(Intercom::Lead) && params == PARAMS_NOT_PROVIDED
params = {}
elsif params == PARAMS_NOT_PROVIDED
raise ArgumentError, '.create requires 1 parameter'
end
Expand All @@ -20,7 +22,6 @@ def create(params = PARAMS_NOT_PROVIDED)
end

def save(object)
collection_name = Utils.resource_class_to_collection_name(collection_class)
if id_present?(object) && !posted_updates?(object)
response = @client.put("/#{collection_name}/#{object.id}", object.to_submittable_hash)
else
Expand All @@ -30,7 +31,7 @@ def save(object)
end

def identity_hash(object)
object.respond_to?(:identity_vars) ? SliceableHash.new(object.to_hash).slice(*(object.identity_vars.map(&:to_s))) : {}
object.respond_to?(:identity_vars) ? SliceableHash.new(object.to_hash).slice(*object.identity_vars.map(&:to_s)) : {}
end

private
Expand Down
9 changes: 4 additions & 5 deletions lib/intercom/api_operations/scroll.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# frozen_string_literal: true

require 'intercom/scroll_collection_proxy'
require 'intercom/utils'

module Intercom
module ApiOperations
module Scroll

def scroll()
collection_name = Utils.resource_class_to_collection_name(collection_class)
def scroll
finder_details = {}
finder_details[:url] = "/#{collection_name}"
ScrollCollectionProxy.new(collection_name, finder_details: finder_details, client: @client)
ScrollCollectionProxy.new(collection_name, collection_class, details: finder_details, client: @client)
end

end
end
end
5 changes: 3 additions & 2 deletions lib/intercom/api_operations/search.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# frozen_string_literal: true

require 'intercom/search_collection_proxy'
require 'intercom/utils'

module Intercom
module ApiOperations
module Search
def search(params)
collection_name = Utils.resource_class_to_collection_name(collection_class)
search_details = {
url: "/#{collection_name}/search",
params: params
}
SearchCollectionProxy.new(collection_name, search_details: search_details, client: @client)
SearchCollectionProxy.new(collection_name, collection_class, details: search_details, client: @client)
end
end
end
Expand Down
Loading

0 comments on commit de12dfc

Please sign in to comment.