semantics3 0.04 → 0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Changes +3 -0
  2. data/README.md +143 -66
  3. data/lib/semantics3.rb +32 -18
  4. data/semantics3.gemspec +2 -2
  5. metadata +63 -49
  6. checksums.yaml +0 -7
data/Changes CHANGED
@@ -1,5 +1,8 @@
1
1
  Revision history for semantics3-ruby
2
2
 
3
+ 0.04 11-03-2014
4
+ Use CGI.escape instead of URI.escape [Thanks to Mark Evans/ShopGenius App (mark@shopgeniusapp.com)]
5
+
3
6
  0.03 07-02-2014
4
7
  Made library thread safe [Thanks to Mark Evans/ShopGenius App (mark@shopgeniusapp.com)]
5
8
 
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  semantics3-ruby is a Ruby client for accessing the Semantics3 Products API, which provides structured information, including pricing histories, for a large number of products.
4
4
  See https://www.semantics3.com for more information.
5
5
 
6
- Quickstart guide: https://www.semantics3.com/quickstart
7
6
  API documentation can be found at https://www.semantics3.com/docs/
8
7
 
9
8
  ## Installation
@@ -46,119 +45,199 @@ API_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
46
45
  sem3 = Semantics3::Products.new(API_KEY,API_SECRET)
47
46
  ```
48
47
 
49
- ### First Query aka 'Hello World':
48
+ ### First Request aka 'Hello World':
50
49
 
51
- Let's make our first query! For this query, we are going to search for all Toshiba products that fall under the category of "Computers and Accessories", whose cat_id is 4992.
50
+ Let's run our first request! We are going to run a simple search fo the word "iPhone" as follows:
52
51
 
53
52
  ```ruby
54
- # Build the query
55
- sem3.products_field( "cat_id", 4992 )
56
- sem3.products_field( "brand", "Toshiba" )
53
+ # Build the request
54
+ sem3.products_field( "search", "iphone" )
57
55
 
58
- # Make the query
56
+ # Run the request
59
57
  productsHash = sem3.get_products()
60
58
 
61
- # View the results of the query
59
+ # View the results of the request
62
60
  puts productsHash.to_json
63
61
  ```
64
62
 
65
- ## Examples
63
+ ## Sample Requests
66
64
 
67
- The following examples show you how to interface with some of the core functionality of the Semantics3 Products API. For more detailed examples check out the Quickstart guide: https://www.semantics3.com/quickstart
65
+ The following requests show you how to interface with some of the core functionality of the Semantics3 Products API:
68
66
 
69
- ### Explore the Category Tree
67
+ ### Pagination
70
68
 
71
- In this example we are going to be accessing the categories endpoint. We are going to be specifically exploring the "Computers and Accessories" category, which has a cat_id of 4992. For more details regarding our category tree and associated cat_ids check out our API docs at https://www.semantics3.com/docs
69
+ The example in our "Hello World" script returns the first 10 results. In this example, we'll scroll to subsequent pages, beyond our initial request:
72
70
 
73
71
  ```ruby
74
- # Build the query
75
- sem3.categories_field( "cat_id", 4992 )
72
+ # Build the request
73
+ sem3.products_field( "search", "iphone" )
76
74
 
77
- # Make the query
78
- categoriesHash = sem3.get_categories()
75
+ # Run the request
76
+ productsHash = sem3.get_products()
79
77
 
80
- # View the results of the query
81
- puts categoriesHash.to_json
78
+ # View the results of the request
79
+ puts productsHash.to_json
80
+
81
+ # Goto the next 'page'
82
+ page = 0
83
+ while (productsHash = sem3.iterate_products) do
84
+ page = page + 1
85
+ puts "We are at page = #{page}"
86
+ puts "The results for this page are:\n"
87
+ puts productsHash.to_json
88
+ end
82
89
  ```
83
90
 
84
- ### Nested Search Query
91
+ ### UPC Query
92
+
93
+ Running a UPC/EAN/GTIN query is as simple as running a search query:
94
+
95
+ ```ruby
96
+ # Build the request
97
+ sem3.products_field( "upc", "883974958450" )
98
+ sem3.products_field( "field", ["name","gtins"] )
99
+
100
+ # Run the request
101
+ productsHash = sem3.get_products()
85
102
 
