collecta_ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ lib/quick_test.rb
3
+ pkg/*
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,66 @@
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
+ ## Setup
11
+
12
+ ### Ruby on Rails Plugin
13
+
14
+ script/plugin install git://github.com/jimeh/collecta_ruby.git
15
+
16
+ And edit `config/collecta.yml` to supply your Collecta API key(s) for the each environment.
17
+
18
+ ### Plain Ruby
19
+
20
+ Supply your API to the Collecta class:
21
+
22
+ Collecta.set_key = "APIKEY"
23
+
24
+ ## Usage
25
+
26
+ Search for iPhone:
27
+
28
+ Collecta.search("iPhone")
29
+
30
+ Search with additional options/parameters:
31
+
32
+ Collecta.search("iPhone", :format => "json", :page => 2, :category => ["technology", "games"], :exclude => ["politics"])
33
+
34
+ Available parameter options are `:rpp`, `:page`, `:since_id`, `:format`, `:callback`, `:category`, and `:exclude`. The `:exclude` parameter simply excludes results from specified categories.
35
+
36
+ ## Notes / Planned Features
37
+
38
+ * 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 :)
39
+
40
+ * If you have any suggestions, comments, or complaints, please don't hesitate to contact me.
41
+
42
+
43
+ ## License
44
+
45
+ (The MIT License)
46
+
47
+ Copyright (c) 2009 Jim Myhrberg.
48
+
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of this software and associated documentation files (the
51
+ 'Software'), to deal in the Software without restriction, including
52
+ without limitation the rights to use, copy, modify, merge, publish,
53
+ distribute, sublicense, and/or sell copies of the Software, and to
54
+ permit persons to whom the Software is furnished to do so, subject to
55
+ the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be
58
+ included in all copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
61
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
62
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
63
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
64
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
65
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
66
+ 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_ruby"
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.0
@@ -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_ruby}
8
+ s.version = "0.1.0"
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-13}
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_ruby.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
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :collecta_ruby do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ development:
2
+ api_key:
3
+
4
+ test:
5
+ api_key:
6
+
7
+ production:
8
+ api_key:
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class CollectaRubyTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
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_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jim Myhrberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-13 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_ruby.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