Skip to content

Commit

Permalink
new design plus spec tests for Store, Product and Page class
Browse files Browse the repository at this point in the history
  • Loading branch information
tonkapark committed Nov 11, 2011
1 parent cf804d5 commit f049230
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 228 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Expand Up @@ -2,3 +2,9 @@ source "http://rubygems.org"

# Specify your gem's dependencies in bigcartel.gemspec
gemspec


gem 'rake'
gem 'rspec'
gem 'webmock'
gem 'hashie'
14 changes: 11 additions & 3 deletions Rakefile
@@ -1,7 +1,15 @@
require 'rubygems'
require 'bundler'
Bundler::GemHelper.install_tasks :name => 'bigcartel'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
desc "Default: run unit tests"
task :default => :spec
task :test => :spec

desc "Run all specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = 'spec/**/*_spec.rb'
end

9 changes: 7 additions & 2 deletions bigcartel.gemspec
Expand Up @@ -12,8 +12,13 @@ Gem::Specification.new do |s|
s.summary = %q{Ruby wrapper for the Big Cartel API}
s.description = %q{A Ruby wrapper for the Big Cartel External REST API}

s.add_development_dependency 'rspec', '~> 2.3'
s.add_runtime_dependency('httparty', '~> 0.7.3')

s.add_runtime_dependency('httparty', '~> 0.8.1')
s.add_runtime_dependency('hashie', '~> 1.2.0')

s.add_development_dependency 'rspec'
s.add_development_dependency 'webmock'


s.rubyforge_project = s.name

Expand Down
232 changes: 30 additions & 202 deletions lib/bigcartel.rb
@@ -1,8 +1,7 @@
require 'httparty'
require 'uri'
require 'hashie'

module BigCartel

# Alias for BigCartel::Store.find
#
# @return [BigCartel::Store]
Expand All @@ -16,221 +15,50 @@ def self.method_missing(method, *args, &block)
client.send(method, *args, &block)
end

class Base



class Base < Hashie::Mash
include HTTParty

base_uri "http://api.bigcartel.com"
headers 'Content-Type' => 'application/json'

attr_reader :subdomain
def self.fetch(path)
response = get(path)
self.new(response)
end

def initialize(subdomain)
@subdomain = subdomain
def self.list(path, opts={})
limit = opts[:limit] || 100

response = get(path, :query => {'limit' => limit})
response.map { |c| self.new(c)}
end

end

end


class Store < Base

attr_reader :id, :description, :categories, :website, :products_count, :pages, :name, :url, :currency, :country, :artists, :currency_sign, :products

def initialize(id, data={})
@id = id
@description = data['description']
@website = data['website']
@products_count = data['products_count']
@pages = data['pages'].map{|p| Page.add(id, data['url'], p)} unless data['pages'].blank?
@name = data['name']
@url = data['url']
@currency = data['currency']['code']
@currency_sign = data['currency']['sign']
@country = data['country']['name']
@categories = data['categories'].blank? ? [] : data['categories'].map{|cat| Category.new(data['url'], cat)}
@artists = data['artists'].blank? ? [] : data['artists'].map{|cat| Artist.new(data['url'], cat)}
@products = Product.all(@id, @url)
end

def self.find(id)
Store.new(id, self.fetch(id))
end

def product_find(permalink)
#self.products.select{|f| f["permalink"] == permalink}
@products.each do |p|
if p.permalink == permalink
return p
end
end
def self.find(subdomain)
store = fetch("/#{subdomain}/store.js")
store.products = Product.all(subdomain)
store
end

def page(permalink)
self.pages.each do |p|
return p if p.permalink == permalink
end
end

def category(permalink)
self.categories.each do |cat, idx|
return cat if cat.permalink == permalink
end
end

def artist(permalink)
self.artists.each do |a, idx|
return a if a.permalink == permalink
end
end

def products_by_category(cat)
result = []
if self.categories.size > 0
self.products.each do |p|
cats = {}
if p.categories.size > 0
cats = p.categories.collect {|c| c.permalink}
result << p if cats.include?(cat)
end
end
end
result
end

def products_by_artist(slug)
result = []
if self.artists.size > 0
self.products.each do |p|
temp = {}
if p.artists.size > 0
temp = p.artists.collect {|a| a.permalink}
result << p if temp.include?(slug)
end
end
end
result
end

protected
def self.fetch(id)
get("/#{id}/store.js", :headers => {'Accept' => 'application/json'})
end
end


class Artist < Base
attr_reader :name, :url, :id, :permalink
def initialize(store_url, data={})
@name = data['name']
@url = "#{store_url}#{data['url']}"
@id = data['id']
@permalink = data['permalink']
end
end

class Category < Artist
end


