basecrm 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,136 @@
1
+ # WARNING: This code is auto-generated from the BaseCRM API Discovery JSON Schema
2
+
3
+ module BaseCRM
4
+ class ProductsService
5
+ OPTS_KEYS_TO_PERSIST = Set[:name, :description, :sku, :active, :cost, :cost_currency, :prices, :max_discount, :max_markup]
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ # Retrieve all products
12
+ #
13
+ # get '/products'
14
+ #
15
+ # If you want to use filtering or sorting (see #where).
16
+ # @return [Enumerable] Paginated resource you can use to iterate over all the resources.
17
+ def all
18
+ PaginatedResource.new(self)
19
+ end
20
+
21
+ # Retrieve all products
22
+ #
23
+ # get '/products'
24
+ #
25
+ # Returns all products available to the user according to the parameters provided
26
+ #
27
+ # @param options [Hash] Search options
28
+ # @option options [String] :ids Comma-separated list of product IDs to be returned in a request.
29
+ # @option options [Integer] :organization_id Unique identifier of an organization.
30
+ # @option options [Integer] :owner_id Unique identifier of the user the product is owned by. Returns all products owned by the user.
31
+ # @option options [Integer] :page (1) Page number to start from. Page numbering starts at 1, and omitting the `page` parameter will return the first page.
32
+ # @option options [Integer] :per_page (25) Number of records to return per page. Default limit is *25* and the maximum number that can be returned is *100*.
33
+ # @option options [String] :sort_by (id:asc) A field to sort by. **Default** ordering is **ascending**. If you want to change the sort ordering to descending, append `:desc` to the field e.g. `sort_by=value:desc`.
34
+ # @option options [String] :name Name of the product.
35
+ # @option options [String] :sku SKU of the product.
36
+ # @option options [Boolean] :active Indicator of whether or not the product is active.
37
+ # @return [Array<Product>] The list of Products for the first page, unless otherwise specified.
38
+ def where(options = {})
39
+ _, _, root = @client.get("/products", options)
40
+
41
+ root[:items].map{ |item| Product.new(item[:data]) }
42
+ end
43
+
44
+
45
+ # Create a product
46
+ #
47
+ # post '/products'
48
+ #
49
+ # Create a new product
50
+ #
51
+ # @param product [Product, Hash] Either object of the Product type or Hash. This object's attributes describe the object to be created.
52
+ # @return [Product] The resulting object represting created resource.
53
+ def create(product)
54
+ validate_type!(product)
55
+
56
+ attributes = sanitize(product)
57
+ _, _, root = @client.post("/products", attributes)
58
+
59
+ Product.new(root[:data])
60
+ end
61
+
62
+
63
+ # Retrieve a single product
64
+ #
65
+ # get '/products/{id}'
66
+ #
67
+ # Returns a single product, according to the unique product ID provided
68
+ # If the specified product does not exist, the request will return an error
69
+ #
70
+ # @param id [Integer] Unique identifier of a Product
71
+ # @return [Product] Searched resource object.
72
+ def find(id)
73
+ _, _, root = @client.get("/products/#{id}")
74
+
75
+ Product.new(root[:data])
76
+ end
77
+
78
+
79
+ # Update a product
80
+ #
81
+ # put '/products/{id}'
82
+ #
83
+ # Updates product information
84
+ # If the specified product does not exist, the request will return an error
85
+ # <figure class="notice"><p>In order to modify prices used on a record, you need to supply the entire set
86
+ # <code>prices</code> are replaced every time they are used in a request
87
+ # </p></figure>
88
+ #
89
+ # @param product [Product, Hash] Either object of the Product type or Hash. This object's attributes describe the object to be updated.
90
+ # @return [Product] The resulting object represting updated resource.
91
+ def update(product)
92
+ validate_type!(product)
93
+ params = extract_params!(product, :id)
94
+ id = params[:id]
95
+
96
+ attributes = sanitize(product)
97
+ _, _, root = @client.put("/products/#{id}", attributes)
98
+
99
+ Product.new(root[:data])
100
+ end
101
+
102
+
103
+ # Delete a product
104
+ #
105
+ # delete '/products/{id}'
106
+ #
107
+ # Delete an existing product from the catalog
108
+ # Existing orders and line items are not affected
109
+ # If the specified product does not exist, the request will return an error
110
+ # This operation cannot be undone
111
+ # Products can be removed only by an account administrator
112
+ #
113
+ # @param id [Integer] Unique identifier of a Product
114
+ # @return [Boolean] Status of the operation.
115
+ def destroy(id)
116
+ status, _, _ = @client.delete("/products/#{id}")
117
+ status == 204
118
+ end
119
+
120
+
121
+ private
122
+ def validate_type!(product)
123
+ raise TypeError unless product.is_a?(Product) || product.is_a?(Hash)
124
+ end
125
+
126
+ def extract_params!(product, *args)
127
+ params = product.to_h.select{ |k, _| args.include?(k) }
128
+ raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
129
+ params
130
+ end
131
+
132
+ def sanitize(product)
133
+ product.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
134
+ end
135
+ end
136
+ end
@@ -1,3 +1,3 @@
1
1
  module BaseCRM
