Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/add-coupons-endpoint #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/celery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ module Celery
autoload :Billing, "celery/billing"
autoload :Product, "celery/product"
autoload :Tracking, "celery/tracking"
autoload :Coupon, 'celery/coupon'
autoload :EndpointMethods, "celery/endpoint_methods"
end
16 changes: 16 additions & 0 deletions lib/celery/coupon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Celery::Coupon < Celery::Base
ENDPOINT_RESOURCE = 'coupons'
ENDPOINT_RESOURCE_SINGULAR = 'coupon'

extend Celery::EndpointMethods::ClassMethods
include Celery::EndpointMethods::InstanceMethods

attr_accessor :_id, :id, :user_id, :enabled, :code,
:type, :discount, :quantity, :slugs,
:emails, :tags, :product, :updated,
:updated_date, :created, :created_date

def id
self._id
end
end
9 changes: 5 additions & 4 deletions lib/celery/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ class User < Base
:shipping_rates, :tax_rates, :nexus, :stripe, :affirm, :paypal_email,
:analytics, :flags, :confirmation_url, :twitter, :facebook, :website,
:subscription, :business, :has_affirm, :message_to_buyer, :access_token,
:emails, :has_paypalx, :confirmation_scripts
:emails, :has_paypalx, :confirmation_scripts, :salt, :secret, :hash,
:application_credit, :application_credit_used, :beta, :data, :version

def update(attrs={})
update_local_object(attrs)
response = perform_request(attrs)
response = perform_request({ user: attrs })

return response['meta']['code'] == 200 ? true : false
return !response['user'].nil?
end

def perform_request(attrs)
Expand All @@ -34,7 +35,7 @@ def me
query_string += Celery.parameterize_options
response = HTTParty.get(query_string)

self.new(response['data'])
self.new(response['user'])
end
end
end
Expand Down
50 changes: 50 additions & 0 deletions spec/celery/coupon_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'

describe Celery::Coupon, '.all' do
before :all do
@coupon = Celery::Coupon.all[0]
end

describe '.all' do
it 'returns all the existent coupons' do
expect(Celery::Coupon.all[0]).to be_kind_of(Celery::Coupon)
end
end

describe '.create' do
let(:attrs) do
{
type: 'percent',
code: Faker::Lorem.word,
discount: 10
}
end

it 'creates a new coupon' do
coupon = Celery::Coupon.create(attrs)
expect(coupon._id).to_not be_nil
end
end

describe '.get' do
it 'finds a coupon by id' do
found_coupon = Celery::Coupon.get(@coupon._id)
expect(found_coupon.code).to eq(@coupon.code)
end
end

describe '.update' do
let(:new_code) { Faker::Lorem.word }
before { @coupon.update(code: new_code) }

it 'updates a coupon' do
expect(@coupon.code).to eq(new_code)
end
end

describe '.delete' do
it 'deletes a coupon' do
expect(@coupon.destroy).to eq(true)
end
end
end
36 changes: 21 additions & 15 deletions spec/celery/order_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
require 'spec_helper'

describe Celery::Order, 'class methods' do
it 'returns all the orders from the endpoint' do
orders = Celery::Order.all
expect(orders).to be_kind_of Array
expect(orders.first).to be_kind_of Celery::Order
describe '.all' do
it 'returns all the orders from the endpoint' do
orders = Celery::Order.all
expect(orders).to be_kind_of Array
expect(orders.first).to be_kind_of Celery::Order
end
end

it 'builds the orders based on the array' do
orders = [{ "id" => "1234" }]
orders = Celery::Order.build_collection(orders)
expect(orders.first).to be_kind_of Celery::Order
expect(orders.first.id).to eq("1234")
describe '.build_collection' do
it 'builds the orders based on the array' do
orders = [{ "id" => "1234" }]
orders = Celery::Order.build_collection(orders)
expect(orders.first).to be_kind_of Celery::Order
expect(orders.first.id).to eq("1234")
end
end

it 'decodes the order' do
order = Celery::Order.decode(celery_encoded_order)
expect(order.id).to eq(celery_decoded_order.id)
describe '.decode' do
it 'decodes the order' do
order = Celery::Order.decode(celery_encoded_order)
expect(order.id).to eq(celery_decoded_order.id)
end
end

describe '.get' do
let!(:attrs) do
{
seller_id: "5388e71c5d519405004e3c3c",
seller_id: "53c69672237dd10700bb65af",
buyer: {
"email" => Faker::Internet.email,
"name"=> Faker::Name.name,
Expand All @@ -37,7 +43,7 @@
"phone"=>""
},
},
products: [ { "slug"=>"choco-cake", "name"=>"Chocholate cake", "quantity"=>1 } ]
products: [ { "slug"=>"computer", "name"=>"Glasses", "quantity"=>1 } ]
}
end

Expand Down Expand Up @@ -84,7 +90,7 @@
describe Celery::Order, 'instance methods' do
let!(:attrs) do
{
seller_id: "5388e71c5d519405004e3c3c",
seller_id: "53c69672237dd10700bb65af",
buyer: {
"email" => Faker::Internet.email,
"name"=> Faker::Name.name,
Expand Down
5 changes: 1 addition & 4 deletions spec/celery_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
require 'spec_helper'

describe Celery do
it { should be_kind_of Module }
it { should respond_to :access_token }
it { should respond_to :access_token= }
it { should respond_to :endpoint }
it { should be_kind_of Module }

it 'returns the endpoint' do
expect(Celery.endpoint).to eq("https://api.trycelery.com/v1/")
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

Celery.access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI1Mzg4ZTcxYzVkNTE5NDA1MDA0ZTNjM2MifQ.Vw1dikATVaAK5YLZFTPOXKpJJexa8T6Ni1RuE3nh1nAU"
Celery.access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI1M2M2OTY3MjIzN2RkMTA3MDBiYjY1YWYifQ.CbBFs_TdCfiVXmsEwVS5_mU47Un3j4NnVxHm_3Scp24'

I18n.enforce_available_locales = true

Expand Down