class Image < Base
attr_reader :height, :width, :url, :thumb, :medium, :large
def initialize(data={})
url_parts = data['url'].scan(/(http:\/\/cache(0|1).bigcartel.com\/product_images\/\d*\/)(.*).(jpg|png|gif|jpeg)/i)

@height = data['height']
@width = data['width']
@url = data['url']
@thumb = "#{url_parts[0][0]}75.#{url_parts[0][3]}"
@medium = "#{url_parts[0][0]}175.#{url_parts[0][3]}"
@large = "#{url_parts[0][0]}300.#{url_parts[0][3]}"
end
end


class Page < Base
attr_reader :id, :name, :permalink, :url, :content, :category
def initialize(store_url, data={})
@id = data['id']
@name = data['name']
@permalink = data['permalink']
@url = "#{store_url}/#{data['permalink']}"
@content = data['content']
@category = data['category']
end

def self.add(id, store_url, page)
page_permalink = page['permalink']
Page.new(store_url, self.fetch(id, page_permalink))
end

protected
def self.fetch(id, page)
get(URI.encode("/#{id}/page/#{page}.js"), :headers => {'Accept' => 'application/json'})
end
end
def self.find(subdomain, permalink)
fetch("/#{subdomain}/page/#{permalink}.js")
end
end

class Product < Base
attr_reader :name, :permalink, :url, :full_url, :description, :artists, :on_sale, :status, :categories, :price, :position, :url, :id, :tax, :images, :shipping, :options,:default_price,:image, :image_count
def initialize(store_url, data={})
@name = data['name']
@description = data['description']
@artists = data['artists'].blank? ? [] : data['artists'].map{|cat| Artist.new(data['url'], cat)}
@on_sale= data['on_sale']
@status = data['status']
@categories = data['categories'].blank? ? [] : data['categories'].map{|cat| Category.new(data['url'], cat)}
@price = data['price']
@default_price = data['default_price']
@position = data['position']
@full_url = "#{store_url}#{data['url']}"
@url = "#{data['url']}"
@id = data['id']
@tax = data['tax']
@permalink = data['permalink']
@images = data['images'].blank? ? [] : data['images'].map{|img| Image.new(img)}
@image_count = data['images'].blank? ? 0 : data['images'].size
@shipping = data['shipping'].map{|ship| Shipping.new(ship)} unless data['shipping'].blank?
@options = data['options'].map{|opt| ProductOption.new(opt)} unless data['options'].blank?
@image = Image.new(data['images'][0]) unless data['images'].blank?
end

def has_default_option
names = {}
if self.options.size <= 1
names = self.options.collect {|x| x.name }
end
return names.include?("Default")
end

def option
self.options.first
end


def self.all(id, store_url)
self.fetch(id).map{|p| Product.new(store_url, p)}
end

protected
def self.fetch(id)
get("/#{id}/products.js", :headers => {'Accept' => 'application/json'})
end
end


class ProductOption < Base
attr_reader :name, :id,:has_custom_price, :price
def initialize(data={})
@name = data['name']
@id = data['id']
@has_custom_price = data['has_custom_price']
@price = data['price']
end
end

class Shipping < Base
attr_reader :amount_alone, :amount_combined, :country
def initialize(data={})
@amount_alone = data['amount_alone']
@amount_combined = data['amount_with_others']
@country = data['country']['code'] unless data['country'].blank?
end
end
def self.all(subdomain, opts={})
list("/#{subdomain}/products.js", opts)
end
end

end
15 changes: 15 additions & 0 deletions lib/bigcartel/base.rb
@@ -0,0 +1,15 @@
module BigCartel
class Base
include HTTParty

base_uri "http://api.bigcartel.com"
headers 'Content-Type' => 'application/json'

attr_reader :subdomain

def initialize(subdomain)
@subdomain = subdomain
end

end
end
32 changes: 32 additions & 0 deletions spec/bigcartel/page_spec.rb
@@ -0,0 +1,32 @@
require 'helper'

describe BigCartel::Store do

before do
stub_request(:get, "api.bigcartel.com/park/page/about.js").
to_return(:body=>fixture("page.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end

it "BigCartel::Page.find return a page" do
BigCartel::Page.find("park", "about")
a_request(:get, "api.bigcartel.com/park/page/about.js").
should have_been_made
end

describe 'should build hash' do
before(:each) { @store = BigCartel::Page.find("park", "about")}

it "should have a permalink" do
@store.permalink.should be_a String
@store.permalink.should eq("about")
end

it { @store.content.should be_a String }
it { @store.category.should be_a String }
it { @store.name.should be_a String }
it { @store.url.should be_a String }
it { @store.id.should be_an Integer }
end


end

0 comments on commit f049230

Please sign in to comment.