2
- VERSION = "1.2.3"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -0,0 +1,11 @@
1
+ FactoryGirl.define do
2
+ factory :deal_source, class: BaseCRM::DealSource do
3
+
4
+ name { Faker::Name.name }
5
+
6
+
7
+ to_create do |deal_source|
8
+ client.deal_sources.create(deal_source)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ FactoryGirl.define do
2
+ factory :lead_source, class: BaseCRM::LeadSource do
3
+
4
+ name { Faker::Name.name }
5
+
6
+
7
+ to_create do |lead_source|
8
+ client.lead_sources.create(lead_source)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ FactoryGirl.define do
2
+ factory :line_item, class: BaseCRM::LineItem do
3
+ # transient attributes, required in url
4
+ order_id nil
5
+
6
+ product_id { 5 }
7
+ value { 1599.99 }
8
+ variation { 0 }
9
+ currency { "USD" }
10
+ quantity { 1 }
11
+ price { 1599.99 }
12
+
13
+
14
+ to_create do |line_item|
15
+ client.line_items.create(line_item.order_id, line_item)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ FactoryGirl.define do
2
+ factory :order, class: BaseCRM::Order do
3
+
4
+ deal_id { 5 }
5
+ discount { 4 }
6
+
7
+
8
+ to_create do |order|
9
+ client.orders.create(order)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ FactoryGirl.define do
2
+ factory :product, class: BaseCRM::Product do
3
+
4
+ name { Faker::Name.name }
5
+ description { "Includes more storage options" }
6
+ sku { "enterprise-plan" }
7
+ active { true }
8
+ max_discount { 100 }
9
+ max_markup { 200 }
10
+ cost { 1599.99 }
11
+ cost_currency { "USD" }
12
+ prices { [{"amount"=>"1599.99", "currency"=>"USD"}, {"amount"=>"3599.99", "currency"=>"PLN"}] }
13
+
14
+
15
+ to_create do |product|
16
+ client.products.create(product)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe BaseCRM::DealSourcesService do
4
+ describe 'Responds to' do
5
+ subject { client.deal_sources }
6
+
7
+ it { should respond_to :all }
8
+ it { should respond_to :create }
9
+ it { should respond_to :destroy }
10
+ it { should respond_to :find }
11
+ it { should respond_to :update }
12
+ it { should respond_to :where }
13
+
14
+ end
15
+
16
+ describe :all do
17
+ it "returns a PaginatedResource" do
18
+ expect(client.deal_sources.all()).to be_instance_of BaseCRM::PaginatedResource
19
+ end
20
+ end
21
+
22
+ describe :where do
23
+ it "returns an array" do
24
+ expect(client.deal_sources.where(page: 1)).to be_an Array
25
+ end
26
+ end
27
+
28
+ describe :create do
29
+ it "returns instance of DealSource class" do
30
+ @deal_source = build(:deal_source)
31
+ expect(client.deal_sources.create(@deal_source)).to be_instance_of BaseCRM::DealSource
32
+ end
33
+ end
34
+
35
+ describe :find do
36
+ before :each do
37
+ @deal_source = create(:deal_source)
38
+ end
39
+
40
+ it "returns an instance of DealSource class" do
41
+ expect(client.deal_sources.find(@deal_source.id)).to be_instance_of BaseCRM::DealSource
42
+ end
43
+ end
44
+
45
+ describe :update do
46
+ it "returns an updated instance of DealSource class" do
47
+ @deal_source = create(:deal_source)
48
+ expect(client.deal_sources.update(@deal_source)).to be_instance_of BaseCRM::DealSource
49
+ end
50
+ end
51
+
52
+ describe :destroy do
53
+ it "returns true on success" do
54
+ @deal_source = create(:deal_source)
55
+ expect(client.deal_sources.destroy(@deal_source.id)).to be_truthy
56
+ end
57
+ end
58
+ end
@@ -10,7 +10,7 @@ describe BaseCRM::DealsService do
10
10
  it { should respond_to :find }
