collecta 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/LICENSE +22 -0
- data/README.md +93 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/collecta.gemspec +54 -0
- data/init.rb +11 -0
- data/install.rb +12 -0
- data/lib/collecta.rb +74 -0
- data/tasks/collecta_ruby_tasks.rake +4 -0
- data/templates/config/collecta.yml +8 -0
- data/test/collecta_ruby_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- data/uninstall.rb +1 -0
- metadata +70 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2009 Jim Myhrberg.
|
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 NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Collecta Ruby Library
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
A quick and rough Ruby/Rails library to query info from the [Collecta][1] API. Check their [documentation][2] for more info.
|
6
|
+
|
7
|
+
[1]: http://collecta.com/
|
8
|
+
[2]: http://developer.collecta.com/
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
### Ruby on Rails Plugin
|
13
|
+
|
14
|
+
script/plugin install git://github.com/jimeh/collecta_ruby.git
|
15
|
+
|
16
|
+
### RubyGem
|
17
|
+
|
18
|
+
The gem is hosted on [Gemcutter][1]. If you don't have Gemcutter setup already, I recommend you do so by following the instructions on their [documentation][2] page.
|
19
|
+
|
20
|
+
[1]: http://gemcutter.org/
|
21
|
+
[2]: http://gemcutter.org/pages/gem_docs
|
22
|
+
|
23
|
+
Install with Gemcutter:
|
24
|
+
|
25
|
+
sudo gem install collecta
|
26
|
+
|
27
|
+
If you would prefer to not get setup for Gemcutter, install with this command instead:
|
28
|
+
|
29
|
+
sudo gem install collecta --source http://gemcutter.org/
|
30
|
+
|
31
|
+
## Setup
|
32
|
+
|
33
|
+
### Ruby on Rails Plugin
|
34
|
+
|
35
|
+
Edit `config/collecta.yml` to supply your Collecta API key(s) for the each environment.
|
36
|
+
|
37
|
+
### Ruby on Rails with RubyGem
|
38
|
+
|
39
|
+
Add the following to your `environment.rb`:
|
40
|
+
|
41
|
+
config.gem "collecta", :source => "http://gemcutter.org/"
|
42
|
+
|
43
|
+
Then create a initializer called `collecta.rb` and specify your Collecta API key the same way as bellow for Plain Ruby.
|
44
|
+
|
45
|
+
### Plain Ruby
|
46
|
+
|
47
|
+
Supply your API key to the Collecta class:
|
48
|
+
|
49
|
+
Collecta.api_key = "APIKEY"
|
50
|
+
|
51
|
+
## Usage
|
52
|
+
|
53
|
+
Search for iPhone:
|
54
|
+
|
55
|
+
Collecta.search("iPhone")
|
56
|
+
|
57
|
+
Search with additional options/parameters:
|
58
|
+
|
59
|
+
Collecta.search("iPhone", :format => "json", :page => 2, :category => ["technology", "games"], :exclude => ["politics"])
|
60
|
+
|
61
|
+
Available parameter options are `:rpp`, `:page`, `:since_id`, `:format`, `:callback`, `:category`, and `:exclude`. The `:exclude` parameter simply excludes results from specified categories.
|
62
|
+
|
63
|
+
## Notes / Warnings
|
64
|
+
|
65
|
+
* Response data from Collecta is currently returned in it's raw JSON or Atom form. Parsing the JSON responses to a Hash fails cause it's more than 20 levels deep. And parsing the Atom data fails for some reason cause XPath doesn't like Atom, and I'm no XML expert. If you know better than me, please fork, fix, and pull request :)
|
66
|
+
|
67
|
+
* If you have any suggestions, comments, or complaints, please don't hesitate to contact me.
|
68
|
+
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
(The MIT License)
|
73
|
+
|
74
|
+
Copyright (c) 2009 Jim Myhrberg.
|
75
|
+
|
76
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
77
|
+
a copy of this software and associated documentation files (the
|
78
|
+
'Software'), to deal in the Software without restriction, including
|
79
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
80
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
81
|
+
permit persons to whom the Software is furnished to do so, subject to
|
82
|
+
the following conditions:
|
83
|
+
|
84
|
+
The above copyright notice and this permission notice shall be
|
85
|
+
included in all copies or substantial portions of the Software.
|
86
|
+
|
87
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
88
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
89
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
90
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
91
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
92
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
93
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gemspec|
|
8
|
+
gemspec.name = "collecta"
|
9
|
+
gemspec.summary = "Ruby library to query the Collecta.com API."
|
10
|
+
gemspec.description = "A quick and rough Ruby/Rails library to query info from the Collecta API."
|
11
|
+
gemspec.email = "contact@jimeh.me"
|
12
|
+
gemspec.homepage = "http://github.com/jimeh/collecta_ruby"
|
13
|
+
gemspec.authors = ["Jim Myhrberg"]
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Default: run unit tests.'
|
21
|
+
task :default => :test
|
22
|
+
|
23
|
+
desc 'Test the collecta_ruby plugin.'
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Generate documentation for the collecta_ruby plugin.'
|
32
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = 'CollectaRuby'
|
35
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
36
|
+
rdoc.rdoc_files.include('README')
|
37
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.3
|
data/collecta.gemspec
ADDED
@@ -0,0 +1,54 @@
|
|
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{collecta}
|
8
|
+
s.version = "0.1.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jim Myhrberg"]
|
12
|
+
s.date = %q{2009-10-14}
|
13
|
+
s.description = %q{A quick and rough Ruby/Rails library to query info from the Collecta API.}
|
14
|
+
s.email = %q{contact@jimeh.me}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"collecta.gemspec",
|
26
|
+
"init.rb",
|
27
|
+
"install.rb",
|
28
|
+
"lib/collecta.rb",
|
29
|
+
"tasks/collecta_ruby_tasks.rake",
|
30
|
+
"templates/config/collecta.yml",
|
31
|
+
"test/collecta_ruby_test.rb",
|
32
|
+
"test/test_helper.rb",
|
33
|
+
"uninstall.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/jimeh/collecta_ruby}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{Ruby library to query the Collecta.com API.}
|
40
|
+
s.test_files = [
|
41
|
+
"test/collecta_ruby_test.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
else
|
51
|
+
end
|
52
|
+
else
|
53
|
+
end
|
54
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
|
2
|
+
if defined? Rails
|
3
|
+
require 'collecta'
|
4
|
+
config_file = File.join(RAILS_ROOT, "config", "collecta.yml")
|
5
|
+
if File.exist?(config_file)
|
6
|
+
config = YAML.load_file(config_file)
|
7
|
+
if !config[RAILS_ENV.to_s].nil? && !config[RAILS_ENV.to_s]["api_key"].nil?
|
8
|
+
Collecta.api_key = config[RAILS_ENV.to_s]["api_key"]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/install.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
dir = File.dirname(__FILE__)
|
5
|
+
templates = File.join(dir, "templates")
|
6
|
+
files = [
|
7
|
+
File.join("config", "collecta.yml")
|
8
|
+
]
|
9
|
+
|
10
|
+
files.each do |file|
|
11
|
+
FileUtils.cp File.join(templates, file), File.join(RAILS_ROOT, file) unless File.exist?(File.join(RAILS_ROOT, file))
|
12
|
+
end
|
data/lib/collecta.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "net/http"
|
3
|
+
require "uri"
|
4
|
+
require "cgi"
|
5
|
+
require "json"
|
6
|
+
require "xml"
|
7
|
+
|
8
|
+
|
9
|
+
class Collecta
|
10
|
+
|
11
|
+
@@api_key = nil
|
12
|
+
@@api_url = "http://api.collecta.com/search"
|
13
|
+
|
14
|
+
def self.api_key
|
15
|
+
@@api_key
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.api_key=(key)
|
19
|
+
@@api_key = key
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.search(q = nil, params = {})
|
23
|
+
if !q.nil? && q != ""
|
24
|
+
params[:q] = q
|
25
|
+
return self.request(params)
|
26
|
+
end
|
27
|
+
return false
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.request(options = {})
|
31
|
+
params = {}
|
32
|
+
params[:api_key] = @@api_key if options[:api_key].nil?
|
33
|
+
params[:format] = (!options[:format].nil?) ? options[:format] : "atom"
|
34
|
+
valid_params = [:q, :rpp, :page, :since_id, :callback, :category, :exclude]
|
35
|
+
valid_params.each do |param|
|
36
|
+
params[param] = options[param] if !options[param].nil?
|
37
|
+
end
|
38
|
+
request_url = self.build_url(params.clone)
|
39
|
+
if request_url
|
40
|
+
return Net::HTTP.get(URI.parse(request_url))
|
41
|
+
end
|
42
|
+
return false
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.build_url(params)
|
46
|
+
if !params[:q].nil? && !params[:api_key].nil?
|
47
|
+
# show specific categories
|
48
|
+
if params[:category].is_a?(Array) && params[:category].size > 0
|
49
|
+
params[:q] << params[:category].map { |item| " category:#{item}"}.join(" OR")
|
50
|
+
elsif params[:category].is_a?(String)
|
51
|
+
params[:q] << params[:category] && params[:category] != ""
|
52
|
+
end
|
53
|
+
params.delete(:category)
|
54
|
+
# exclude specific categories
|
55
|
+
if params[:exclude].is_a?(Array) && params[:exclude].size > 0
|
56
|
+
params[:q] << params[:exclude].map { |item| " -category:#{item}"}.join(" OR")
|
57
|
+
elsif params[:exclude].is_a?(String)
|
58
|
+
params[:q] << params[:exclude] && params[:exclude] != ""
|
59
|
+
end
|
60
|
+
params.delete(:exclude)
|
61
|
+
return @@api_url + self.build_query(params)
|
62
|
+
end
|
63
|
+
return false
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.build_query(params, first = true)
|
67
|
+
query = (first) ? "?" : ""
|
68
|
+
params[:format] = "json" if params[:format] == "hash"
|
69
|
+
return query << params.map { |name, value| "#{name}=" + CGI::escape(value).gsub("+", "%20") }.join("&")
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: collecta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jim Myhrberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-14 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A quick and rough Ruby/Rails library to query info from the Collecta API.
|
17
|
+
email: contact@jimeh.me
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- collecta.gemspec
|
32
|
+
- init.rb
|
33
|
+
- install.rb
|
34
|
+
- lib/collecta.rb
|
35
|
+
- tasks/collecta_ruby_tasks.rake
|
36
|
+
- templates/config/collecta.yml
|
37
|
+
- test/collecta_ruby_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
- uninstall.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/jimeh/collecta_ruby
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Ruby library to query the Collecta.com API.
|
68
|
+
test_files:
|
69
|
+
- test/collecta_ruby_test.rb
|
70
|
+
- test/test_helper.rb
|