86
- You can construct complex queries by just repeatedly calling the products_field() or add() methods. Here is how we translate the following JSON query - '{"cat_id":4992,"brand":"Toshiba","weight":{"gte":1000000,"lt":1500000},"sitedetails":{"name":"newegg.com","latestoffers":{"currency":"USD","price":{"gte":100}}}}'.
103
+ # View the results of the request
104
+ puts productsHash.to_json
105
+ ```
87
106
 
88
- This query returns all Toshiba products within a certain weight range narrowed down to just those that retailed recently on newegg.com for >= USD 100.
107
+ ### URL Query
108
+
109
+ Get the picture? You can run URL queries as follows:
89
110
 
90
111
  ```ruby
91
- # Build the query
92
- sem3.products_field( "cat_id", 4992 )
93
- sem3.products_field( "brand", "Toshiba" )
94
- sem3.products_field( "weight", "gte", 1000000 )
95
- sem3.products_field( "weight", "lt", 1500000 )
96
- sem3.products_field( "sitedetails", "name", "newegg.com" )
97
- sem3.products_field( "sitedetails", "latestoffers", "currency", "USD" )
98
- sem3.products_field( "sitedetails", "latestoffers", "price", "gte", 100 )
112
+ sem3.products_field( "url", "http://www.walmart.com/ip/15833173" )
113
+ productsHash = sem3.get_products()
114
+ puts productsHash.to_json
115
+ ```
99
116
 
100
- # Let's make a modification - say we no longer want the weight attribute
101
- sem3.remove( "products", "brand", "weight" )
117
+ ### Price Filter
102
118
 
103
- # Let's view the JSON query we just constructed. This is a good starting point to debug, if you are getting incorrect
104
- # results for your query
105
- constructedJson = sem3.get_query_json( "products" )
106
- puts constructedJson
119
+ Filter by price using the "lt" (less than) tag:
107
120
 
108
- # Make the query
109
- productsHash = sem3.get_products
121
+ ```ruby
122
+ sem3.products_field( "search", "iphone" )
123
+ sem3.products_field( "price", "lt", 300 )
124
+ productsHash = sem3.get_products()
125
+ puts productsHash.to_json
126
+ ```
110
127
 
111
- # View the results of the query
128
+ ### Category ID Query
129
+
130
+ To lookup details about a cat_id, run your request against the categories resource:
131
+
132
+ ```ruby
133
+ # Build the request
134
+ sem3.products_field( "cat_id", 4992 )
135
+
136
+ # Run the request
137
+ productsHash = sem3.get_products()
138
+
139
+ # View the results of the request
112
140
  puts productsHash.to_json
113
141
  ```
114
142
 
115
- ### Pagination
143
+ ## Webhooks
144
+ You can use webhooks to get near-real-time price updates from Semantics3.
145
+
146
+ ### Creating a webhook
116
147
 
117
- Let's now look at doing pagination, continuing from where we stopped previously.
148
+ You can register a webhook with Semantics3 by sending a POST request to `"webhooks"` endpoint.
149
+ To verify that your URL is active, a GET request will be sent to your server with a `verification_code` parameter. Your server should respond with `verification_code` in the response body to complete the verification process.
118
150
 
119
151
  ```ruby