11
11
  it { should respond_to :update }
12
12
  it { should respond_to :where }
13
-
13
+
14
14
  end
15
15
 
16
16
  describe :all do
@@ -39,7 +39,7 @@ describe BaseCRM::DealsService do
39
39
 
40
40
  describe :find do
41
41
  before :each do
42
- @deal = create(:deal)
42
+ @deal = create(:deal)
43
43
  end
44
44
 
45
45
  it "returns an instance of Deal class" do
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe BaseCRM::LeadSourcesService do
4
+ describe 'Responds to' do
5
+ subject { client.lead_sources }
6
+
7
+ it { should respond_to :all }
8
+ it { should respond_to :create }
9
+ it { should respond_to :destroy }
10
+ it { should respond_to :find }
11
+ it { should respond_to :update }
12
+ it { should respond_to :where }
13
+
14
+ end
15
+
16
+ describe :all do
17
+ it "returns a PaginatedResource" do
18
+ expect(client.lead_sources.all()).to be_instance_of BaseCRM::PaginatedResource
19
+ end
20
+ end
21
+
22
+ describe :where do
23
+ it "returns an array" do
24
+ expect(client.lead_sources.where(page: 1)).to be_an Array
25
+ end
26
+ end
27
+
28
+ describe :create do
29
+ it "returns instance of LeadSource class" do
30
+ @lead_source = build(:lead_source)
31
+ expect(client.lead_sources.create(@lead_source)).to be_instance_of BaseCRM::LeadSource
32
+ end
33
+ end
34
+
35
+ describe :find do
36
+ before :each do
37
+ @lead_source = create(:lead_source)
38
+ end
39
+
40
+ it "returns an instance of LeadSource class" do
41
+ expect(client.lead_sources.find(@lead_source.id)).to be_instance_of BaseCRM::LeadSource
42
+ end
43
+ end
44
+
45
+ describe :update do
46
+ it "returns an updated instance of LeadSource class" do
47
+ @lead_source = create(:lead_source)
48
+ expect(client.lead_sources.update(@lead_source)).to be_instance_of BaseCRM::LeadSource
49
+ end
50
+ end
51
+
52
+ describe :destroy do
53
+ it "returns true on success" do
54
+ @lead_source = create(:lead_source)
55
+ expect(client.lead_sources.destroy(@lead_source.id)).to be_truthy
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe BaseCRM::LineItemsService do
4
+ before(:all) do
5
+ @product = create(:product)
6
+ @order = create_order
7
+ end
8
+ describe 'Responds to' do
9
+ subject { client.line_items }
10
+
11
+ it { should respond_to :all }
12
+ it { should respond_to :create }
13
+ it { should respond_to :destroy }
14
+ it { should respond_to :find }
15
+ it { should respond_to :where }
16
+
17
+ end
18
+
19
+ describe :all do
20
+ it "returns a PaginatedResource" do
21
+ order_id = create_order.id
22
+ expect(client.line_items.all(order_id)).to be_instance_of BaseCRM::PaginatedResource
23
+ end
24
+ end
25
+
26
+ describe :where do
27
+ it "returns an array" do
28
+ order_id = create_order.id
29
+ expect(client.line_items.where(order_id, page: 1)).to be_an Array
30
+ end
31
+ end
32
+
33
+ describe :create do
34
+ it "returns instance of LineItem class" do
35
+ order_id = create_order.id
36
+ @line_item = build(:line_item, product_id: @product.id, order_id: @order.id)
37
+ expect(client.line_items.create(order_id, @line_item)).to be_instance_of BaseCRM::LineItem
38
+ end
39
+ end
40
+
41
+ describe :find do
42
+ before :each do
43
+ @line_item = create(:line_item, product_id: @product.id, order_id: @order.id)
44
+ end
45
+
46
+ it "returns an instance of LineItem class" do
47
+ expect(client.line_items.find(@order.id, @line_item.id)).to be_instance_of BaseCRM::LineItem
48
+ end
49
+ end
50
+
51
+ describe :destroy do
52
+ it "returns true on success" do
53
+ @line_item = create(:line_item, product_id: @product.id, order_id: @order.id)
54
+ expect(client.line_items.destroy(@order.id, @line_item.id)).to be_truthy
55
+ end
56
+ end
57
+
58
+ def create_order
59
+ deal = create(:deal)
60
+ create(:order, deal_id: deal.id)
61
+ end
62
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe BaseCRM::OrdersService do
4
+ describe 'Responds to' do
5
+ subject { client.orders }
6
+
7
+ it { should respond_to :all }
8
+ it { should respond_to :create }
9
+ it { should respond_to :destroy }
10
+ it { should respond_to :find }
11
+ it { should respond_to :update }
12
+ it { should respond_to :where }
13
+
14
+ end
15
+
16
+ describe :all do
17
+ it "returns a PaginatedResource" do
18
+ expect(client.orders.all()).to be_instance_of BaseCRM::PaginatedResource
19
+ end
20
+ end
21
+
22
+ describe :where do
23
+ it "returns an array" do
24
+ expect(client.orders.where(page: 1)).to be_an Array
25
+ end
26
+ end
27
+
28
+ describe :create do
29
+ it "returns instance of Order class" do
30
+ deal_id = create(:deal).id
31
+ @order = build(:order, deal_id: deal_id)
32
+ expect(client.orders.create(@order)).to be_instance_of BaseCRM::Order
33
+ end
34
+ end
35
+
36
+ describe :find do
37
+ before :each do
38
+ @order = create_order
39
+ end
40
+
41
+ it "returns an instance of Order class" do
42
+ expect(client.orders.find(@order.id)).to be_instance_of BaseCRM::Order
43
+ end
44
+ end
45
+
46
+ describe :update do
47
+ it "returns an updated instance of Order class" do
48
+ @order = create_order
49
+ expect(client.orders.update(@order)).to be_instance_of BaseCRM::Order
50
+ end
51
+ end
52
+
53
+ describe :destroy do
54
+ it "returns true on success" do
55
+ @order = create_order
56
+ expect(client.orders.destroy(@order.id)).to be_truthy
57
+ end
58
+ end
59
+
60
+ def create_order
61
+ deal = create(:deal)
62
+ create(:order, deal_id: deal.id)
63
+ end
64
+ end