duck_duck_go_api 0.0.1

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,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ InstalledFiles
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+
20
+ # YARD artifacts
21
+ .yardoc
22
+ _yardoc
23
+ doc/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx-19mode
7
+ # uncomment this line if your project needs to run something other than `rake`:
8
+ script: bundle exec rspec spec
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in duck_duck_go_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TM Lee
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,40 @@
1
+ [![Build Status](https://travis-ci.org/tmlee/duck_duck_go_api.png)](https://travis-ci.org/tmlee/duck_duck_go_api)
2
+
3
+ # DuckDuckGoApi
4
+
5
+ Lightweight, flexible library for the Duck Duck Go API (https://duckduckgo.com/api) in Ruby
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'duck_duck_go_api', git: 'git://github.com/tmlee/duck_duck_go_api.git'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ Call the API directly via the DuckDuckGoApi::Client. Pass in parameters such as (q, format, etc..)
20
+ Parameters map 1-1 as stated in the documentation https://duckduckgo.com/api
21
+
22
+ ### You'll need the DuckDuckGoApi::Client
23
+
24
+ client = DuckDuckGoApi::Client
25
+
26
+ ### Query Anything!
27
+
28
+ client.query :q => "github", :format => "json"
29
+
30
+ ### Parameters Maps 1-1 in https://duckduckgo.com/api
31
+
32
+ client.query :q => "github", :format => "json", :no_html => 1, :skip_disambig => 1
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'duck_duck_go_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "duck_duck_go_api"
8
+ spec.version = DuckDuckGoApi::VERSION
9
+ spec.authors = ["TM Lee"]
10
+ spec.email = ["tm89lee@gmail.com"]
11
+ spec.description = %q{Duck Duck Go API for Ruby}
12
+ spec.summary = %q{Lightweight, flexible library for the Duck Duck Go API in Ruby}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency('faraday', '~> 0.8')
22
+ spec.add_dependency('json', '~> 1.7.7')
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 2.13.0"
27
+ spec.add_development_dependency("fakeweb", "~> 1.3.0")
28
+ end
@@ -0,0 +1,79 @@
1
+ require "duck_duck_go_api/version"
2
+ require "faraday"
3
+ require "json"
4
+
5
+ module DuckDuckGoApi
6
+ class Client
7
+ BASE_URL = 'https://api.duckduckgo.com'
8
+
9
+ class << self
10
+ def query(options={})
11
+ api_call '', options
12
+ end
13
+
14
+ ############## Private API Call Method ##############
15
+ private
16
+
17
+ def api_url
18
+ BASE_URL + '/'
19
+ end
20
+
21
+ def api_call(method_name, options, verb=:get)
22
+ response = connection(method_name, options, verb)
23
+ parse_response response
24
+ end
25
+
26
+ def parse_response(response)
27
+ raise_errors response
28
+ if response.body == " "
29
+ {}
30
+ else
31
+ JSON.parse response.body
32
+ end
33
+ end
34
+
35
+ def connection(method_name, options, verb)
36
+
37
+ conn = Faraday.new(:url => api_url) do |faraday|
38
+ faraday.request :url_encoded
39
+ faraday.adapter Faraday.default_adapter
40
+ end
41
+ response = conn.get(method_name + "?" + to_query_params(options))
42
+ end
43
+
44
+
45
+ def to_query_params(options)
46
+ options.collect { |key, value| "#{key}=#{value}" }.join('&')
47
+ end
48
+
49
+ def raise_errors(response)
50
+ message = "(#{response.status})"
51
+
52
+ case response.status.to_i
53
+ when 400
54
+ raise BadRequest, message
55
+ when 401
56
+ raise Unauthorized, message
57
+ when 403
58
+ raise General, message
59
+ when 404
60
+ raise NotFound, message
61
+ when 500
62
+ raise InternalError, "An internal error is thrown."
63
+ when 502..503
64
+ raise Unavailable, message
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ class BadRequest < StandardError; end
73
+ class Unauthorized < StandardError; end
74
+ class General < StandardError; end
75
+ class Unavailable < StandardError; end
76
+ class InternalError < StandardError; end
77
+ class NotFound < StandardError; end
78
+
79
+ end
@@ -0,0 +1,3 @@
1
+ module DuckDuckGoApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'duck_duck_go_api'
3
+
4
+ describe DuckDuckGoApi do
5
+
6
+ before :all do
7
+ @client = DuckDuckGoApi::Client
8
+ end
9
+
10
+ it "makes basic api call when passed in parameter q and format" do
11
+ stub_get("https://api.duckduckgo.com/?q=github&format=json", 'response.json')
12
+ result = @client.query :q => "github", :format => "json"
13
+ expect(result["Heading"]).to eql("GitHub")
14
+ end
15
+
16
+ context "passing parameter no_html" do
17
+ it "returns api response with no_html" do
18
+ stub_get("https://api.duckduckgo.com/?q=github&format=json&no_html=1", 'response.json')
19
+ result = @client.query :q => "github", :format => "json", :no_html => 1
20
+ expect(result["Results"].first["Result"]).to_not eql("<a href=\"https://github.com/\"><b>Official site</b></a><a href=\"https://github.com/\"></a>")
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,59 @@
1
+ {
2
+ "Definition" : "",
3
+ "DefinitionSource" : "",
4
+ "Heading" : "GitHub",
5
+ "AbstractSource" : "Wikipedia",
6
+ "Image" : "https://i.duckduckgo.com/i/27ff1233.png",
7
+ "RelatedTopics" : [
8
+ {
9
+ "Result" : "<a href=\"http://duckduckgo.com/c/Bug_and_issue_tracking_software\">Bug and issue tracking software</a>",
10
+ "Icon" : {
11
+ "URL" : "",
12
+ "Height" : "",
13
+ "Width" : ""
14
+ },
15
+ "FirstURL" : "http://duckduckgo.com/c/Bug_and_issue_tracking_software",
16
+ "Text" : "Bug and issue tracking software"
17
+ },
18
+ {
19
+ "Result" : "<a href=\"http://duckduckgo.com/c/Computing_websites\">Computing websites</a>",
20
+ "Icon" : {
21
+ "URL" : "",
22
+ "Height" : "",
23
+ "Width" : ""
24
+ },
25
+ "FirstURL" : "http://duckduckgo.com/c/Computing_websites",
26
+ "Text" : "Computing websites"
27
+ },
28
+ {
29
+ "Result" : "<a href=\"http://duckduckgo.com/c/Project_management_software\">Project management software</a>",
30
+ "Icon" : {
31
+ "URL" : "",
32
+ "Height" : "",
33
+ "Width" : ""
34
+ },
35
+ "FirstURL" : "http://duckduckgo.com/c/Project_management_software",
36
+ "Text" : "Project management software"
37
+ }
38
+ ],
39
+ "AbstractText" : "GitHub is a web-based hosting service for software development projects that use the Git revision control system.",
40
+ "Abstract" : "GitHub is a web-based hosting service for software development projects that use the Git revision control system.",
41
+ "AnswerType" : "",
42
+ "Redirect" : "",
43
+ "Type" : "A",
44
+ "DefinitionURL" : "",
45
+ "Answer" : "",
46
+ "Results" : [
47
+ {
48
+ "Result" : "<a href=\"https://github.com/\">Official site</a><a href=\"https://github.com/\"></a>",
49
+ "Icon" : {
50
+ "URL" : "https://i.duckduckgo.com/i/github.com.ico",
51
+ "Height" : 16,
52
+ "Width" : 16
53
+ },
54
+ "FirstURL" : "https://github.com/",
55
+ "Text" : "Official site"
56
+ }
57
+ ],
58
+ "AbstractURL" : "https://en.wikipedia.org/wiki/GitHub"
59
+ }
@@ -0,0 +1 @@
1
+ {"Definition":"","DefinitionSource":"","Heading":"GitHub","AbstractSource":"Wikipedia","Image":"https://i.duckduckgo.com/i/27ff1233.png","RelatedTopics":[{"Result":"<a href=\"http://duckduckgo.com/c/Bug_and_issue_tracking_software\">Bug and issue tracking software</a>","Icon":{"URL":"","Height":"","Width":""},"FirstURL":"http://duckduckgo.com/c/Bug_and_issue_tracking_software","Text":"Bug and issue tracking software"},{"Result":"<a href=\"http://duckduckgo.com/c/Computing_websites\">Computing websites</a>","Icon":{"URL":"","Height":"","Width":""},"FirstURL":"http://duckduckgo.com/c/Computing_websites","Text":"Computing websites"},{"Result":"<a href=\"http://duckduckgo.com/c/Project_management_software\">Project management software</a>","Icon":{"URL":"","Height":"","Width":""},"FirstURL":"http://duckduckgo.com/c/Project_management_software","Text":"Project management software"}],"AbstractText":"GitHub is a web-based hosting service for software development projects that use the Git revision control system.","Abstract":"GitHub is a web-based hosting service for software development projects that use the Git revision control system.","AnswerType":"","Redirect":"","Type":"A","DefinitionURL":"","Answer":"","Results":[{"Result":"<a href=\"https://github.com/\">Official site</a><a href=\"https://github.com/\"></a>","Icon":{"URL":"https://i.duckduckgo.com/i/github.com.ico","Height":16,"Width":16},"FirstURL":"https://github.com/","Text":"Official site"}],"AbstractURL":"https://en.wikipedia.org/wiki/GitHub"}
@@ -0,0 +1,36 @@
1
+ require 'rspec'
2
+ require 'duck_duck_go_api'
3
+ require 'fakeweb'
4
+
5
+ FakeWeb.allow_net_connect = false
6
+
7
+ def fixture_file(filename, options={})
8
+ return '' if filename == ''
9
+
10
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
11
+ fixture = File.read(file_path)
12
+
13
+ case File.extname(file_path)
14
+ when '.json'
15
+ options[:parse] ? JSON.parse(fixture) : fixture
16
+ else
17
+ fixture
18
+ end
19
+ end
20
+
21
+
22
+ def stub_get(url, filename, options={})
23
+ opts = {
24
+ :body => error_or_standard_body(filename, options)
25
+ }.merge(options)
26
+ FakeWeb.register_uri(:get, url, opts)
27
+ end
28
+
29
+ def error_or_standard_body(filename, options)
30
+ error_options = options.delete(:error)
31
+ body = fixture_file(error_options ? 'error_template.json' : filename)
32
+ body = body.gsub(/%error_code%/, error_options[:code])
33
+ .gsub(/%error_type%/, error_options[:type])
34
+ .gsub(/%error_message%/, error_options[:message]) if error_options
35
+ body
36
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duck_duck_go_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - TM Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.8'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.8'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.7.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.7.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.13.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.13.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: fakeweb
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.3.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.0
110
+ description: Duck Duck Go API for Ruby
111
+ email:
112
+ - tm89lee@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .travis.yml
119
+ - CHANGELOG
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - duck_duck_go_api.gemspec
125
+ - lib/duck_duck_go_api.rb
126
+ - lib/duck_duck_go_api/version.rb
127
+ - spec/duck_duck_go_api_spec.rb
128
+ - spec/fixtures/no_html.json
129
+ - spec/fixtures/response.json
130
+ - spec/spec_helper.rb
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.23
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: Lightweight, flexible library for the Duck Duck Go API in Ruby
156
+ test_files:
157
+ - spec/duck_duck_go_api_spec.rb
158
+ - spec/fixtures/no_html.json
159
+ - spec/fixtures/response.json
160
+ - spec/spec_helper.rb
161
+ has_rdoc: