diff --git a/lib/pipedrive-ruby.rb b/lib/pipedrive-ruby.rb index b2b7ab9..e0b0697 100644 --- a/lib/pipedrive-ruby.rb +++ b/lib/pipedrive-ruby.rb @@ -1,4 +1,4 @@ -require 'pipedrive/base' +require_relative 'pipedrive/base' require 'pipedrive/activity' require 'pipedrive/activity-type' require 'pipedrive/authorization' @@ -14,7 +14,7 @@ require 'pipedrive/person-field' require 'pipedrive/permission-set' require 'pipedrive/pipeline' -require 'pipedrive/product' +require_relative 'pipedrive/product' require 'pipedrive/product-field' require 'pipedrive/role' require 'pipedrive/search-result' @@ -30,5 +30,4 @@ module Pipedrive def self.authenticate(token) Base.authenticate(token) end - -end \ No newline at end of file +end diff --git a/lib/pipedrive/base.rb b/lib/pipedrive/base.rb index 9a0adb0..b03cff9 100644 --- a/lib/pipedrive/base.rb +++ b/lib/pipedrive/base.rb @@ -16,7 +16,7 @@ class Base < OpenStruct include HTTParty - base_uri 'api.pipedrive.com/v1' + base_uri 'https://api.pipedrive.com/v1' headers HEADERS format :json @@ -49,13 +49,14 @@ def initialize(attrs = {}) # # @param [Hash] opts # @return [Boolean] - def update(opts = {}) - res = put "#{resource_path}/#{id}", :body => opts + def update(opts = {}, custom_header = {}) + res = put "#{resource_path}/#{id}", body: opts, headers: Base.default_options[:headers].merge(custom_header) + opts = JSON.parse(opts) unless opts.is_a?(Hash) if res.success? res['data'] = Hash[res['data'].map {|k, v| [k.to_sym, v] }] @table.merge!(res['data']) else - false + Base.bad_response(res, opts) end end @@ -84,22 +85,23 @@ def new_list( attrs ) attrs['data'].is_a?(Array) ? attrs['data'].map {|data| self.new( 'data' => data ) } : [] end - def all(response = nil, options={},get_absolutely_all=false) + def all(response = nil, options = { query: {} }, get_absolutely_all = true) res = response || get(resource_path, options) if res.ok? data = res['data'].nil? ? [] : res['data'].map{|obj| new(obj)} - if get_absolutely_all && res['additional_data']['pagination'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection'] + if get_absolutely_all && res['additional_data'] && res['additional_data']['pagination'] && res['additional_data']['pagination']['more_items_in_collection'] options[:query] = options[:query].merge({:start => res['additional_data']['pagination']['next_start']}) data += self.all(nil,options,true) end data else - bad_response(res,attrs) + bad_response(res, options) end end - def create( opts = {} ) - res = post resource_path, :body => opts + def create(opts = {}, custom_header = {}) + res = post resource_path, body: opts, headers: default_options[:headers].merge(custom_header) + opts = JSON.parse(opts) unless opts.is_a?(Hash) if res.success? res['data'] = opts.merge res['data'] new(res) diff --git a/lib/pipedrive/product.rb b/lib/pipedrive/product.rb index a8baaf8..c23eae7 100644 --- a/lib/pipedrive/product.rb +++ b/lib/pipedrive/product.rb @@ -1,4 +1,12 @@ module Pipedrive class Product < Base + def update(opts = {}) + super(opts.to_json, {'Content-Type' => 'application/json'}) + end + class << self + def create(opts = {}) + super(opts.to_json, {'Content-Type' => 'application/json'}) + end + end end end