zootool 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Mauer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ = zootool
2
+
3
+ Provides a ruby wrapper around the Zootool API http://zootool.com/api.
4
+
5
+ Zootool is about collecting, organizing and sharing your favorite images, videos, documents and links from all over the internet. This gem gives you a simple and easy way to access everything Zootool from your ruby projects. You can see it in action in the wild at http://ryanmauer.com.
6
+
7
+ == Installation
8
+
9
+ $ gem install zootool
10
+
11
+ == Usage
12
+
13
+ Using this gem is simple. You simply create a ZooApi object:
14
+
15
+ require 'zootool'
16
+
17
+ # You'll access everything through a ZooApi object
18
+ zoo = Zootool::ZooApi.new('your_api_key_here')
19
+
20
+ And use its users, item, and items methods to access data from Zootool:
21
+
22
+ # Get items for a specific user. Returns an array of hashes with each hash representing an item.
23
+ my_items = zoo.users('rmauer').items
24
+
25
+ # Get items for all users. Returns an array of hashes.
26
+ all_items = zoo.users.items
27
+
28
+ # Get a single item by its uid. This returns a single hash.
29
+ an_item = zoo.item('1kf7s')
30
+
31
+ # Gets the most popular items today.
32
+ popular_today = zoo.items.popular('today')
33
+
34
+ Each of these methods returns either an array of hashes or a single hash (see the method documentation for more details on what is returned and what keys the hashes contain).
35
+
36
+ == More Examples
37
+
38
+ If you learn by example, this should show you pretty much everything you need to know to start using this gem:
39
+
40
+ require 'zootool'
41
+
42
+ # You'll access everything through a ZooApi object
43
+ zoo = Zootool::ZooApi.new('your_api_key_here')
44
+
45
+ # Example queries
46
+ my_items = zoo.users('rmauer').items
47
+ my_info = zoo.users('rmauer').info
48
+ my_friends = zoo.users('rmauer').friends
49
+ my_followers = zoo.users('rmauer').followers
50
+ my_inspiration_items = zoo.users('rmauer').items(:tag => 'inspiration')
51
+ all_inspiration_items = zoo.users.items(:tag => 'inspiration')
52
+ paged_items = zoo.users.items(:limit => 10, :offset => 20)
53
+ popular_today = zoo.items.popular('today')
54
+
55
+ # Using the returned data
56
+ first_title = popular_today[0]['title']
57
+ first_thumb = popular_today[0]['thumbnail']
58
+
59
+ == Features
60
+
61
+ * Query items by user
62
+ * Query items for all users
63
+ * Query popular items
64
+ * Query items by tag
65
+ * Perform paging of items using limit and offset parameters
66
+ * Access user info, friends, followers, and profiles
67
+
68
+ == Planned Features
69
+
70
+ * Authentication to allow access to private items
71
+
72
+ == Contributing to zootool
73
+
74
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
75
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
76
+ * Fork the project
77
+ * Start a feature/bugfix branch
78
+ * Commit and push until you are happy with your contribution
79
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
80
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
81
+
82
+ == Copyright
83
+
84
+ Copyright (c) 2011 Ryan Mauer. See LICENSE.txt for
85
+ further details.
86
+
@@ -0,0 +1,9 @@
1
+ module Zootool
2
+ # The base url of the Zootool API.
3
+ API_URL = "http://zootool.com/api/".freeze
4
+ end
5
+
6
+ require File.dirname(__FILE__) + '/zootool/zoo_query'
7
+ require File.dirname(__FILE__) + '/zootool/users_query'
8
+ require File.dirname(__FILE__) + '/zootool/items_query'
9
+ require File.dirname(__FILE__) + '/zootool/zootool_api'
@@ -0,0 +1,39 @@
1
+ require 'zootool/zoo_query'
2
+
3
+ module Zootool
4
+
5
+ # Performs Zootool item queries to the Zootool API. This includes queries
6
+ # for popular items.
7
+ #
8
+ # Usage: You don't need to create an object of this class directly. Instead
9
+ # use the item method on your ZootoolApi object.
10
+ #
11
+ # See http://zootool.com/api/docs/items for Items API documentation.
12
+ #
13
+ class ItemsQuery < ZooQuery
14
+
15
+ # Returns the info for an item with the specified uid.
16
+ #
17
+ # Returns a hash containing the item info.
18
+ #
19
+ def info(uid)
20
+ hash = {:uid => uid}
21
+ @api.request("items/info/?#{build_query_string(hash)}")
22
+ end
23
+
24
+ # Returns the most popular items. The time period for which to return
25
+ # popular items may be specified via the type parameter. Valid values
26
+ # are: all, month, week, today. If not specified (or if an invalid time
27
+ # period is specified), it will default to all.
28
+ #
29
+ # Returns an array of hashes containing item info.
30
+ #
31
+ def popular(type='all')
32
+ # ensure only allowed popular types
33
+ type = 'all' unless ['all','month','week','today'].include?(type)
34
+ @api.request("items/popular/?type=#{type}")
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,115 @@
1
+ require 'zootool/zoo_query'
2
+
3
+ module Zootool
4
+
5
+ # Performs User queries to the Zootool API.
6
+ #
7
+ # Usage: You don't need to create an object of this class directly. Instead
8
+ # use the users method on your ZootoolApi object.
9
+ #
10
+ # See http://zootool.com/api/docs/users for Users API documentation.
11
+ #
12
+ class UsersQuery < ZooQuery
13
+
14
+ # The username of the user for which the API should return results.
15
+ attr_accessor :username
16
+
17
+ # Initializes the UsersQuery.
18
+ def initialize api, username
19
+ @api = api
20
+ @username = username
21
+ end
22
+
23
+ # Requests and returns the items for a specific user. Private items will be
24
+ # returned if authenticated. If username is nil or empty, items for all users
25
+ # will be returned.
26
+ #
27
+ # Optional Supported Arguments: tag, offset, limit
28
+ #
29
+ # Returns an array of zero or more hashes where each hash contains the info
30
+ # for an item. The hash keys (as of the current version of the API) include:
31
+ # - uid: the unique id of the item
32
+ # - title: the title of the item
33
+ # - url: the url of the item
34
+ # - added: when the item was added
35
+ # - type: the type of the item (page, etc.)
36
+ # - views: the number of views for the item (a string)
37
+ # - likes: the number of likes the item has received (a string)
38
+ # - comments: the number of comments the item has received (a string)
39
+ # - permalink: the url of the permalink for the item
40
+ # - tinyurl: a tiny url for linking to the item
41
+ # - thumbnail: the url of the thumbnail for the item
42
+ # - image: the url of the image for the item
43
+ # - referer: the page from which the item was captured
44
+ # - description: a description of the item
45
+ # - tags: an array of tags for the item
46
+ # - inthezoo: a boolean
47
+ # - public: a boolean indicating whether or not the item is public
48
+ #
49
+ def items *args
50
+ hash = args_to_hash(args)
51
+ hash[:username] = @username unless @username.nil? || @username.empty?
52
+ @api.request("users/items/?#{build_query_string(hash)}")
53
+ end
54
+
55
+ # Returns info about a certain user. If username is nil or empty, the API
56
+ # will return an error.
57
+ #
58
+ # Returns a hash containing the info for a user. The hash keys (as of the
59
+ # current version of the API) include:
60
+ # - username: the username of the user
61
+ # - website: the url of the user's website
62
+ # - avatar: the url of the user's avatar image
63
+ # - profile: the url of the user's profile
64
+ #
65
+ def info
66
+ hash = {:username => @username}
67
+ @api.request("users/info/?#{build_query_string(hash)}")
68
+ end
69
+
70
+ # Returns a list of friends for a specific user. If username is nil or empty,
71
+ # the API will return an error.
72
+ #
73
+ # Optional Supported Arguments: offset, limit, search
74
+ #
75
+ # Returns an array of hashes containing user info.
76
+ #
77
+ def friends *args
78
+ hash = args_to_hash(args)
79
+ hash[:username] = @username
80
+ @api.request("users/friends/?#{build_query_string(hash)}")
81
+ end
82
+
83
+ # Returns a list of followers for a specific user. If username is nil or empty,
84
+ # the API will return an error.
85
+ #
86
+ # Optional Supported Arguments: offset, limit, search
87
+ #
88
+ # Returns an array of hashes containing user info.
89
+ #
90
+ def followers *args
91
+ hash = args_to_hash(args)
92
+ hash[:username] = @username
93
+ @api.request("users/followers/?#{build_query_string(hash)}")
94
+ end
95
+
96
+ # Returns a list of external profiles for a user. If username is nil or empty,
97
+ # the API will return an error.
98
+ #
99
+ # Returns an array of hashes where each hash contains info for a user's profile
100
+ # on an external site, including the following info: slug, url, title
101
+ #
102
+ def profiles
103
+ hash = {:username => @username}
104
+ @api.request("users/profiles/?#{build_query_string(hash)}")
105
+ end
106
+
107
+ # Not implemented yet.
108
+ #
109
+ def validate
110
+ return nil
111
+ end
112
+
113
+ end
114
+
115
+ end
@@ -0,0 +1,39 @@
1
+ module Zootool
2
+
3
+ # The base class for other Zootool API query classes. Simply provides
4
+ # common functionality to simplify the API querying process.
5
+ #
6
+ class ZooQuery
7
+
8
+ # The ZootoolApi to use for making requests.
9
+ attr_accessor :api
10
+
11
+ # Initializes the ZooQuery.
12
+ def initialize api
13
+ @api = api
14
+ end
15
+
16
+ # Builds a query string from a hash for use in the query URL in the form:
17
+ # key1=value1&key2=value2
18
+ #
19
+ def build_query_string hash=nil
20
+ return '' if hash.nil?
21
+ return (hash.to_a.map {|q| "#{q[0]}=#{q[1]}"}).join("&")
22
+ end
23
+
24
+ # Returns a hash containing the first element of the specified
25
+ # args array or a new hash if the args array is nil or empty.
26
+ # This is simply a convenience to simplify the API queries that
27
+ # allow optional arguments.
28
+ #
29
+ def args_to_hash args
30
+ if args.nil? || args.length < 1
31
+ return {}
32
+ else
33
+ return args[0]
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,77 @@
1
+ require 'httparty'
2
+ require 'json'
3
+
4
+ module Zootool
5
+
6
+ # A wrapper that provides access to the Zootool API.
7
+ #
8
+ class ZootoolApi
9
+
10
+ # HTTP requests are made via HTTParty
11
+ include HTTParty
12
+
13
+ # The API key for your Zootool application. Set when creating a new ZootoolApi.
14
+ attr_accessor :api_key
15
+
16
+ # Initializes the ZootoolApi object for making requests. You must specify your
17
+ # Zootool Application API key for requests to return data successfully.
18
+ #
19
+ # Example:
20
+ #
21
+ # api = Zootool::ZootoolApi.new('your_api_key_goes_here')
22
+ #
23
+ def initialize api_key
24
+ @api_key = api_key
25
+ end
26
+
27
+ # Provides a simple way to get Zootool items and info for users.
28
+ #
29
+ # Returns a UsersQuery that provides methods to easily access the items and
30
+ # other info for users. The methods of the returned UsersQuery return a
31
+ #
32
+ # Example:
33
+ #
34
+ # api = Zootool::ZootoolApi.new('your_api_key')
35
+ # user_items = api.users('rmauer').items
36
+ # all_items = api.users.items
37
+ # all_paged = api.users.items(:limit => 5, :offset => 10)
38
+ #
39
+ def users username=nil
40
+ Zootool::UsersQuery.new(self,username)
41
+ end
42
+
43
+ # Provides a simple way to get a specific item by its uid. This is just
44
+ # shorthand for the longer api.items.info method.
45
+ #
46
+ # Example:
47
+ #
48
+ # api = Zootool::ZootoolApi.new('apikey')
49
+ # item_by_uid = api.item('1kf7s')
50
+ #
51
+ def item uid
52
+ self.items.info(uid)
53
+ end
54
+
55
+ # Provides a simple way to get items.
56
+ #
57
+ # Example:
58
+ #
59
+ # api = Zootool::ZootoolApi.new('apikey')
60
+ # items = api.items.popular('week')
61
+ # item_by_uid = api.items.info('1kf7s')
62
+ #
63
+ def items
64
+ Zootool::ItemsQuery.new(self)
65
+ end
66
+
67
+ # Makes a request to the specified path and returns a Hash containing
68
+ # the result of the HTTP request.
69
+ #
70
+ def request(path)
71
+ url = "#{Zootool::API_URL}#{path}&apikey=#{@api_key}"
72
+ self.class.get(url).parsed_response
73
+ end
74
+
75
+ end
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zootool
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Mauer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-09 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: shoulda
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.0
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.6.2
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: rcov
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ description: A ruby wrapper for the Zootool API. See http://zootool.com/api for the official API documentation.
82
+ email: me@ryanmauer.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - lib/zootool.rb
92
+ - lib/zootool/items_query.rb
93
+ - lib/zootool/users_query.rb
94
+ - lib/zootool/zoo_query.rb
95
+ - lib/zootool/zootool_api.rb
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ homepage: http://github.com/ryanmauer/zootool
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 984848100567268235
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.5
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Ruby wrapper for the Zootool API
128
+ test_files: []
129
+