cognitivebing 0.2.1 → 0.2.2

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: 04682a05db10c57761f663d1044f77da48fedc31
4
- data.tar.gz: 746d54039a565bd7ac68be65e8a8aef46223b236
3
+ metadata.gz: e7169093b94a32bdd929b73f707921061ddb9cfe
4
+ data.tar.gz: c78d42fd181c5dac66fb3cb0f4aa44b6c3878ce4
5
5
  SHA512:
6
- metadata.gz: e0e8d07da9c99e6bcc48da47e7ba065159f05e2ccc2e1defaa05c5a2bb1bcee61f111839eec8d5976b24aa8e4adc56e8cbbc62d67a342e3e3d3c9cf195a68b25
7
- data.tar.gz: 5670cf02789d4b6cf21da2609e0165c784e5286fc12f5b2e8e2caae41810f87e1969e6e9fa6b55b156eb615944cfe7c0d05324581d5ae5634432b8cf744d83c3
6
+ metadata.gz: 01be164c702d717bec2765cf80c22ef054f063396c186ac61e94753ece85d45fb5b74d56a087ed8e76a4c80436db14daadd048c02784159756924b0fae779e7f
7
+ data.tar.gz: 4379df8a83e072e3e34b06fac3e13a15bdf5104a8efe3b8b61d0ba2f56bc0422e3c0b8a651a2deb1162fbbb6ac34111c2b730c18bbcef794f4060eb5e4763f8a
data/README.md CHANGED
@@ -22,15 +22,16 @@ Or install it yourself as:
22
22
  $ gem install cognitivebing
23
23
 
24
24
  ## Usage
25
-
25
+ ```ruby
26
26
  require 'cognitivebing'
27
27
 
28
28
  bing = CognitiveBing.new(account_key)
29
29
  #call with params
30
30
  bing = CognitiveBing.new(account_key, :mkt => "en-US", :count => 10, :offset => 0, :safesearch => "Moderate")
31
+ ```
31
32
 
32
-
33
- (to see params details : https://dev.cognitive.microsoft.com/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d)
33
+ (to see params details : https://dev.cognitive.microsoft.com/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d)
34
+ ```ruby
34
35
  #web search
35
36
  result_set = bing.search(search_term)
36
37
  #Image search
@@ -44,6 +45,7 @@ bing = CognitiveBingNews.new(account_key)
44
45
  result_set = bing.search(search_term)
45
46
  #News trending
46
47
  result_set = bing.trending
48
+ ```
47
49
 
48
50
  ## Development
49
51
 
@@ -53,7 +55,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
53
55
 
54
56
  ## Contributing
55
57
 
56
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cognitivebing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cognitivebing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](./CODE_OF_CONDUCT.md) code of conduct.
57
59
 
58
60
 
59
61
  ## License
data/lib/cognitivebing.rb CHANGED
@@ -4,27 +4,30 @@ require 'open-uri'
4
4
  require 'net/http'
5
5
 
6
6
  class CognitiveBing
7
- attr_accessor :account_key, :params
8
-
7
+ class BingUnavailableException < StandardError;
8
+ end
9
+
10
+ attr_accessor :account_key, :params
11
+
9
12
  def initialize(account_key, params = {})
10
- @account_key = account_key
13
+ @account_key = account_key
11
14
  @params = params
12
15
  end
13
-
14
-
16
+
17
+
15
18
  def search(search_term, type = 'web')
16
-
17
-
19
+
20
+
18
21
  query_string = '?q='
19
- query_portion = URI.encode_www_form_component('\'' + search_term + '\'')
20
- params = "&Ocp-Apim-Subscription-Key=#{@account_key}"
21
- @params.each do |k,v|
22
+ query_portion = URI.encode_www_form_component(search_term)
23
+ params = ""
24
+ @params.each do |k, v|
22
25
  params << "&#{k.to_s}=#{v.to_s}"
23
-
26
+
24
27
  end
25
-
28
+
26
29
  web_search_url = ""
27
-
30
+
28
31
  if type == "videos"
29
32
  web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/videos/search"
30
33
  elsif type == "image"
@@ -32,138 +35,140 @@ class CognitiveBing
32
35
  else
33
36
  web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/search"
34
37
  end
35
-
38
+
36
39
  full_address = web_search_url + query_string + query_portion + params
37
-
40
+
38
41
  uri = URI(full_address)
39
42
  req = Net::HTTP::Get.new(uri.request_uri)
40
43
  req.add_field("Ocp-Apim-Subscription-Key", @account_key)
41
-
42
44
 
43
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
45
+
46
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http|
44
47
  http.request(req)
45
48
  }
46
49
 
50
+
47
51
  body = JSON.parse(res.body, :symbolize_names => true)
