cognitivebing 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa4e0321e7dd19b8789bbab3affb0726097c46a1
4
- data.tar.gz: 70710914f53b76fb2f236789c402a668b8373ac1
3
+ metadata.gz: cd8ad5f0bf4ca0524638ba8d917ba0fc572d9c76
4
+ data.tar.gz: 4e84d1c8c13d2a6500700a96525578d6874e8bf3
5
5
  SHA512:
6
- metadata.gz: de6febcaa0433e27dfe5f5d72c42d45064da6f8dd538cd3e6648b235ce36eb6d808ec9caaf8be02e9b802676706ffad22018c5db8fa6631f89922c17cf448113
7
- data.tar.gz: 08e97b69145228b75f769d2c6a92ad314827fb34a462c8e72950e1b4bf16e02f3335049b1cff4f9742dd88e63f42c4b7199dbcabc9218c52c9b67c656a18c921
6
+ metadata.gz: ccb271ff7d68f543f1776a1bf594526a3268cf1ac18335caa94653bca961d90cc7a0b48b99976a4ff3544e484f8831c70fba65b063b1947da7222e87be52cac5
7
+ data.tar.gz: 91247fb9028ab23ff68812c81e881019fd2ef95ce98e1e2c60b642a3b5358a3fc01813f2d68ee4692e33dc927d70da43793b8a4b8f29c2b74d7ea089c7be1d4f
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Cognitivebing
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cognitivebing`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A gem for the new Bing Web Search API from Microsoft cognitive services.
4
+
5
+ https://www.microsoft.com/cognitive-services/en-us/apis
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,7 +23,22 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ require 'cognitivebing'
27
+
28
+ bing = CognitiveBing.new(account_key)
29
+ #web search
30
+ result_set = bing.search(search_term)
31
+ #Image search
32
+ result_set = bing.search(search_term, 'image')
33
+ #Video search
34
+ result_set = bing.search(search_term, 'videos')
35
+ #Suggestion search
36
+ result_set = bing.suggestions(search_term)
37
+ #News search
38
+ bing = CognitiveBingNews.new(account_key)
39
+ result_set = bing.search(search_term)
40
+ #News trending
41
+ result_set = bing.trending
26
42
 
27
43
  ## Development
28
44
 
@@ -1,3 +1,3 @@
1
1
  module Cognitivebing
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/cognitivebing.rb CHANGED
@@ -11,21 +11,57 @@ class CognitiveBing
11
11
  @params = params
12
12
  end
13
13
 
14
- def search(search_term)
14
+
15
+ def search(search_term, type = 'web')
16
+
15
17
 
16
- user = ''
17
- #sources_portion = URI.encode_www_form_component('\'' + @type + '\'')
18
- query_string = 'q='
18
+ query_string = '?q='
19
19
  query_portion = URI.encode_www_form_component('\'' + search_term + '\'')
20
- params = "&Ocp-Apim-Subscription-Key=#{@account_key}&count=20&offset=0&mkt=fr-FR&safesearch=Moderate"
21
- #@params.each do |k,v|
22
- # params << "&#{k.to_s}=\'#{v.to_s}\'"
23
- #end
20
+ params = "&Ocp-Apim-Subscription-Key=#{@account_key}"
21
+ @params.each do |k,v|
22
+ params << "&#{k.to_s}=#{v.to_s}"
23
+
24
+ end
24
25
 
26
+ web_search_url = ""
27
+
28
+ if type == "videos"
29
+ web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/videos/search"
30
+ elsif type == "image"
31
+ web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/images/search"
32
+ else
33
+ web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/search"
34
+ end
25
35
 
26
- web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/search?"
27
36
  full_address = web_search_url + query_string + query_portion + params
37
+ puts full_address
28
38
 
39
+ uri = URI(full_address)
40
+ req = Net::HTTP::Get.new(uri.request_uri)
41
+ req.add_field("Ocp-Apim-Subscription-Key", @account_key)
42
+
43
+
44
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
45
+ http.request(req)
46
+ }
47
+
48
+ body = JSON.parse(res.body, :symbolize_names => true)
49
+
50
+
51
+ return body
52
+ end
53
+
54
+ def suggestions(search_term)
55
+
56
+
57
+ query_string = '?q='
58
+ query_portion = URI.encode_www_form_component( search_term )
59
+
60
+
61
+
62
+ web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/suggestions"
63
+
64
+ full_address = web_search_url + query_string + query_portion
29
65
 
30
66
  uri = URI(full_address)
31
67
  req = Net::HTTP::Get.new(uri.request_uri)
@@ -38,9 +74,7 @@ class CognitiveBing
38
74
 
39
75
  body = JSON.parse(res.body, :symbolize_names => true)
40
76
 
41
- #puts body[:webPages].keys
42
- #puts body[:webPages][:value]
43
- #result_set = body[:d][:results]
77
+
44
78
  return body
45
79
  end
46
80
  end
@@ -0,0 +1,93 @@
1
+ require "cognitivebing/version"
2
+ require 'json'
3
+ require 'open-uri'
4
+ require 'net/http'
5
+
6
+
7
+ class CognitiveBingNews
8
+ attr_accessor :account_key
9
+
10
+ def initialize(account_key, params = {})
11
+ @account_key = account_key
12
+ end
13
+
14
+ def search(search_term, params)
15
+
16
+
17
+ query_string = '?q='
18
+ query_portion = URI.encode_www_form_component('\'' + search_term + '\'')
19
+ paramsbuilder = "&Ocp-Apim-Subscription-Key=#{@account_key}"
20
+ params.each do |k,v|
21
+ paramsbuilder << "&#{k.to_s}=#{v.to_s}"
22
+
23
+
24
+ end
25
+
26
+ web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/news/search"
27
+
28
+
29
+ full_address = web_search_url + query_string + query_portion + paramsbuilder
30
+ puts full_address
31
+
32
+ uri = URI(full_address)
33
+ req = Net::HTTP::Get.new(uri.request_uri)
34
+ req.add_field("Ocp-Apim-Subscription-Key", @account_key)
35
+
36
+
37
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
38
+ http.request(req)
39
+ }
40
+
41
+ body = JSON.parse(res.body, :symbolize_names => true)
42
+
43
+
44
+ return body
45
+ end
46
+
47
+
48
+
49
+ def category(category_term, params = {})
50
+
51
+ web_search_url = URI('https://api.cognitive.microsoft.com/bing/v5.0/news/')
52
+ web_search_url.query = URI.encode_www_form({
53
+ # Request parameters
54
+ 'category' => '#{category_term}'
55
+ })
56
+
57
+
58
+
59
+ uri = URI(web_search_url)
60
+ req = Net::HTTP::Get.new(uri.request_uri)
61
+ req.add_field("Ocp-Apim-Subscription-Key", @account_key)
62
+
63
+
64
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
65
+ http.request(req)
66
+ }
67
+
68
+ body = JSON.parse(res.body, :symbolize_names => true)
69
+
70
+
71
+ return body
72
+ end
73
+
74
+
75
+ def trending
76
+
77
+ web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/news/trendingtopics"
78
+
79
+
80
+ uri = URI(web_search_url)
81
+ req = Net::HTTP::Get.new(uri.request_uri)
82
+ req.add_field("Ocp-Apim-Subscription-Key", @account_key)
83
+
84
+
85
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
86
+ http.request(req)
87
+ }
88
+
89
+ body = JSON.parse(res.body, :symbolize_names => true)
90
+
91
+ return body
92
+ end
93
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cognitivebing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aggounix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-22 00:00:00.000000000 Z
11
+ date: 2016-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,7 @@ files:
72
72
  - cognitivebing.gemspec
73
73
  - lib/cognitivebing.rb
74
74
  - lib/cognitivebing/version.rb
75
+ - lib/cognitivebingnews.rb
75
76
  homepage: https://github.com/aggounix/cognitivebing
76
77
  licenses:
77
78
  - MIT