bing_search_client 0.1.1

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
+ SHA1:
3
+ metadata.gz: 2af3a12020feeb6a46c830ae44a5f7221e723efe
4
+ data.tar.gz: ed80c0a5558d046340e18e7aadbef29b5ea4c609
5
+ SHA512:
6
+ metadata.gz: a415d3906854530d085b1188a476164d6a3a87e21de786d7b5c68abdbc186b14d4c9520d451d3f55f53dfdeded47ee67ee2df6bdc21d215db9acb430f779b8c0
7
+ data.tar.gz: dd14b98a9e94cd7577b72927cace1be549d1eddb41b2a51181bdbad8955395a38442795de85524c9228ebe9638e9d913b4b2d700fac06da696dcd4e5dd075b42
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.10
5
+ - 2.2.6
6
+ - 2.3.3
7
+ before_install: gem install bundler -v 1.13.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bing_search_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Leanbit s.r.l
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,62 @@
1
+ [![Build Status](https://travis-ci.org/leanbit/bing_search_client.svg?branch=master)](https://travis-ci.org/leanbit/bing_search_client)
2
+
3
+ # BingSearchClient
4
+
5
+ Simple Ruby Bing Api Client for Cognito Search Services.
6
+ Currently support only the News endpoint.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'bing_search_client'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install bing_search_client
23
+
24
+ ## Usage
25
+
26
+ Configure your account key, if you do not have one just visit [Microsoft Cognitive Service](https://www.microsoft.com/cognitive-services/en-us/subscriptions)
27
+
28
+ ```ruby
29
+ BingSearchClient.configure do |c|
30
+ c.account_key = "123"
31
+ end
32
+
33
+ news_client = BingSearchClient::News.new(num_results: 10, params: {:mkt => 'it-IT'})
34
+ news_client.search(q: "test")
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ Run the specs
44
+
45
+ ```
46
+ $ rspec
47
+
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/leanbit/bing_search_client.
53
+
54
+ ## TODO
55
+
56
+ Implement other services like Web, Image and Video search.
57
+ Just take a look at the BingSearchClient::News class, add one class for each service.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
62
+
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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bing_search_client"
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
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
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bing_search_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bing_search_client"
8
+ spec.version = BingSearchClient::VERSION
9
+ spec.authors = ["Giacomo Macrì"]
10
+ spec.email = 'giacomo at leanbit dot it'
11
+
12
+ spec.summary = %q{Bing Search Ruby client for new v5 api}
13
+ spec.description = %q{A simple client to interact with Bing search API.}
14
+ spec.homepage = "https://github.com/leanbit/bing_search_client"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = '>= 2.1'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "webmock", "~> 1.22"
27
+ end
@@ -0,0 +1,54 @@
1
+ require 'json'
2
+ require "bing_search_client/version"
3
+ require "bing_search_client/client"
4
+ require "bing_search_client/result"
5
+ require "bing_search_client/news"
6
+ #TODO implement client for Web, Image and Video search
7
+
8
+ module BingSearchClient
9
+ # Class method to set up configuration parameters
10
+ #
11
+ # @example
12
+ # BingSearchClient.configure do |c|
13
+ # c.account_key = "123"
14
+ # end
15
+ #
16
+ def self.configure(&block)
17
+ yield @config ||= Configuration.new
18
+ end
19
+
20
+ # Return configuration parameters
21
+ #
22
+ # @example
23
+ # BingSearchClient.config.api_key
24
+ #
25
+ def self.config
26
+ @config
27
+ end
28
+
29
+ # Reset configuration parameters
30
+ #
31
+ # @example
32
+ # BingSearchClient.reset_config
33
+ #
34
+ def self.reset_config
35
+ @config = nil
36
+ end
37
+
38
+
39
+ # Container for configuration parameters
40
+ #
41
+ class Configuration
42
+ BASE_URL = "https://api.cognitive.microsoft.com/bing/v5.0".freeze
43
+
44
+ attr_accessor :account_key, :base_url
45
+
46
+ def base_url
47
+ @base_url || BASE_URL
48
+ end
49
+ end
50
+
51
+ # Exception raised for connection error
52
+ #
53
+ class BadResponse < Exception; end
54
+ end
@@ -0,0 +1,30 @@
1
+ module BingSearchClient
2
+ class Client
3
+ attr_reader :url
4
+
5
+ def initialize(url:)
6
+ @url = url
7
+ end
8
+
9
+ def get
10
+ uri = URI(url)
11
+ req = Net::HTTP::Get.new(uri.request_uri)
12
+ req.add_field('Ocp-Apim-Subscription-Key', BingSearchClient.config.account_key)
13
+ response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
14
+ http.request(req)
15
+ end
16
+ parse_response(response)
17
+ end
18
+
19
+ private
20
+
21
+ def parse_response(response)
22
+ body = JSON.parse(response.body, symbolize_names: true)
23
+ if (200..299).include?(response.code.to_i)
24
+ return { results: BingSearchClient::Result.build_from_array_response(raw_response: body[:value]), extra: body.reject { |k| k != :value } }
25
+ else
26
+ raise BingSearchClient::BadResponse.new("#{response.status}: #{body}")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ module BingSearchClient
2
+ class News
3
+
4
+ attr_reader :num_results, :params
5
+
6
+ def initialize(num_results: 100, params: {})
7
+ @num_results = num_results
8
+ @params = params
9
+ end
10
+
11
+ def search(q:, offset: 0)
12
+ query_string = 'q='
13
+ query_portion = URI.encode_www_form_component('\'' + q + '\'')
14
+ local_params = "&count=#{num_results}&offset=#{offset}"
15
+ @params.each do |k,v|
16
+ local_params << "&#{k.to_s}=#{v.to_s}"
17
+ end
18
+ full_address = [BingSearchClient.config.base_url, 'news/search'].join('/') + '?' + query_string + query_portion + local_params
19
+ Client.new(url: full_address).get
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ module BingSearchClient
2
+ class Result
3
+ attr_reader :body, :expanded_url
4
+
5
+ def initialize(body:)
6
+ @body = body
7
+ @expanded_url = nil
8
+ end
9
+
10
+ def self.build_from_array_response(raw_response:)
11
+ raw_response.map do |result_json_response|
12
+ new(body: result_json_response).tap do |result|
13
+ result.expand_url!
14
+ end
15
+ end
16
+ end
17
+
18
+ # Instance method to get the real url insted of the url bing
19
+ def expand_url!
20
+ bing_url = URI(body[:url])
21
+ parsed_query = CGI::parse(bing_url.query)
22
+ @expanded_url = parsed_query['r'].first
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module BingSearchClient
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bing_search_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Giacomo Macrì
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-15 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: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.22'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.22'
69
+ description: A simple client to interact with Bing search API.
70
+ email: giacomo at leanbit dot it
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - bing_search_client.gemspec
85
+ - lib/bing_search_client.rb
86
+ - lib/bing_search_client/client.rb
87
+ - lib/bing_search_client/news.rb
88
+ - lib/bing_search_client/result.rb
89
+ - lib/bing_search_client/version.rb
90
+ homepage: https://github.com/leanbit/bing_search_client
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '2.1'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Bing Search Ruby client for new v5 api
114
+ test_files: []