class PipedriveRuby::HttpFactory
responsable for hold all commum endpoints shared between n resources
Attributes
base_url[R]
class_name[R]
client[R]
default_param[R]
Public Class Methods
new(resource_name, client)
click to toggle source
# File lib/http_factory.rb, line 9 def initialize(resource_name, client) @client = client @class_name = resource_name @default_param = { api_token: @client.api_token } @base_url = make_base_url end
Public Instance Methods
add_follower(resource, user)
click to toggle source
# File lib/http_factory.rb, line 16 def add_follower(resource, user) custom_post(:path => "#{resource['id']}/followers", :json => {:id => resource['id'], :user_id => user['id'] }) end
all(params={})
click to toggle source
# File lib/http_factory.rb, line 21 def all(params={}) params.merge!(default_param) custom_get(:params => params) end
create(resource)
click to toggle source
# File lib/http_factory.rb, line 26 def create(resource) custom_post(:params => default_param, :json => resource) end
custom_delete(options={})
click to toggle source
# File lib/http_factory.rb, line 45 def custom_delete(options={}) options.merge!({:method => :delete}) custom_request(options) end
custom_get(options={})
click to toggle source
# File lib/http_factory.rb, line 30 def custom_get(options={}) options.merge!({:method => :get}) custom_request(options) end
custom_post(options={})
click to toggle source
# File lib/http_factory.rb, line 35 def custom_post(options={}) options.merge!({:method => :post}) custom_request(options) end
custom_put(options={})
click to toggle source
# File lib/http_factory.rb, line 40 def custom_put(options={}) options.merge!({:method => :put}) custom_request(options) end
delete_follower(resource, follower)
click to toggle source
# File lib/http_factory.rb, line 50 def delete_follower(resource, follower) custom_delete(:path => "#{resource['id']}/followers/#{follower['id']}", :json => {:id => resource['id'], :user_id => follower['id'] }) end
duplicate(resource)
click to toggle source
# File lib/http_factory.rb, line 55 def duplicate(resource) custom_post(:path => "#{resource['id']}/duplicate", :params => default_param) end
files(resource)
click to toggle source
# File lib/http_factory.rb, line 67 def files(resource) custom_get(:path => "#{resource['id']}/files") end
find(id)
click to toggle source
# File lib/http_factory.rb, line 59 def find(id) custom_get(:path => "#{id}/") end
find_by(term)
click to toggle source
# File lib/http_factory.rb, line 63 def find_by(term) custom_get(:path => "find/", :params => {:term => term}) end
followers(resource)
click to toggle source
# File lib/http_factory.rb, line 71 def followers(resource) custom_get(:path => "#{resource['id']}/followers") end
merge(resource, merge_with_resource)
click to toggle source
# File lib/http_factory.rb, line 75 def merge(resource, merge_with_resource) id = resource['id'] custom_post(:path => "#{id}/merge", :params => default_param, :json => { :id => id, :merge_with_id => merge_with_resource['id'] } ) end
permitted_users(resource)
click to toggle source
# File lib/http_factory.rb, line 86 def permitted_users(resource) custom_get(:path => "#{resource['id']}/permittedUsers") end
remove(resource)
click to toggle source
# File lib/http_factory.rb, line 90 def remove(resource) custom_delete(:path => "#{resource['id']}", :params => default_param) end
remove_many(resources)
click to toggle source
# File lib/http_factory.rb, line 94 def remove_many(resources) ids = [] resources.each do |resource| ids << resource['data']['id'] end custom_delete(:params => default_param, :json => { ids: ids.join(',') }) end
update(resource)
click to toggle source
# File lib/http_factory.rb, line 102 def update(resource) custom_put(:path => "#{resource['id']}", :params => default_param, :json => resource) end
Private Instance Methods
call_http(path, options={})
click to toggle source
# File lib/http_factory.rb, line 108 def call_http(path, options={}) http_method = options[:method] params = options[:params] case http_method when :get parse_response(HTTP.send(http_method, path, params: params)) else parse_response(HTTP.send(http_method, path, params: params, json: options[:json])) end end
custom_request(options={})
click to toggle source
# File lib/http_factory.rb, line 119 def custom_request(options={}) http_method = options[:method] return unless [:get, :post, :put, :delete].include? http_method options = set_defaults_params(options) path = "#{base_url}/" path = path.concat(options[:path]) if options[:path] call_http(path,options) end
make_base_url()
click to toggle source
# File lib/http_factory.rb, line 128 def make_base_url length = class_name.size resource_name = class_name.split('::').last resource = resource_name[0].downcase + resource_name[1, length] "#{PipedriveRuby::API_URL}/#{resource}" end
parse_response(response)
click to toggle source
# File lib/http_factory.rb, line 135 def parse_response(response) response_status = response.status.to_i begin parsed = response.parse parsed[:status_code] = response_status OpenStruct.new(parsed) rescue OpenStruct.new(status_code: response_status ,:status => false,:error_info => nil, :data => nil, :additional_data => nil) end end
set_defaults_params(options)
click to toggle source
# File lib/http_factory.rb, line 146 def set_defaults_params(options) default_options = {:params => default_param} params = options[:params] if params params.merge!(default_options[:params]) else options.merge!(default_options) end options end