ahiru 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 503c60d3f4cfd72ed13cccc9319e627bd03ba9e7b4832d22a13d4c8e52d97493
4
+ data.tar.gz: ba05588b758293f9a3939e44e26822db8ad621de1b6069e83d79e470caa3cd58
5
+ SHA512:
6
+ metadata.gz: ac0107171b051857cb6b82813ef4c30a11ee1147be70f10ef337989dd8c114ca6d3a818ec85ea66760d5ff1c200da402481dfd4b9e4dd56125c749f97889f81d
7
+ data.tar.gz: 829108f6ddeeb1d262ff17756dc118e24c15b969073e693c3fd1b01b774522614f5defba43f2137803695eb209e990c88f4b76d30f770f6d7aa19afb70fca071
data/.gitignore ADDED
@@ -0,0 +1,52 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ahiru.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Manabu Niseki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # ahiru
2
+
3
+ [![Build Status](https://travis-ci.org/ninoseki/ahiru.svg?branch=master)](https://travis-ci.org/ninoseki/ahiru)
4
+ [![Coverage Status](https://coveralls.io/repos/github/ninoseki/ahiru/badge.svg?branch=master)](https://coveralls.io/github/ninoseki/ahiru?branch=master)
5
+
6
+ Yet another Ruby gem for DuckDuckGo Search.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ % gem install ahiru
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```ruby
17
+ require "ahiru"
18
+
19
+ page = Ahiru::API.search(q: "test")
20
+ result = page.results.first
21
+ p result.title
22
+ # => "Test | Definition of Test by Merriam-Webster"
23
+ p result.url
24
+ # => "https://www.merriam-webster.com/dictionary/test"
25
+ p result.snippet
26
+ # => "(1): something (such as a series of questions or exercises) for measuring the skill, knowledge, intelligence, capacities, or aptitudes of an individual or group"
27
+
28
+ next_page = page.next_page if page.next_page?
29
+ ```
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/ahiru.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "ahiru/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "ahiru"
9
+ spec.version = Ahiru::VERSION
10
+ spec.authors = ["Manabu Niseki"]
11
+ spec.email = ["manabu.niseki@gmail.com"]
12
+
13
+ spec.summary = "Yet another Ruby gem for DuckDuckGo search"
14
+ spec.description = "Yet another Ruby gem for DuckDuckGo search"
15
+ spec.homepage = "https://github.com/ninoseki/ahiru"
16
+ spec.license = "MIT"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "coveralls", "~> 0.8"
29
+ spec.add_development_dependency "glint", "~> 0.1"
30
+ spec.add_development_dependency "rake", "~> 12.3"
31
+ spec.add_development_dependency "rspec", "~> 3.8"
32
+ spec.add_development_dependency "vcr", "~> 4.0"
33
+ spec.add_development_dependency "webmock", "~> 3.5"
34
+
35
+ spec.add_dependency "http", "~> 4.0"
36
+ spec.add_dependency "oga", "~> 2.15"
37
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ahiru"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/ahiru/api.rb ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "http"
4
+ require "oga"
5
+
6
+ module Ahiru
7
+ class API
8
+ DUCKDUCKGO_ENDPOINT = "https://duckduckgo.com/html/"
9
+
10
+ def search(params)
11
+ body = get_body(DUCKDUCKGO_ENDPOINT, params)
12
+ return nil if body.nil?
13
+
14
+ Page.new body
15
+ end
16
+
17
+ def self.search(params)
18
+ new.search(params)
19
+ end
20
+
21
+ private
22
+
23
+ def get_body(url, params)
24
+ res = HTTP.get(url, params: params)
25
+ return nil unless res.code == 200
26
+
27
+ res.body.to_s
28
+ rescue HTTP::Error, OpenSSL::SSL::SSLError, Addressable::URI::InvalidURIError => _
29
+ nil
30
+ end
31
+ end
32
+ end
data/lib/ahiru/page.rb ADDED
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+ require "uri"
3
+
4
+ module Ahiru
5
+ class Page
6
+ DEFAULT_PARAM_KEYS = [:q, :s, :dc].freeze
7
+
8
+ def initialize(html)
9
+ @html = html
10
+ end
11
+
12
+ def results
13
+ doc.css(".result").map do |result|
14
+ href = result.at_css("a.result__a")&.get("href")
15
+ url = URI.decode_www_form(href).to_h["uddg"]
16
+ title = result.at_css("a.result__a")&.text
17
+ snippet = result.at_css("a.result__snippet")&.text
18
+ Result.new(url: url, title: title, snippet: snippet)
19
+ end
20
+ end
21
+
22
+ def next_page
23
+ return nil unless next_page?
24
+
25
+ params = doc.css("div.nav-link > form > input[type='hidden']").map do |input|
26
+ key = input.get("name")
27
+ value = input.get("value")
28
+ [key.to_sym, value]
29
+ end.to_h.slice(*DEFAULT_PARAM_KEYS)
30
+ API.search(params)
31
+ end
32
+
33
+ def next_page?
34
+ _next_page != nil
35
+ end
36
+
37
+ private
38
+
39
+ def _next_page
40
+ @_next_page ||= doc.at_css("div.nav-link > form > input[value='Next']")
41
+ end
42
+
43
+ def doc
44
+ @doc ||= Oga.parse_html(@html)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ahiru
4
+ class Result
5
+ attr_reader :url, :title, :snippet
6
+
7
+ def initialize(url:, title:, snippet:)
8
+ @url = url
9
+ @title = title
10
+ @snippet = snippet
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ahiru
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ahiru.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ahiru/version"
4
+ require "ahiru/api"
5
+ require "ahiru/result"
6
+ require "ahiru/page"
7
+
8
+ module Ahiru
9
+ class Error < StandardError; end
10
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ahiru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Manabu Niseki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: glint
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.5'
111
+ - !ruby/object:Gem::Dependency
112
+ name: http
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: oga
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.15'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.15'
139
+ description: Yet another Ruby gem for DuckDuckGo search
140
+ email:
141
+ - manabu.niseki@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - ahiru.gemspec
154
+ - bin/console
155
+ - bin/setup
156
+ - lib/ahiru.rb
157
+ - lib/ahiru/api.rb
158
+ - lib/ahiru/page.rb
159
+ - lib/ahiru/result.rb
160
+ - lib/ahiru/version.rb
161
+ homepage: https://github.com/ninoseki/ahiru
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.0.2
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Yet another Ruby gem for DuckDuckGo search
184
+ test_files: []