spreadshirt_client 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  # SpreadshirtClient
3
2
 
4
3
  Use this gem to communicate with the spreadshirt API.
@@ -8,29 +7,39 @@ http://developer.spreadshirt.net
8
7
 
9
8
  Add this line to your application's Gemfile:
10
9
 
11
- gem 'spreadshirt_client'
10
+ ```
11
+ gem 'spreadshirt_client'
12
+ ```
12
13
 
13
14
  And then execute:
14
15
 
15
- $ bundle
16
+ ```
17
+ $ bundle
18
+ ```
19
+
20
+ Alternatively, you can of course install it without bundler via:
21
+
22
+ ```
23
+ $ gem install spreadshirt_client
24
+ ```
16
25
 
17
26
  ## Setup
18
27
 
19
28
  First, you need to setup your API credentials:
20
29
 
21
- <pre>
30
+ ```ruby
22
31
  SpreadshirtClient.api_key = "..."
23
32
  SpreadshirtClient.api_secret = "..."
24
33
  SpreadshirtClient.base_url = "http://api.spreadshirt.net/api/v1" # optional
25
34
  SpreadshirtClient.timeout = 5 # optional (default: 30)
26
- </pre>
35
+ ```
27
36
 
28
37
  ## Usage
29
38
 
30
39
  The DSL to interact with the spreadshirt API is similar
31
40
  to RestClient's DSL.
32
41
 
33
- <pre>
42
+ ```ruby
34
43
  # Add an article to a previously created spreadshirt basket.
35
44
  SpreadshirtClient.post "/baskets/[basket_id]/items", "<basketItem>...</basketItem>", :authorization => true
36
45
 
@@ -47,9 +56,94 @@ SpreadshirtClient.get "/baskets/[basket_id]/checkout", :authorization => true
47
56
  SpreadshirtClient.get "/shops/[shop_id]/articles", :params => { :limit => 50 }
48
57
 
49
58
  ...
50
- </pre>
59
+ ```
60
+
61
+ ## Examples
62
+
63
+ ### Creating a basket on spreadshirt
64
+
65
+ (This example is made for Rails 3.x+ applications)
66
+
67
+ Create your own basket model:
68
+
69
+ ```bash
70
+ $ rails g model Basket spreadshirt_id:string:uniq
71
+ ```
72
+
73
+ Run the migration
74
+
75
+ ```
76
+ $ rake db:migrate
77
+ ```
78
+
79
+ Modify your model:
80
+
81
+ ```ruby
82
+ # An example of a basket model
83
+
84
+ class Basket < ActiveRecord::Base
85
+ before_create :create_spreadshirt_basket
86
+
87
+ # Find our stored basket by using the id previously provided by spreadshirt
88
+ def self.find_by_spreadshirt_id(spreadshirt_id)
89
+ basket = where(:spreadshirt_id => spreadshirt_id).first
90
+
91
+ return nil unless basket
92
+
93
+ begin
94
+ SpreadshirtClient.get "/baskets/#{spreadshirt_id}", :authorization => true
95
+ rescue RestClient::ResourceNotFound
96
+ return nil
97
+ end
98
+
99
+ basket
100
+ end
101
+
102
+ private
103
+
104
+ # Create a spreadshirt basket with minimal payload
105
+ def create_spreadshirt_basket
106
+ xml = Builder::XmlMarkup.new
107
+ xml.instruct!
108
+
109
+ xml.basket :xmlns => "http://api.spreadshirt.net" do |basket|
110
+ basket.shop :id => $app_config.shop_id # Your shop id
111
+ end
112
+
113
+ self.spreadshirt_id = SpreadshirtClient.post("/baskets", xml.target!, :authorization => true).headers[:location].split("/").last
114
+ end
115
+ end
116
+ ```
117
+
118
+ Then, in your application, simply retrieve/create your customer's basket:
119
+
120
+ ```ruby
121
+ def current_basket
122
+ return @current_basket if @current_basket
123
+
124
+ # Use spreadshirt's basket id stored in a cookie to retrieve the basket
125
+ @current_basket = Basket.find_by_spreadshirt_id(cookies[:spreadshirt_basket_id])
126
+ @current_basket ||= Basket.create! # Create the basket as it doesn't exist yet
127
+
128
+ cookies[:spreadshirt_basket_id] = @current_basket.spreadshirt_id
129
+
130
+ @current_basket
131
+ end
132
+ ```
133
+
134
+ ## Resources
135
+
136
+ Spreadshirt API Docs:
137
+ - [Spreadshirt API Documentation](http://developer.spreadshirt.net/display/API/API)
138
+ - [Spreadshirt API Documentation - Basket resource](http://developer.spreadshirt.net/display/API/Basket+Resources)
139
+ - [Spreadshirt API Browser - DemoApp](http://demoapp.spreadshirt.net/apibrowser/)
140
+
141
+ SpreadshirtClient uses the RestClient gem to send requests to the Spreadshirt API:
142
+ - Take a look into the [RestClient gem documentation](https://github.com/rest-client/rest-client) to see how to handle the Spreadshirt API responses.
51
143
 
52
- Please take a look into the spreadshirt API docs for more details.
144
+ Builder Docs:
145
+ - [Github](https://github.com/jimweirich/builder)
146
+ - [RDOCS](http://builder.rubyforge.org/)
53
147
 
54
148
  ## Contributing
55
149
 
@@ -1,3 +1,3 @@
1
1
  module SpreadshirtClient
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -3,6 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
  require "spreadshirt_client/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
+ s.license = "MIT"
6
7
  s.name = "spreadshirt_client"
7
8
  s.version = SpreadshirtClient::VERSION
8
9
  s.authors = ["Benjamin Vetter"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spreadshirt_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-27 00:00:00.000000000 Z
12
+ date: 2013-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -77,7 +77,8 @@ files:
77
77
  - spreadshirt_client.gemspec
78
78
  - test/spreadshirt_client_test.rb
79
79
  homepage: https://github.com/mrkamel/spreadshirt_client
80
- licenses: []
80
+ licenses:
81
+ - MIT
81
82
  post_install_message:
82
83
  rdoc_options: []
83
84
  require_paths: