Skip to content

Commit

Permalink
added Classes to BigCartel to be more practical for regular use. BigC…
Browse files Browse the repository at this point in the history
…artel.store replaces BigCartel::Base.new
  • Loading branch information
tonkapark committed Jan 28, 2011
1 parent db6b717 commit 1860bbc
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 11 deletions.
13 changes: 10 additions & 3 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ Usage
require 'bigcartel'
require 'pp'

s = BigCartel::Base.new("tonkapark")
pp s.store

s = BigCartel.store("tonkapark")

pp s.description

s.products.each do |product|
pp product.name
pp product.img.thumb
end



Copyright
---------
Expand Down
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
require 'bundler'
Bundler::GemHelper.install_tasks :name => 'bigcartel'

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

task :default => :spec
2 changes: 1 addition & 1 deletion bigcartel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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_runtime_dependency('crack', '~> 0.1.8')
s.add_development_dependency 'rspec', '~> 2.3'
s.add_runtime_dependency('httparty', '~> 0.7.3')

s.rubyforge_project = s.name
Expand Down
128 changes: 122 additions & 6 deletions lib/bigcartel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

module BigCartel

# Alias for BigCartel::Store.find
#
# @return [BigCartel::Store]
def self.store(subdomain)
BigCartel::Store.find(subdomain)
end

# Delegate to BigCartel::Store
def self.method_missing(method, *args, &block)
return super unless client.respond_to?(method)
client.send(method, *args, &block)
end

class Base
include HTTParty
base_uri "http://api.bigcartel.com"
Expand All @@ -12,16 +25,119 @@ class Base
def initialize(subdomain)
@subdomain = subdomain
end

end

class Store < Base

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

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

def store
self.class.get("/#{@subdomain}/store.js")
end
def self.find(id)
Store.new(id, self.fetch(id))
end

def products(limit=10)
self.class.get("/#{@subdomain}/products.js?limit=#{limit}")
def products
Product.all(@id, @url)
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(data={}, store_url)
@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)/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 :name, :permalink, :url
def initialize(data={}, store_url)
@name = data['name']
@permalink = data['permalink']
@url = "#{store_url}/#{data['permalink']}"
end
end

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

def img
self.images.first
end

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

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


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

end
2 changes: 1 addition & 1 deletion lib/bigcartel/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BigCartel
VERSION = "0.0.1"
VERSION = "0.1.4"
end
17 changes: 17 additions & 0 deletions spec/bigcartel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'helper'

describe BigCartel do

describe ".store" do
it "should be a BigCartel::Store" do
BigCartel.store("tonkapark").should be_a BigCartel::Store
end

it "should have id" do
store = BigCartel.store('tonkapark')
store.id.should == 'tonkapark'
end

end

end
15 changes: 15 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$:.unshift(File.expand_path('../../lib', __FILE__))

require 'rubygems'
require 'bundler'

Bundler.require(:default, :development)

require 'bigcartel'

Rspec.configure do |config|

config.before(:each) do

end
end

0 comments on commit 1860bbc

Please sign in to comment.