120
- # Goto the next 'page'
121
- page = 0
122
- while (productsHash = sem3.iterate_products) do
123
- page = page + 1
124
- puts "Iterating through page: #{page}"
125
- end
152
+ params = {
153
+ "webhook_uri" => "http://mydomain.com/webhooks-callback-url"
154
+ }
155
+
156
+ res = sem3.run_query("webhooks","POST",params)
157
+ puts res
158
+ ```
159
+ To fetch existing webhooks
160
+ ```ruby
161
+ res = sem3.run_query("webhooks","GET")
162
+ puts res
126
163
  ```
127
164
 
128
- ### Explore Price Histories
165
+ To remove a webhook
166
+ ```ruby
167
+ webhook_id = "7JcGN81u"
168
+ endpoint = "webhooks/"+webhook_id
169
+
170
+ res = sem3.run_query(endpoint,"DELETE" )
171
+ puts res
172
+ ```
129
173
 
130
- We shall use the add() method, which allows you to access any of the supported endpoints by just specifiying the name of the endpoint. add( "products", param1, param2, ...) is the equivalent of products_field( param1, param2, ... ), add( "offers", param1, param2, ... ) is the equivalent of offers_field( param1, param2, ...)
174
+ ### Registering events
175
+ Once you register a webhook, you can start adding events to it. Semantics3 server will send you notifications when these events occur.
176
+ To register events for a specific webhook send a POST request to the `"webhooks/{webhook_id}/events"` endpoint
131
177
 
132
- For this example, we are going to look at a particular product that is sold by select mercgants and whose price is >= USD 30 and seen after a specific date (specified as a UNIX timestamp).
178
+ ```ruby
179
+ params = {
180
+ "type" => "price.change",
181
+ "product" => {
182
+ "sem3_id" => "1QZC8wchX62eCYS2CACmka"
183
+ },
184
+ "constraints" => {
185
+ "gte" => 10,
186
+ "lte" => 100
187
+ }
188
+ }
189
+
190
+ webhook_id = '7JcGN81u'
191
+ endpoint = "webhooks/#{webhook_id}/events"
192
+
193
+ eventObject = sem3.run_query(endpoint,"POST",params)
194
+ puts eventObject["id"]
195
+ puts eventObject["type"]
196
+ puts eventObject["product"]
197
+ ```
133
198
 
199
+ To fetch all registered events for a give webhook
134
200
  ```ruby
135
- # Build the query
136
- sem3.add( "offers", "sem3_id", "4znupRCkN6w2Q4Ke4s6sUC")
137
- sem3.add( "offers", "seller", ["LFleurs","Frys","Walmart"] )
138
- sem3.add( "offers", "currency", "USD")
139
- sem3.add( "offers", "price", "gte", 30)
140
- sem3.add( "offers", "lastrecorded_at", "gte", 1348654600)
201
+ webhook_id = "7JcGN81u"
202
+ endpoint = "webhooks/#{webhook_id}/events"
141
203
 
