dawanda_client 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ doc
2
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Christoph Bünte (info@christophbuente.de)
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,97 @@
1
+ = Dawanda
2
+
3
+ == Description
4
+
5
+ The Dawanda gem provides a friendly Ruby interface to the Dawanda API
6
+
7
+ == Installation
8
+  
9
+ Installing the latest stable version is simple:
10
+
11
+ sudo gem install dawanda-dawanda_client --source=http://gems.github.com
12
+
13
+ == Usage
14
+
15
+ The Dawanda API is read-only - all you need to gain access is an API Key (available
16
+ from http://dawanda.com/apps). Once you have your API key, set it in your script:
17
+
18
+ require 'rubygems'
19
+ require 'dawanda'
20
+ Dawanda.api_key = 'foobar'
21
+ Dawanda.country = 'de'
22
+
23
+ From there, you can make any calls to the API that you need. Note: Any call to the API can fail.
24
+ In case of an error an exception is raised. So make sure your API client codes handles it.
25
+
26
+ === Users
27
+
28
+ If you're starting with a user, the easiest way is to use the Dawanda.user method:
29
+
30
+ >> user = Dawanda.user('meko')
31
+ => #<Dawanda::User:0x141bf58 @result={"name"=>"meko", "city"=>"auma", "is_seller"=>true, ...
32
+ >> user.name
33
+ => "meko"
34
+ >> user.id
35
+ => 13008
36
+
37
+ For more information about what is available for a user, check out the documentation
38
+ for Dawanda::User.
39
+
40
+ == Shops
41
+
42
+ Each user may optionally have a shop. If a user is a seller, he / she also has an
43
+ associated shop object:
44
+
45
+ >> user.seller?
46
+ => true
47
+ >> shop = user.shop
48
+ => #<Dawanda::Shop:0x14170c0 @result={"is_vacation"=>false, "name"=>"MEKO STORE" ...
49
+ >> shop.name
50
+ => "MEKO STORE"
51
+
52
+ More information about shops can be found in the documentation for Dawanda::Shop.
53
+
54
+ == Products
55
+
56
+ Dawanda::Shop, Dawanda::Category, Dawanda::Color contain multiple products:
57
+
58
+ >> shop.products
59
+ => [#<Dawanda::Product:0x1405f3c @result={"price"=>{"cents"=>2500, "currency_code"=>"EUR"}, "name"=>"Harmonie", ... ]
60
+ >> product = shop.products.first
61
+ => #<Dawanda::Product:0x1405f3c @result={"price"=>{"cents"=>2500, "currency_code"=>"EUR"}, "name"=>"Harmonie", ...
62
+ >> product.name
63
+ => "Harmonie"
64
+ >> product.description
65
+ => "Harmonie \n- Inselsymposium / Gimmlitztal \n- Eiche \n- geölt \n- L:150 / Durchmesser ca. 28cm"
66
+ >> product.product_url
67
+ => ""http://de.dawanda.com/product/1-Harmonie""
68
+ >> product.view_count
69
+ => 155
70
+ >> product.created_at
71
+ => Sat Sep 16 01:35:05 +0200 2006
72
+
73
+ See the documentation for [Dawanda::Product|http://wiki.github.com/dawanda/dawanda-api/data-type-product] for more information.
74
+
75
+
76
+ == Category
77
+
78
+ Dawanda categories are organized in a tree. To get the top level of categories, you can call:
79
+
80
+ >> category = Dawanda::Category.top.first
81
+ => #<Dawanda::Category:0x138c5d8 @result={"product_count"=>0, "name"=>"2000", "id"=>218,
82
+ >> category.parent
83
+ => nil
84
+ >> category.children
85
+ => [#<Dawanda::Category:0x1395cb4 @result={"product_count"=>4851, "name"=>"2001", "id"=>222, ...
86
+ >> category.product_count
87
+ => 100
88
+
89
+ == Exception
90
+
91
+ Chances are, that for some reason a HTTP request to the dawanda API can fail. This can have many reasons, either
92
+ missing network connection or a wrong id. In case the HTTP status code is different von 2xx or 3xx an exception containing
93
+ a meaningful error message is raised.
94
+
95
+ >> category = Dawanda::Category.find_by_id(2323)
96
+ => RuntimeError: {"error":{"status":404,"message":"The Category 2323 could not be found"}}
97
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ task :default => :test
6
+
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'test'
10
+ t.test_files = FileList["test/**/*_test.rb"]
11
+ t.verbose = true
12
+ end
13
+
14
+ namespace :doc do
15
+
16
+ desc 'generate doc'
17
+ task :generate do
18
+ `rdoc`
19
+ end
20
+
21
+ desc 'clean up all docs'
22
+ task :clean do
23
+ `rm -fr doc`
24
+ end
25
+
26
+ desc 'open docs in browser'
27
+ task :open do
28
+ `open doc/index.html`
29
+ end
30
+ end
31
+
32
+
33
+ begin
34
+ require 'jeweler'
35
+ Jeweler::Tasks.new do |gemspec|
36
+ gemspec.name = "dawanda_client"
37
+ gemspec.summary = "Provides a friendly ruby-like interface to the Dawanda API"
38
+ gemspec.description = "Describe your gem"
39
+ gemspec.email = 'info@christophbuente.de'
40
+ gemspec.homepage = "http://github.com/dawanda/dawanda_client"
41
+ gemspec.authors = ['Christoph Bünte']
42
+ end
43
+ rescue LoadError
44
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.6
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dawanda_client}
8
+ s.version = "0.1.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christoph B\303\274nte"]
12
+ s.date = %q{2009-10-13}
13
+ s.description = %q{Describe your gem}
14
+ s.email = %q{info@christophbuente.de}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "dawanda_client.gemspec",
26
+ "lib/dawanda.rb",
27
+ "lib/dawanda/category.rb",
28
+ "lib/dawanda/channel.rb",
29
+ "lib/dawanda/color.rb",
30
+ "lib/dawanda/model.rb",
31
+ "lib/dawanda/product.rb",
32
+ "lib/dawanda/request.rb",
33
+ "lib/dawanda/response.rb",
34
+ "lib/dawanda/shop.rb",
35
+ "lib/dawanda/shop_category.rb",
36
+ "lib/dawanda/user.rb",
37
+ "test/fixtures/getCategoryDetails.json",
38
+ "test/fixtures/getProductDetails.json",
39
+ "test/fixtures/getShopDetails.json",
40
+ "test/fixtures/getUserDetails.json",
41
+ "test/test_helper.rb",
42
+ "test/unit/dawanda/category_test.rb",
43
+ "test/unit/dawanda/color_test.rb",
44
+ "test/unit/dawanda/product_test.rb",
45
+ "test/unit/dawanda/shop_category_test.rb",
46
+ "test/unit/dawanda/shop_test.rb",
47
+ "test/unit/dawanda/user_test.rb",
48
+ "test/unit/dawanda_test.rb"
49
+ ]
50
+ s.homepage = %q{http://github.com/dawanda/dawanda_client}
51
+ s.rdoc_options = ["--charset=UTF-8"]
52
+ s.require_paths = ["lib"]
53
+ s.rubygems_version = %q{1.3.5}
54
+ s.summary = %q{Provides a friendly ruby-like interface to the Dawanda API}
55
+ s.test_files = [
56
+ "test/test_helper.rb",
57
+ "test/unit/dawanda/category_test.rb",
58
+ "test/unit/dawanda/color_test.rb",
59
+ "test/unit/dawanda/product_test.rb",
60
+ "test/unit/dawanda/shop_category_test.rb",
61
+ "test/unit/dawanda/shop_test.rb",
62
+ "test/unit/dawanda/user_test.rb",
63
+ "test/unit/dawanda_test.rb"
64
+ ]
65
+
66
+ if s.respond_to? :specification_version then
67
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
68
+ s.specification_version = 3
69
+
70
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
71
+ else
72
+ end
73
+ else
74
+ end
75
+ end
data/lib/dawanda.rb ADDED
@@ -0,0 +1,99 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'time'
6
+
7
+ require 'dawanda/request'
8
+ require 'dawanda/response'
9
+
10
+ require 'dawanda/model'
11
+ require 'dawanda/user'
12
+ require 'dawanda/shop'
13
+ require 'dawanda/product'
14
+ require 'dawanda/category'
15
+ require 'dawanda/shop_category'
16
+ require 'dawanda/color'
17
+ require 'dawanda/channel'
18
+
19
+ # = DaWanda Client: A friendly Ruby interface to the DaWanda API
20
+ #
21
+ # == Quick Start
22
+ #
23
+ # Getting started is easy. First, you will need a valid API key from the Dawanda
24
+ # developer site (http://dawanda.com/apps). Since the API is read-only at
25
+ # the moment, that's all you need to do.
26
+ #
27
+ # To start using the API, require the dawanda gem and set it up to use your API key:
28
+ #
29
+ # require 'rubygems'
30
+ # require 'dawanda'
31
+ #
32
+ # Dawanda.api_key = 'itsasecret'
33
+ #
34
+ # Now you can make API calls that originate from an Dawanda user:
35
+ #
36
+ # # Find a user by username
37
+ # user = Dawanda.user('meko')
38
+ #
39
+ # # Grab that user's shop information
40
+ # user.seller?
41
+ # user.shop
42
+ # user.shop.title
43
+ #
44
+ # # ... and the products in the shop
45
+ # product = user.shop.products.first
46
+ # product.title
47
+ # product.description
48
+ #
49
+ # To see what else is available for a user, check out the full documentation for
50
+ # the Dawanda::User class.
51
+ #
52
+ module Dawanda
53
+
54
+ # Set the API key for all requests
55
+ def self.api_key=(api_key)
56
+ @api_key = api_key
57
+ end
58
+
59
+ # Retrieve the API key
60
+ def self.api_key
61
+ @api_key
62
+ end
63
+
64
+ # Set the country for all requests
65
+ def self.country=(country)
66
+ @country = country
67
+ end
68
+
69
+ # Retrieve the country
70
+ def self.country
71
+ @country || 'de'
72
+ end
73
+
74
+ # Find a user by username. See Dawanda::User for more information.
75
+ def self.user(username_or_id)
76
+ User.find_by_user_id(username_or_id)
77
+ end
78
+
79
+ # Find a shop by username. See Dawanda::Shop for more information.
80
+ def self.shop(username_or_id)
81
+ Shop.find_by_user_id(username_or_id)
82
+ end
83
+
84
+ # Find a product by id. See Dawanda::Product for more information.
85
+ def self.product(product_id)
86
+ Product.find_by_id(product_id)
87
+ end
88
+
89
+ # Find a category by id. See Dawanda::Category for more information.
90
+ def self.category(category_id)
91
+ Category.find_by_id(category_id)
92
+ end
93
+
94
+ # Find a shop_category by id. See Dawanda::ShopCategory for more information.
95
+ def self.shop_category(shop_category_id)
96
+ ShopCategory.find_by_id(shop_category_id)
97
+ end
98
+
99
+ end
@@ -0,0 +1,47 @@
1
+ module Dawanda
2
+
3
+ # = Category
4
+ #
5
+ # Dawanda::Category are organized in a tree. To get the top level categories call top
6
+ #
7
+ # Represents a single Dawanda::Category - has the following attributes:
8
+ #
9
+ # [id] The unique identifier for this category
10
+ # [parent_id] This category's parent id
11
+ # [name] This category's name
12
+ # [product_count] The amount of products in this category
13
+ #
14
+ class Category
15
+
16
+ include Dawanda::Model
17
+
18
+ finder :all, '/categories/:name'
19
+ finder :all, '/categories/:parent_id/children'
20
+ finder :all, '/channels/:channel_id/categories'
21
+ finder :one, '/categories/:id'
22
+
23
+ attributes :id, :parent_id, :name, :product_count
24
+
25
+ # Get all products for this category.
26
+ def products(params = {})
27
+ @products ||= Product.find_all_by_category_id(id, params)
28
+ end
29
+
30
+ # Get all the top level categories.
31
+ def self.top(params = {})
32
+ # This is a hack, because dynamic finder generation takes at least
33
+ # one placeholder like ":name"
34
+ self.find_all_by_name('top', params)
35
+ end
36
+
37
+ # Get all child categories of this category
38
+ def children(params = {})
39
+ Category.find_all_by_parent_id(id, params)
40
+ end
41
+
42
+ # Get the parent category of this category
43
+ def parent(params = {})
44
+ (parent_id and parent_id !=0) ? Category.find_by_id(parent_id, params) : nil
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,26 @@
1
+ module Dawanda
2
+
3
+ # = Channel
4
+ #
5
+ # Dawanda::Channel are a top hierachy to categories.
6
+ #
7
+ # Represents a single Dawanda::Channel - has the following attributes:
8
+ #
9
+ # [id] The unique identifier for this channel
10
+ # [name] This channels's name
11
+ # [categories] The categories that belong to this channel
12
+ #
13
+ class Channel
14
+
15
+ include Dawanda::Model
16
+
17
+ finder :one, '/channels/:channel_id'
18
+
19
+ attributes :id, :name
20
+
21
+ # Get all categories for this channel.
22
+ def categories(params = {})
23
+ @categories ||= Category.find_all_by_channel_id(id, params)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module Dawanda
2
+
3
+ # = Color
4
+ #
5
+ # Represents a single Color - has the following attributes:
6
+ #
7
+ # [hex] This color's hex value
8
+ # [red] The color's red value from 0-255
9
+ # [green] The color's green value from 0-255
10
+ # [blue] The color's blue value from 0-255
11
+ #
12
+ class Color
13
+
14
+ include Dawanda::Model
15
+
16
+ attributes :hex, :red, :green, :blue
17
+
18
+ def products(params = {})
19
+ Product.find_all_by_hex(hex, params)
20
+ end
21
+
22
+ end
23
+ end