spreadshirt_client 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spreadshirt_client.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Benjamin Vetter
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+
2
+ # SpreadshirtClient
3
+
4
+ Use this gem to communicate with the spreadshirt API.
5
+ http://developer.spreadshirt.net
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'spreadshirt_client'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Setup
18
+
19
+ First, you need to setup your API credentials:
20
+
21
+ <pre>
22
+ SpreadshirtClient.api_key = "..."
23
+ SpreadshirtClient.api_secret = "..."
24
+ SpreadshirtClient.base_url = "http://api.spreadshirt.net/api/v1" # optional
25
+ SpreadshirtClient.timeout = 5 # optional (default: 30)
26
+ </pre>
27
+
28
+ ## Usage
29
+
30
+ The DSL to interact with the spreadshirt API is similar
31
+ to RestClient's DSL.
32
+
33
+ <pre>
34
+ # Add an article to a previously created spreadshirt basket.
35
+ SpreadshirtClient.post "/baskets/[basket_id]/items", "<basketItem>...</basketItem>", :authorization => true
36
+
37
+ # To make a request that requires a valid spreadshirt session.
38
+ SpreadshirtClient.post "/orders", "<order>...</order>", :authorization => true, :session => "..."
39
+
40
+ # Update a line item.
41
+ SpreadshirtClient.put "/baskets/[basket_id]/items/[item_id]", "<basketItem>...</basketItem>", :authorization => true
42
+
43
+ # Retrieve the checkout url for a spreadshirt basket.
44
+ SpreadshirtClient.get "/baskets/[basket_id]/checkout", :authorization => true
45
+
46
+ # Retrieve a spreadshirt shop's articles.
47
+ SpreadshirtClient.get "/shops/[shop_id]/articles", :params => { :limit => 50 }
48
+
49
+ ...
50
+ </pre>
51
+
52
+ Please take a look into the spreadshirt API docs for more details.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
61
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "lib"
7
+ t.pattern = "test/**/*_test.rb"
8
+ t.verbose = true
9
+ end
10
+
@@ -0,0 +1,80 @@
1
+
2
+ require "spreadshirt_client/version"
3
+ require "rubygems"
4
+ require "rest-client"
5
+ require "timeout"
6
+
7
+ module SpreadshirtClient
8
+ class << self
9
+ attr_accessor :api_key, :api_secret
10
+
11
+ def timeout=(timeout)
12
+ @timeout = timeout
13
+ end
14
+
15
+ def timeout
16
+ @timeout || 30
17
+ end
18
+
19
+ def base_url=(base_url)
20
+ @base_url = base_url
21
+ end
22
+
23
+ def base_url
24
+ @base_url || "http://api.spreadshirt.net/api/v1"
25
+ end
26
+
27
+ def authorize(method, path, session = nil)
28
+ time = Time.now.to_i
29
+
30
+ authorization = [
31
+ "apiKey=\"#{api_key}\"",
32
+ "data=\"#{method} #{url_for path} #{time}\"",
33
+ "sig=\"#{Digest::SHA1.hexdigest "#{method} #{url_for path} #{time} #{api_secret}"}\""
34
+ ]
35
+
36
+ res = []
37
+ res.push "SprdAuth #{authorization.join ", "}"
38
+ res.push "sessionId=\"#{session}\"" if session
39
+
40
+ res.join ", "
41
+ end
42
+
43
+ def url_for(path)
44
+ return path if path =~ /\Ahttps?:\/\//
45
+
46
+ "#{base_url}#{path}"
47
+ end
48
+
49
+ def headers_for(method_symbol, path, options)
50
+ headers = {}
51
+
52
+ headers[:authorization] = authorize(method_for(method_symbol), path, options[:session]) if options[:authorization]
53
+
54
+ opts = options.dup
55
+ opts.delete :session
56
+ opts.merge headers
57
+ end
58
+
59
+ def method_for(method_symbol)
60
+ method_symbol.to_s.upcase
61
+ end
62
+
63
+ def put(path, payload, options = {})
64
+ Timeout::timeout(timeout) { RestClient.put url_for(path), payload, headers_for(:put, path, options) }
65
+ end
66
+
67
+ def post(path, payload, options = {})
68
+ Timeout::timeout(timeout) { RestClient.post url_for(path), payload, headers_for(:post, path, options) }
69
+ end
70
+
71
+ def get(path, options = {})
72
+ Timeout::timeout(timeout) { RestClient.get url_for(path), headers_for(:get, path, options) }
73
+ end
74
+
75
+ def delete(path, options = {})
76
+ Timeout::timeout(timeout) { RestClient.delete url_for(path), headers_for(:delete, path, options) }
77
+ end
78
+ end
79
+ end
80
+
@@ -0,0 +1,3 @@
1
+ module SpreadshirtClient
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "spreadshirt_client/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "spreadshirt_client"
7
+ s.version = SpreadshirtClient::VERSION
8
+ s.authors = ["Benjamin Vetter"]
9
+ s.email = ["vetter@flakks.com"]
10
+ s.homepage = "https://github.com/mrkamel/spreadshirt_client"
11
+ s.summary = %q{Communicate with the spreadshirt API}
12
+ s.description = %q{Communicate with the spreadshirt API using a DSL similar to the one of RestClient}
13
+
14
+ s.rubyforge_project = "spreadshirt_client"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "mocha"
23
+ s.add_dependency "rest-client"
24
+ end
25
+
@@ -0,0 +1,96 @@
1
+
2
+ require File.expand_path("../../lib/spreadshirt_client", __FILE__)
3
+ require "test/unit"
4
+ require "mocha"
5
+
6
+ class SpreadshirtClientTest < Test::Unit::TestCase
7
+ def setup
8
+ SpreadshirtClient.api_key = "test"
9
+ SpreadshirtClient.api_secret = "test"
10
+
11
+ Time.expects(:now).returns(Time.utc(2012, 1, 1)).at_least(0)
12
+ end
13
+
14
+ def test_api_key
15
+ assert_nothing_raised { SpreadshirtClient.api_key }
16
+ end
17
+
18
+ def test_api_secret
19
+ assert_nothing_raised { SpreadshirtClient.api_secret }
20
+ end
21
+
22
+ def test_base_url
23
+ assert_equal "http://api.spreadshirt.net/api/v1", SpreadshirtClient.base_url
24
+
25
+ SpreadshirtClient.base_url = "http://test.spreadshirt.net/api/v1"
26
+
27
+ assert_equal "http://test.spreadshirt.net/api/v1", SpreadshirtClient.base_url
28
+
29
+ SpreadshirtClient.base_url = "http://api.spreadshirt.net/api/v1"
30
+ end
31
+
32
+ def test_authorize
33
+ assert_equal 'SprdAuth apiKey="test", data="post http://api.spreadshirt.net/api/v1/baskets/1/items 1325376000", sig="932555fa9cc0854b03473487af521045dc12d3d0"', SpreadshirtClient.authorize(:post, "/baskets/1/items")
34
+ end
35
+
36
+ def test_authorize_with_session
37
+ regex = /\ASprdAuth apiKey=\"test\", data=\"post http:\/\/api.spreadshirt.net\/api\/v1\/orders [0-9]+\", sig=\"[0-9a-f]{40}\", sessionId=\"abcd-1234\"\Z/
38
+
39
+ assert SpreadshirtClient.authorize(:post, "/orders", "abcd-1234") =~ regex
40
+ end
41
+
42
+ def test_url_for
43
+ assert_equal "http://api.spreadshirt.net/api/v1/basket/1/items", SpreadshirtClient.url_for("/basket/1/items")
44
+ assert_equal "http://test.spreadshirt.net/api/v1/basket/1/items", SpreadshirtClient.url_for("http://test.spreadshirt.net/api/v1/basket/1/items")
45
+ end
46
+
47
+ def test_headers_for
48
+ headers = SpreadshirtClient.headers_for(:post, "/basket/1/items", :authorization => true, :session => "abcd-1234")
49
+
50
+ assert_equal [:authorization], headers.keys
51
+
52
+ assert headers[:authorization].include?("SprdAuth")
53
+ assert headers[:authorization].include?("abcd-1234")
54
+
55
+ assert_equal({ :params => { :limit => 500 } }, SpreadshirtClient.headers_for(:get, "/shops/1/articles", :params => { :limit => 500 }))
56
+ end
57
+
58
+ def test_method_for
59
+ assert_equal "POST", SpreadshirtClient.method_for(:post)
60
+ end
61
+
62
+ def test_timeout
63
+ assert_equal 30, SpreadshirtClient.timeout
64
+
65
+ SpreadshirtClient.timeout = 5
66
+
67
+ assert_equal 5, SpreadshirtClient.timeout
68
+
69
+ SpreadshirtClient.timeout = 30
70
+ end
71
+
72
+ def test_put
73
+ RestClient.expects(:put).with("http://api.spreadshirt.net/api/v1/baskets/1/items/1", "payload", :authorization => 'SprdAuth apiKey="test", data="PUT http://api.spreadshirt.net/api/v1/baskets/1/items/1 1325376000", sig="2f7a28edc68bbec83a8a580bbd1506ba9192f68e"').returns(200)
74
+
75
+ assert_equal 200, SpreadshirtClient.put("/baskets/1/items/1", "payload", :authorization => true)
76
+ end
77
+
78
+ def test_post
79
+ RestClient.expects(:post).with("http://api.spreadshirt.net/api/v1/baskets", "payload", :authorization => 'SprdAuth apiKey="test", data="POST http://api.spreadshirt.net/api/v1/baskets 1325376000", sig="04097ac87eadc5d6d0766b2dabf509ebc35eb3bc"').returns(200)
80
+
81
+ assert_equal 200, SpreadshirtClient.post("/baskets", "payload", :authorization => true)
82
+ end
83
+
84
+ def test_get
85
+ RestClient.expects(:get).with("http://api.spreadshirt.net/api/v1/orders/1", :authorization => 'SprdAuth apiKey="test", data="GET http://api.spreadshirt.net/api/v1/orders/1 1325376000", sig="7f06c7604b97b682c545262bd9acbbaf04eb8fcd", sessionId="abcd-1234"').returns(200)
86
+
87
+ assert_equal 200, SpreadshirtClient.get("/orders/1", :authorization => true, :session => "abcd-1234")
88
+ end
89
+
90
+ def test_delete
91
+ RestClient.expects(:delete).with("http://api.spreadshirt.net/api/v1/baskets/1/items/1", :authorization => 'SprdAuth apiKey="test", data="DELETE http://api.spreadshirt.net/api/v1/baskets/1/items/1 1325376000", sig="010f76d9c0a01278872a897b72bcac4daf0109cf"').returns(200)
92
+
93
+ assert_equal 200, SpreadshirtClient.delete("/baskets/1/items/1", :authorization => true)
94
+ end
95
+ end
96
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadshirt_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benjamin Vetter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rest-client
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Communicate with the spreadshirt API using a DSL similar to the one of
63
+ RestClient
64
+ email:
65
+ - vetter@flakks.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - lib/spreadshirt_client.rb
76
+ - lib/spreadshirt_client/version.rb
77
+ - spreadshirt_client.gemspec
78
+ - test/spreadshirt_client_test.rb
79
+ homepage: https://github.com/mrkamel/spreadshirt_client
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project: spreadshirt_client
99
+ rubygems_version: 1.8.23
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Communicate with the spreadshirt API
103
+ test_files: []