Skip to content

Commit

Permalink
Merge 2f2fc38 into 793a5e7
Browse files Browse the repository at this point in the history
  • Loading branch information
jufemaiz committed Mar 17, 2014
2 parents 793a5e7 + 2f2fc38 commit 6af49d9
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 21 deletions.
19 changes: 17 additions & 2 deletions lib/pipedrive-ruby.rb
@@ -1,14 +1,29 @@
require 'pipedrive/base'
require 'pipedrive/activity'
require 'pipedrive/activity-type'
require 'pipedrive/authorization'
require 'pipedrive/currency'
require 'pipedrive/deal'
require 'pipedrive/deal-field'
require 'pipedrive/file'
require 'pipedrive/filter'
require 'pipedrive/note'
require 'pipedrive/organization'
require 'pipedrive/person'
require 'pipedrive/organization-field'
require 'pipedrive/person-field'
require 'pipedrive/product'
require 'pipedrive/permission-set'
require 'pipedrive/pipeline'
require 'pipedrive/product'
require 'pipedrive/product-field'
require 'pipedrive/role'
require 'pipedrive/search-result'
require 'pipedrive/stage'
require 'pipedrive/note'
require 'pipedrive/user'
require 'pipedrive/user-setting'
require 'pipedrive/goal'
require 'pipedrive/user-connection'
require 'pipedrive/push-notification'

module Pipedrive

Expand Down
4 changes: 4 additions & 0 deletions lib/pipedrive/activity-type.rb
@@ -0,0 +1,4 @@
module Pipedrive
class ActivityType < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/authorization.rb
@@ -0,0 +1,4 @@
module Pipedrive
class Authorization < Base
end
end
34 changes: 23 additions & 11 deletions lib/pipedrive/base.rb
Expand Up @@ -15,6 +15,7 @@ module Pipedrive
class Base < OpenStruct

include HTTParty

base_uri 'api.pipedrive.com/v1'
headers HEADERS
format :json
Expand All @@ -24,12 +25,12 @@ class Base < OpenStruct

attr_reader :data

# Create a new CloudApp::Base object.
# Create a new Pipedrive::Base object.
#
# Only used internally
#
# @param [Hash] attributes
# @return [CloudApp::Base]
# @return [Pipedrive::Base]
def initialize(attrs = {})
if attrs['data']
struct_attrs = attrs['data']
Expand All @@ -50,7 +51,12 @@ def initialize(attrs = {})
# @return [Boolean]
def update(opts = {})
res = put "#{resource_path}/#{id}", :body => opts
!!(res.success? && @table.merge!(res['data'].symbolize_keys))
if res.success?
res['data'] = Hash[res['data'].map {|k, v| [k.to_sym, v] }]
@table.merge!(res['data'])
else
false
end
end

class << self
Expand All @@ -66,7 +72,8 @@ def authenticate(token)
# Examines a bad response and raises an appropriate exception
#
# @param [HTTParty::Response] response
def bad_response(response)
def bad_response(response, params={})
puts params.inspect
if response.class == HTTParty::Response
raise HTTParty::ResponseError, response
end
Expand All @@ -77,12 +84,17 @@ def new_list( attrs )
attrs['data'].is_a?(Array) ? attrs['data'].map {|data| self.new( 'data' => data ) } : []
end

def all(response = nil, options={})
def all(response = nil, options={},get_absolutely_all=false)
res = response || get(resource_path, options)
if res.ok?
res['data'].nil? ? [] : res['data'].map{|obj| new(obj)}
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']
options[:query] = options[:query].merge({:start => res['additional_data']['pagination']['next_start']})
data += self.all(nil,options,true)
end
data
else
bad_response(res)
bad_response(res,attrs)
end
end

Expand All @@ -92,18 +104,18 @@ def create( opts = {} )
res['data'] = opts.merge res['data']
new(res)
else
bad_response(res)
bad_response(res,opts)
end
end

def find(id)
res = get "#{resource_path}/#{id}"
res.ok? ? new(res) : bad_response(res)
res.ok? ? new(res) : bad_response(res,id)
end

def find_by_name(name, opts={})
res = get "#{resource_path}/find", :query => { :term => name }.merge(opts)
res.ok? ? new_list(res) : bad_response(res)
res.ok? ? new_list(res) : bad_response(res,{:name => name}.merge(opts))
end

def resource_path
Expand Down
4 changes: 4 additions & 0 deletions lib/pipedrive/currency.rb
@@ -0,0 +1,4 @@
module Pipedrive
class Currency < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/deal-field.rb
@@ -0,0 +1,4 @@
module Pipedrive
class DealField < Base
end
end
19 changes: 16 additions & 3 deletions lib/pipedrive/deal.rb
Expand Up @@ -3,16 +3,29 @@ class Deal < Base

def add_product(opts = {})
res = post "#{resource_path}/#{id}/products", :body => opts
res.success? ? res['data']['product_attachment_id'] : bad_response(res)
res.success? ? res['data']['product_attachment_id'] : bad_response(res,opts)
end

def products
Product.all(get "#{resource_path}/#{id}/products")
end

def remove_product product_attachment_id
res = delete "#{resource_path}/#{id}/products", { :body => { :product_attachment_id => product_attachment_id } }
res.success? ? nil : bad_response(res)
res.success? ? nil : bad_response(res,product_attachment_id)
end

def activities
Activity.all(get "#{resource_path}/#{id}/activities")
end

def files
File.all(get "#{resource_path}/#{id}/files")
end

def notes(opts = {:sort_by => 'add_time', :sort_mode => 'desc'})
Note.all( get("/notes", :query => opts.merge(:deal_id => id) ) )
end

end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/file.rb
@@ -0,0 +1,4 @@
module Pipedrive
class File < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/filter.rb
@@ -0,0 +1,4 @@
module Pipedrive
class Filter < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/goal.rb
@@ -0,0 +1,4 @@
module Pipedrive
class Goal < Base
end
end
2 changes: 1 addition & 1 deletion lib/pipedrive/organization.rb
Expand Up @@ -12,7 +12,7 @@ def deals
class << self

def find_or_create_by_name(name, opts={})
find_by_name(name).first || create(opts.merge(:title => name))
find_by_name(name).first || create(opts.merge(:name => name))
end

end
Expand Down
4 changes: 4 additions & 0 deletions lib/pipedrive/permission-set.rb
@@ -0,0 +1,4 @@
module Pipedrive
class PermissionSet < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/person.rb
Expand Up @@ -8,5 +8,9 @@ def find_or_create_by_name(name, opts={})
end

end

def deals()
Deal.all(get "#{resource_path}/#{id}/deals", :everyone => 1)
end
end
end
2 changes: 1 addition & 1 deletion lib/pipedrive/pipeline.rb
Expand Up @@ -7,7 +7,7 @@ def stages
def statistics(id, start_date, end_date)
res = get("#{resource_path}/#{id}/movement_statistics",
:query => {:start_date => start_date, :end_date => end_date})
res.ok? ? new(res) : bad_response(res)
res.ok? ? new(res) : bad_response(res,{:id=>id,:start_date=>start_date,:end_date=>end_date})
end

def deals(id, stage_id)
Expand Down
4 changes: 4 additions & 0 deletions lib/pipedrive/product-field.rb
@@ -0,0 +1,4 @@
module Pipedrive
class ProductField < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/push-notification.rb
@@ -0,0 +1,4 @@
module Pipedrive
class PushNotification < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/role.rb
@@ -0,0 +1,4 @@
module Pipedrive
class Role < Base
end
end
28 changes: 28 additions & 0 deletions lib/pipedrive/search-result.rb
@@ -0,0 +1,28 @@
module Pipedrive
class SearchResult < Base

# Class Methods
class << self

def search(term, start=0, limit=nil)
res = get(resource_path, :query => { :term => term, :start => start, :limit => limit})
if res.ok?
res['data'].nil? ? [] : res['data'].map{|obj| new(obj)}
else
bad_response(res,{:term=>term,:start=>start,:limit=>limit})
end
end

def field(term, field_type, field_key, opts={})
res = get("#{resource_path}/field", :query => opts.merge(:term => term, :field_type => field_type, :field_key => field_key) )
if res.ok?
res['data'].nil? ? [] : res['data'].map{|obj| new(obj)}
else
bad_response(res,{:term=>term,:field_type=>field_type,:field_key=>field_key}.merge(opts))
end
end

end

end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/user-connection.rb
@@ -0,0 +1,4 @@
module Pipedrive
class UserConnection < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/user-setting.rb
@@ -0,0 +1,4 @@
module Pipedrive
class UserSetting < Base
end
end
4 changes: 4 additions & 0 deletions lib/pipedrive/user.rb
@@ -0,0 +1,4 @@
module Pipedrive
class User < Base
end
end
25 changes: 22 additions & 3 deletions pipedrive-ruby.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = "pipedrive-ruby"
s.version = "0.2.6"
s.version = "0.3.3"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jan Schwenzien", "Waldemar Kusnezow"]
s.date = "2013-06-17"
s.authors = ["Jan Schwenzien", "Waldemar Kusnezow", "Joel Courtney"]
s.date = "2014-01-30"
s.description = "Ruby wrapper for the Pipedrive API"
s.email = "jan@general-scripting.com"
s.extra_rdoc_files = [
Expand All @@ -24,12 +24,31 @@ Gem::Specification.new do |s|
"VERSION",
"lib/pipedrive-ruby.rb",
"lib/pipedrive/base.rb",
"lib/pipedrive/activity.rb",
"lib/pipedrive/activity-type.rb",
"lib/pipedrive/authorization.rb",
"lib/pipedrive/currency.rb",
"lib/pipedrive/deal.rb",
"lib/pipedrive/deal-field.rb",
"lib/pipedrive/file.rb",
"lib/pipedrive/filter.rb",
"lib/pipedrive/note.rb",
"lib/pipedrive/organization.rb",
"lib/pipedrive/person.rb",
"lib/pipedrive/organization-field.rb",
"lib/pipedrive/person-field.rb",
"lib/pipedrive/permission-set.rb",
"lib/pipedrive/pipeline.rb",
"lib/pipedrive/product.rb",
"lib/pipedrive/product-field.rb",
"lib/pipedrive/role.rb",
"lib/pipedrive/search-result.rb",
"lib/pipedrive/stage.rb",
"lib/pipedrive/user.rb",
"lib/pipedrive/user-setting.rb",
"lib/pipedrive/goal.rb",
"lib/pipedrive/user-connection.rb",
"lib/pipedrive/push-notification.rb",
"pipedrive-ruby.gemspec",
"test/data/create_deal_body.json",
"test/data/create_organization_body.json",
Expand Down

0 comments on commit 6af49d9

Please sign in to comment.