142
- # Make the query
143
- offersHash = sem3.get_offers
144
- #Alternatively we could also do
145
- offersHash = sem3.run_query( "offers" )
204
+ res = sem3.run_query(endpoint,"GET")
205
+ puts res
206
+ ```
146
207
 
147
- # View the results of the query
148
- puts offersHash.to_json
208
+ ### Webhook Notifications
209
+ Once you have created a webhook and registered events on it, notifications will be sent to your registered webhook URI via a POST request when the corresponding events occur. Make sure that your server can accept POST requests. Here is how a sample notification object looks like
210
+ ```javascript
211
+ {
212
+ "type": "price.change",
213
+ "event_id": "XyZgOZ5q",
214
+ "notification_id": "X4jsdDsW",
215
+ "changes": [{
216
+ "site": "abc.com",
217
+ "url": "http://www.abc.com/def",
218
+ "previous_price": 45.50,
219
+ "current_price": 41.00
220
+ }, {
221
+ "site": "walmart.com",
222
+ "url": "http://www.walmart.com/ip/20671263",
223
+ "previous_price": 34.00,
224
+ "current_price": 42.00
225
+ }]
226
+ }
149
227
  ```
150
228
 
151
229
  ## Contributing
152
230
 
153
231
  Use GitHub's standard fork/commit/pull-request cycle. If you have any questions, email <support@semantics3.com>.
154
232
 
155
- ## Author
233
+ ## Authors
156
234
 
157
235
  * Sivamani VARUN <varun@semantics3.com>
236
+ * Mounarajan <mounarajan@semantics3.com>
158
237
 
159
238
  ## Copyright
160
239
 
161
- Copyright (c) 2013 Semantics3 Inc.
240
+ Copyright (c) 2015 Semantics3 Inc.
162
241
 
163
242
  ## License
164
243
 
@@ -180,6 +259,4 @@ Copyright (c) 2013 Semantics3 Inc.
180
259
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
181
260
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
182
261
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
183
- DEALINGS IN THE SOFTWARE.
184
-
185
-
262
+ DEALINGS IN THE SOFTWARE.
data/lib/semantics3.rb CHANGED
@@ -22,21 +22,37 @@ module Semantics3
22
22
  raise Error.new('API Credentials Missing','You did not supply an api_key. Please sign up at https://semantics3.com/ to obtain your api_key.','api_key') if api_key == ''
23
23
  raise Error.new('API Credentials Missing','You did not supply an api_secret. Please sign up at https://semantics3.com/ to obtain your api_secret.','api_secret') if api_secret == ''
24
24
 
25
- consumer = OAuth::Consumer.new(@api_key, @api_secret)
26
- @auth = OAuth::AccessToken.new(consumer)
25
+ @consumer = OAuth::Consumer.new(@api_key, @api_secret)
26
+ @auth = OAuth::AccessToken.new(@consumer)
27
27
  end
28
28
 
29
29
  private
30
-
30
+
31
+ #def oauth(response)
31
32
  #returns a value
32
- def _make_request(endpoint, params)
33
- url = 'https://api.semantics3.com/v1/' + endpoint + '?q=' + CGI.escape(params)
34
-
35
- #puts "url = #{url}"
36
- response = @auth.get(url)
37
-
38
- #-- Response.code - TBD
39
- JSON.parse response.body
33
+ def _make_request(endpoint,method = "GET",params)
34
+ base_url = 'https://api-staging.semantics3.com/v1/' #+ endpoint + '?q=' + CGI.escape(params)
35
+
36
+ if method == "GET"
37
+ request_data = CGI.escape(params)
38
+ encoded_url = base_url + endpoint + '?q=' + request_data
39
+ response = @auth.get(encoded_url)
40
+ JSON.parse response.body
41
+ elsif method == "DELETE"
42
+ url = base_url + endpoint
43
+ response = @auth.delete(url,params)
44
+ JSON.parse response.body
45
+ else
46
+ url = URI(base_url+endpoint)
47
+ request = Net::HTTP::Post.new url.request_uri,params
48
+ http = Net::HTTP.new url.host, url.port
49
+ http.use_ssl = true
50
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
51
+ request.oauth! http, @consumer,@auth
52
+ http.start
53
+ response = http.request request
54
+ JSON.parse response.body
55
+ end
40
56
  end
41
57
 
42
58
  end
@@ -204,7 +220,7 @@ module Semantics3
204
220
  @query_result={}
205
221
  end
206
222
 
207
- def run_query(endpoint,*params)
223
+ def run_query(endpoint,method = "GET",*params)
208
224
 
209
225
  #-- If not defined endpoint, throw error
210
226
  if not ( endpoint.kind_of? String and endpoint != '')
@@ -214,7 +230,7 @@ module Semantics3
214
230
  data = params[0]
215
231
 
216
232
  if data == nil
217
- @query_result = _make_request(endpoint,@data_query[endpoint].to_json)
233
+ @query_result = _make_request(endpoint,method,@data_query[endpoint].to_json)
218
234
  else
219
235
  if not data.is_a?(Hash) and not data.is_a?(String)
220
236
  #-- Throw error - neither string nor hash
@@ -222,12 +238,12 @@ module Semantics3
222
238
  else
223
239
  #-- Data is Hash ref. Great just send it.
224
240
  if data.is_a?(Hash)
225
- @query_result = _make_request(endpoint,data.to_json)
241
+ @query_result = _make_request(endpoint,method,data)
226
242
  #-- Data is string
227
243
  elsif data.is_a?(String)
228
244
  #-- Check if it's valid JSON
229
245
  if JSON.is_json?(data)
230
- @query_result = _make_request(endpoint,data)
246
+ @query_result = _make_request(endpoint,method,data)
231
247
  else
232
248
  raise Error.new('Invalid Input','You submitted an invalid JSON query string')
233
249
  end
@@ -261,6 +277,4 @@ module JSON
261
277
  false
262
278
  end
263
279
  end
264
- end
265
-
266
-
280
+ end
data/semantics3.gemspec CHANGED
@@ -2,10 +2,10 @@ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = 'semantics3'
5
- s.version = '0.04'
5
+ s.version = '0.10'
6
6
  s.summary = 'Ruby bindings for the Semantics3 API'
7
7
  s.description = 'Get access to a constantly updated database of product and price data. See https://semantics3.com/ for more information.'
8
- s.authors = ['Sivamani Varun']
8
+ s.authors = ['Sivamani Varun', 'Mounarajan P A']
9
9
  s.email = ['varun@semantics3.com']
10
10
  s.homepage = 'https://semantics3.com'
11
11
  s.require_paths = %w{lib}
metadata CHANGED
@@ -1,52 +1,69 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: semantics3
3
- version: !ruby/object:Gem::Version
4
- version: "0.04"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.10'
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Sivamani Varun
9
+ - Mounarajan P A
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
-
12
- date: 2014-03-11 00:00:00 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
13
+ date: 2015-03-31 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
15
16
  name: json
16
- prerelease: false
17
- requirement: &id001 !ruby/object:Gem::Requirement
18
- requirements:
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
19
20
  - - ~>
20
- - !ruby/object:Gem::Version
21
- version: "1.8"
22
- - - ">="
23
- - !ruby/object:Gem::Version
21
+ - !ruby/object:Gem::Version
22
+ version: '1.8'
23
+ - - ! '>='
24
+ - !ruby/object:Gem::Version
24
25
  version: 1.8.1
25
26
  type: :runtime
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: oauth
29
27
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
31
- requirements:
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 1.8.1
37
+ - !ruby/object:Gem::Dependency
38
+ name: oauth
39
+ requirement: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
32
42
  - - ~>
33
- - !ruby/object:Gem::Version
34
- version: "0.4"
35
- - - ">="
36
- - !ruby/object:Gem::Version
43
+ - !ruby/object:Gem::Version
44
+ version: '0.4'
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
37
47
  version: 0.4.6
38
48
  type: :runtime
39
- version_requirements: *id002
40
- description: Get access to a constantly updated database of product and price data. See https://semantics3.com/ for more information.
41
- email:
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '0.4'
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: 0.4.6
59
+ description: Get access to a constantly updated database of product and price data.
60
+ See https://semantics3.com/ for more information.
61
+ email:
42
62
  - varun@semantics3.com
43
63
  executables: []
44
-
45
64
  extensions: []
46
-
47
65
  extra_rdoc_files: []
48
-
49
- files:
66
+ files:
50
67
  - .gitignore
51
68
  - Changes
52
69
  - Gemfile
@@ -57,29 +74,26 @@ files:
57
74
  - semantics3.gemspec
58
75
  homepage: https://semantics3.com
59
76
  licenses: []
60
-
61
- metadata: {}
62
-
63
77
  post_install_message:
64
78
  rdoc_options: []
65
-
66
- require_paths:
79
+ require_paths:
67
80
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
69
- requirements:
70
- - &id003
71
- - ">="
72
- - !ruby/object:Gem::Version
73
- version: "0"
74
- required_rubygems_version: !ruby/object:Gem::Requirement
75
- requirements:
76
- - *id003
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
77
93
  requirements: []
78
-
79
94
  rubyforge_project:
80
- rubygems_version: 2.2.2
95
+ rubygems_version: 1.8.23
81
96
  signing_key:
82
- specification_version: 4
97
+ specification_version: 3
83
98
  summary: Ruby bindings for the Semantics3 API
84
99
  test_files: []
85
-
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA512:
3
- metadata.gz: ec1c393a4622410f28f72e856d70bcfb86a2ae4fa7a833379d699900d51a9b91e0e81b179227db0163083bbe8212e9bfc10d13eda005dac9a73810a9f397dafe
4
- data.tar.gz: c31391dfd60d7d21a02fb485c162eeb23d3d24fb58cf8f60ff5de29feb4aae22358295cb5285290b9ecce49c2b104d0e12d649e9dc9c4a6930047654dc387ce8
5
- SHA1:
6
- metadata.gz: af7ad4b7caafa920f0ba0f66e27e4d94d1d02600
7
- data.tar.gz: 2dbc54c3143cfc6adebd9e82a83f5fded103e13b