bigcartel 0.0.1 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.mkd CHANGED
@@ -12,9 +12,16 @@ Usage
12
12
  require 'bigcartel'
13
13
  require 'pp'
14
14
 
15
- s = BigCartel::Base.new("tonkapark")
16
- pp s.store
17
-
15
+ s = BigCartel.store("tonkapark")
16
+
17
+ pp s.description
18
+
19
+ s.products.each do |product|
20
+ pp product.name
21
+ pp product.img.thumb
22
+ end
23
+
24
+
18
25
 
19
26
  Copyright
20
27
  ---------
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks :name => 'bigcartel'
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{Ruby wrapper for the Big Cartel API}
13
13
  s.description = %q{A Ruby wrapper for the Big Cartel External REST API}
14
14
 
15
- s.add_runtime_dependency('crack', '~> 0.1.8')
15
+ s.add_development_dependency 'rspec', '~> 2.3'
16
16
  s.add_runtime_dependency('httparty', '~> 0.7.3')
17
17
 
18
18
  s.rubyforge_project = s.name
@@ -1,27 +1,143 @@
1
- require 'httparty'
2
-
3
- module BigCartel
4
-
5
- class Base
6
- include HTTParty
7
- base_uri "http://api.bigcartel.com"
8
- headers 'Content-Type' => 'application/json'
9
-
10
- attr_reader :subdomain
11
-
12
- def initialize(subdomain)
13
- @subdomain = subdomain
14
- end
15
-
16
-
17
- def store
18
- self.class.get("/#{@subdomain}/store.js")
19
- end
20
-
21
- def products(limit=10)
22
- self.class.get("/#{@subdomain}/products.js?limit=#{limit}")
23
- end
24
-
25
-
26
- end
27
- end
1
+ require 'httparty'
2
+
3
+ module BigCartel
4
+
5
+ # Alias for BigCartel::Store.find
6
+ #
7
+ # @return [BigCartel::Store]
8
+ def self.store(subdomain)
9
+ BigCartel::Store.find(subdomain)
10
+ end
11
+
12
+ # Delegate to BigCartel::Store
13
+ def self.method_missing(method, *args, &block)
14
+ return super unless client.respond_to?(method)
15
+ client.send(method, *args, &block)
16
+ end
17
+
18
+ class Base
19
+ include HTTParty
20
+ base_uri "http://api.bigcartel.com"
21
+ headers 'Content-Type' => 'application/json'
22
+
23
+ attr_reader :subdomain
24
+
25
+ def initialize(subdomain)
26
+ @subdomain = subdomain
27
+ end
28
+
29
+ end
30
+
31
+ class Store < Base
32
+
33
+ attr_reader :id, :description, :categories, :website, :products_count, :pages, :name, :url, :currency, :country
34
+
35
+ def initialize(id, data={})
36
+ @id = id
37
+ @description = data['description']
38
+ @website = data['website']
39
+ @products_count = data['products_count']
40
+ @pages = data['pages'].map{|p| Page.new(p, data['url'])} unless data['pages'].blank?
41
+ @name = data['name']
42
+ @url = data['url']
43
+ @currency = data['currency']['code']
44
+ @country = data['country']['name']
45
+ @categories = data['categories'].map{|cat| Category.new(cat,data['url'])} unless data['categories'].blank?
46
+ end
47
+
48
+ def self.find(id)
49
+ Store.new(id, self.fetch(id))
50
+ end
51
+
52
+ def products
53
+ Product.all(@id, @url)
54
+ end
55
+
56
+ protected
57
+ def self.fetch(id)
58
+ get("/#{id}/store.js", :headers => {'Accept' => 'application/json'})
59
+ end
60
+ end
61
+
62
+
63
+ class Artist < Base
64
+ attr_reader :name, :url, :id, :permalink
65
+ def initialize(data={}, store_url)
66
+ @name = data['name']
67
+ @url = "#{store_url}#{data['url']}"
68
+ @id = data['id']
69
+ @permalink = data['permalink']
70
+ end
71
+ end
72
+
73
+ class Category < Artist
74
+ end
75
+
76
+
77
+ class Image < Base
78
+ attr_reader :height, :width, :url, :thumb, :medium, :large
79
+ def initialize(data={})
80
+ url_parts = data['url'].scan(/(http:\/\/cache(0|1).bigcartel.com\/product_images\/\d*\/)(.*).(jpg|png|gif)/i)
81
+
82
+ @height = data['height']
83
+ @width = data['width']
84
+ @url = data['url']
85
+ @thumb = "#{url_parts[0][0]}75.#{url_parts[0][3]}"
86
+ @medium = "#{url_parts[0][0]}175.#{url_parts[0][3]}"
87
+ @large = "#{url_parts[0][0]}300.#{url_parts[0][3]}"
88
+ end
89
+ end
90
+
91
+
92
+ class Page < Base
93
+ attr_reader :name, :permalink, :url
94
+ def initialize(data={}, store_url)
95
+ @name = data['name']
96
+ @permalink = data['permalink']
97
+ @url = "#{store_url}/#{data['permalink']}"
98
+ end
99
+ end
100
+
101
+ class Product < Base
102
+ attr_reader :name, :permalink, :url, :description, :artists, :on_sale, :status, :categories, :price, :position, :url, :id, :tax, :images, :shipping
103
+ def initialize(data={}, store_url)
104
+ @name = data['name']
105
+ @description = data['description']
106
+ @artists = data['artists'].map{|cat| Artist.new(cat,data['url'])} unless data['artists'].blank?
107
+ @on_sale= data['on_sale']
108
+ @status = data['status']
109
+ @categories = data['categories'].map{|cat| Category.new(cat,data['url'])} unless data['categories'].blank?
110
+ @price = data['price']
111
+ @position = data['position']
112
+ @url = "#{store_url}#{data['url']}"
113
+ @id = data['id']
114
+ @tax = data['tax']
115
+ @permalink = data['permalink']
116
+ @images = data['images'].blank? ? [] : data['images'].map{|img| Image.new(img)}
117
+ @shipping = data['shipping']
118
+ end
119
+
120
+ def img
121
+ self.images.first
122
+ end
123
+
124
+ def self.all(id, store_url)
125
+ self.fetch(id).map{|p| Product.new(p, store_url)}
126
+ end
127
+
128
+ protected
129
+ def self.fetch(id)
130
+ get("/#{id}/products.js", :headers => {'Accept' => 'application/json'})
131
+ end
132
+ end
133
+
134
+
135
+ class ProductOption < Base
136
+ attr_reader :name, :id
137
+ def initialize(data={})
138
+ @name = data['name']
139
+ @id = data['id']
140
+ end
141
+ end
142
+
143
+ end
@@ -1,3 +1,3 @@
1
- module BigCartel
2
- VERSION = "0.0.1"
3
- end
1
+ module BigCartel
2
+ VERSION = "0.1.4"
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ describe BigCartel do
4
+
5
+ describe ".store" do
6
+ it "should be a BigCartel::Store" do
7
+ BigCartel.store("tonkapark").should be_a BigCartel::Store
8
+ end
9
+
10
+ it "should have id" do
11
+ store = BigCartel.store('tonkapark')
12
+ store.id.should == 'tonkapark'
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,15 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require(:default, :development)
7
+
8
+ require 'bigcartel'
9
+
10
+ Rspec.configure do |config|
11
+
12
+ config.before(:each) do
13
+
14
+ end
15
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 0
8
7
  - 1
9
- version: 0.0.1
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Anderson
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-26 00:00:00 -06:00
17
+ date: 2011-01-28 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: crack
21
+ name: rspec
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -26,11 +26,10 @@ dependencies:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
- - 0
30
- - 1
31
- - 8
32
- version: 0.1.8
33
- type: :runtime
29
+ - 2
30
+ - 3
31
+ version: "2.3"
32
+ type: :development
34
33
  version_requirements: *id001
35
34
  - !ruby/object:Gem::Dependency
36
35
  name: httparty
@@ -64,6 +63,8 @@ files:
64
63
  - bigcartel.gemspec
65
64
  - lib/bigcartel.rb
66
65
  - lib/bigcartel/version.rb
66
+ - spec/bigcartel_spec.rb
67
+ - spec/helper.rb
67
68
  has_rdoc: true
68
69
  homepage: ""
69
70
  licenses: []