48
-
49
-
50
- return body
52
+ rescue => e
53
+ if e.message.include?("Bing services aren't available right now")
54
+ raise(BingUnavailableException)
55
+ else
56
+ raise(e)
57
+ end
51
58
  end
52
-
59
+
53
60
  def suggestions(search_term)
54
-
55
-
61
+
62
+
56
63
  query_string = '?q='
57
- query_portion = URI.encode_www_form_component( search_term )
58
-
59
-
60
-
64
+ query_portion = URI.encode_www_form_component(search_term)
65
+
66
+
61
67
  web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/suggestions"
62
-
63
- full_address = web_search_url + query_string + query_portion
64
-
68
+
69
+ full_address = web_search_url + query_string + query_portion
70
+
65
71
  uri = URI(full_address)
66
72
  req = Net::HTTP::Get.new(uri.request_uri)
67
73
  req.add_field("Ocp-Apim-Subscription-Key", @account_key)
68
-
69
74
 
70
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
75
+
76
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http|
71
77
  http.request(req)
72
78
  }
73
79
 
74
80
  body = JSON.parse(res.body, :symbolize_names => true)
75
-
76
-
81
+
82
+
77
83
  return body
78
84
  end
85
+
79
86
  end
80
87
 
81
88
 
82
89
  class CognitiveBingNews
83
90
  attr_accessor :account_key
84
-
91
+
85
92
  def initialize(account_key, params = {})
86
93
  @account_key = account_key
87
94
  @params = params
88
95
  end
89
-
96
+
90
97
  def search(search_term)
91
-
92
-
98
+
99
+
93
100
  query_string = '?q='
94
- query_portion = URI.encode_www_form_component('\'' + search_term + '\'')
101
+ query_portion = URI.encode_www_form_component(search_term)
95
102
  paramsbuilder = "&Ocp-Apim-Subscription-Key=#{@account_key}"
96
- @params.each do |k,v|
103
+ @params.each do |k, v|
97
104
  paramsbuilder << "&#{k.to_s}=#{v.to_s}"
98
-
99
-
105
+
106
+
100
107
  end
101
-
108
+
102
109
  web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/news/search"
103
-
104
-
105
- full_address = web_search_url + query_string + query_portion + paramsbuilder
106
- puts full_address
107
-
110
+
111
+
112
+ full_address = web_search_url + query_string + query_portion + paramsbuilder
113
+
114
+
108
115
  uri = URI(full_address)
109
116
  req = Net::HTTP::Get.new(uri.request_uri)
110
117
  req.add_field("Ocp-Apim-Subscription-Key", @account_key)
111
-
112
118
 
113
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
119
+
120
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http|
114
121
  http.request(req)
115
122
  }
116
123
 
117
124
  body = JSON.parse(res.body, :symbolize_names => true)
118
-
119
-
125
+
126
+
120
127
  return body
121
128
  end
122
-
123
-
124
-
129
+
130
+
125
131
  def category(category_term, params = {})
126
-
132
+
127
133
  web_search_url = URI('https://api.cognitive.microsoft.com/bing/v5.0/news/')
128
- web_search_url.query = URI.encode_www_form({
129
- # Request parameters
130
- 'category' => '#{category_term}'
134
+ web_search_url.query = URI.encode_www_form({
135
+ # Request parameters
136
+ 'category' => category_term.to_s
131
137
  })
132
-
133
-
134
-
138
+
139
+
135
140
  uri = URI(web_search_url)
136
141
  req = Net::HTTP::Get.new(uri.request_uri)
137
142
  req.add_field("Ocp-Apim-Subscription-Key", @account_key)
138
-
139
143
 
140
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
144
+
145
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http|
141
146
  http.request(req)
142
147
  }
143
148
 
144
149
  body = JSON.parse(res.body, :symbolize_names => true)
145
-
146
-
150
+
151
+
147
152
  return body
148
153
  end
149
-
150
-
154
+
155
+
151
156
  def trending
152
-
157
+
153
158
  web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/news/trendingtopics"
154
-
155
-
159
+
160
+
156
161
  uri = URI(web_search_url)
157
162
  req = Net::HTTP::Get.new(uri.request_uri)
158
163
  req.add_field("Ocp-Apim-Subscription-Key", @account_key)
159
-
160
164
 
161
- res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
165
+
166
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http|
162
167
  http.request(req)
163
168
  }
164
169
 
165
170
  body = JSON.parse(res.body, :symbolize_names => true)
166
-
171
+
167
172
  return body
168
173
  end
169
174
  end
@@ -1,3 +1,3 @@
1
1
  module Cognitivebing
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  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.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - aggounix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-04 00:00:00.000000000 Z
11
+ date: 2018-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler