semantics3 0.01
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Changes +5 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +185 -0
- data/examples/example.rb +62 -0
- data/lib/semantics3.rb +262 -0
- data/semantics3.gemspec +18 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Changes
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2013 Semantics3, Inc. https://semantics3.com
|
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,185 @@
|
|
1
|
+
# semantics3-ruby
|
2
|
+
|
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
|
+
See https://www.semantics3.com for more information.
|
5
|
+
|
6
|
+
Quickstart guide: https://www.semantics3.com/quickstart
|
7
|
+
API documentation can be found at https://www.semantics3.com/docs/
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
semantics3-ruby can be installed through the RubyGem system:
|
12
|
+
```
|
13
|
+
gem install semantics3
|
14
|
+
```
|
15
|
+
To build and install from the latest source:
|
16
|
+
```
|
17
|
+
git clone git@github.com:Semantics3/semantics3-ruby.git
|
18
|
+
cd semantics3-ruby
|
19
|
+
gem build semantics3.gemspec
|
20
|
+
gem install semantics3-<VERSION>.gem
|
21
|
+
```
|
22
|
+
|
23
|
+
## Requirements
|
24
|
+
|
25
|
+
* json
|
26
|
+
* oauth
|
27
|
+
|
28
|
+
## Getting Started
|
29
|
+
|
30
|
+
In order to use the client, you must have both an API key and an API secret. To obtain your key and secret, you need to first create an account at
|
31
|
+
https://www.semantics3.com/
|
32
|
+
You can access your API access credentials from the user dashboard at https://www.semantics3.com/dashboard/applications
|
33
|
+
|
34
|
+
### Setup Work
|
35
|
+
|
36
|
+
Let's lay the groundwork.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'semantics3'
|
40
|
+
|
41
|
+
# Your Semantics3 API Credentials
|
42
|
+
API_KEY = 'SEM3xxxxxxxxxxxxxxxxxxxxxx'
|
43
|
+
API_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
44
|
+
|
45
|
+
# Set up a client to talk to the Semantics3 API
|
46
|
+
sem3 = Semantics3::Products.new(API_KEY,API_SECRET)
|
47
|
+
```
|
48
|
+
|
49
|
+
### First Query aka 'Hello World':
|
50
|
+
|
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.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# Build the query
|
55
|
+
sem3.products_field( "cat_id", 4992 )
|
56
|
+
sem3.products_field( "brand", "Toshiba" )
|
57
|
+
|
58
|
+
# Make the query
|
59
|
+
productsHash = sem3.get_products()
|
60
|
+
|
61
|
+
# View the results of the query
|
62
|
+
puts productsHash.to_json
|
63
|
+
```
|
64
|
+
|
65
|
+
## Examples
|
66
|
+
|
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
|
68
|
+
|
69
|
+
### Explore the Category Tree
|
70
|
+
|
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
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# Build the query
|
75
|
+
sem3.categories_field( "cat_id", 4992 )
|
76
|
+
|
77
|
+
# Make the query
|
78
|
+
categoriesHash = sem3.get_categories()
|
79
|
+
|
80
|
+
# View the results of the query
|
81
|
+
puts categoriesHash.to_json
|
82
|
+
```
|
83
|
+
|
84
|
+
### Nested Search Query
|
85
|
+
|
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}}}}'.
|
87
|
+
|
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.
|
89
|
+
|
90
|
+
```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 )
|
99
|
+
|
100
|
+
# Let's make a modification - say we no longer want the weight attribute
|
101
|
+
sem3.remove( "products", "brand", "weight" )
|
102
|
+
|
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
|
107
|
+
|
108
|
+
# Make the query
|
109
|
+
productsHash = sem3.get_products
|
110
|
+
|
111
|
+
# View the results of the query
|
112
|
+
puts productsHash.to_json
|
113
|
+
```
|
114
|
+
|
115
|
+
### Pagination
|
116
|
+
|
117
|
+
Let's now look at doing pagination, continuing from where we stopped previously.
|
118
|
+
|
119
|
+
```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
|
126
|
+
```
|
127
|
+
|
128
|
+
### Explore Price Histories
|
129
|
+
|
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, ...)
|
131
|
+
|
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).
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
# Build the query
|
136
|
+
sem3.add( "offers", "sem3_id", "4znupRCkN6w2Q4Ke4s6sUC")
|
137
|
+
sem3.add( "offers", "seller", ["ATRQ56T3H9TM5","LFleurs","Frys","Walmart"] )
|
138
|
+
sem3.add( "offers", "currency", "USD")
|
139
|
+
sem3.add( "offers", "price", "gte", 30)
|
140
|
+
sem3.add( "offers", "lastrecorded_at", "gte", 1348654600)
|
141
|
+
|
142
|
+
# Make the query
|
143
|
+
offersHash = sem3.get_offers
|
144
|
+
#Alternatively we could also do
|
145
|
+
offersHash = sem3.run_query( "offers" )
|
146
|
+
|
147
|
+
# View the results of the query
|
148
|
+
puts offersHash.to_json
|
149
|
+
```
|
150
|
+
|
151
|
+
## Contributing
|
152
|
+
|
153
|
+
Use GitHub's standard fork/commit/pull-request cycle. If you have any questions, email <support@semantics3.com>.
|
154
|
+
|
155
|
+
## Author
|
156
|
+
|
157
|
+
* Sivamani VARUN <varun@semantics3.com>
|
158
|
+
|
159
|
+
## Copyright
|
160
|
+
|
161
|
+
Copyright (c) 2013 Semantics3 Inc.
|
162
|
+
|
163
|
+
## License
|
164
|
+
|
165
|
+
The "MIT" License
|
166
|
+
|
167
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
168
|
+
of this software and associated documentation files (the "Software"), to deal
|
169
|
+
in the Software without restriction, including without limitation the rights
|
170
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
171
|
+
copies of the Software, and to permit persons to whom the Software is
|
172
|
+
furnished to do so, subject to the following conditions:
|
173
|
+
|
174
|
+
The above copyright notice and this permission notice shall be included in
|
175
|
+
all copies or substantial portions of the Software.
|
176
|
+
|
177
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
178
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
179
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
180
|
+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
181
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
182
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
183
|
+
DEALINGS IN THE SOFTWARE.
|
184
|
+
|
185
|
+
|
data/examples/example.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'semantics3'
|
4
|
+
#
|
5
|
+
################################################################################
|
6
|
+
# Simple test script to showcase use of the semantics3 Ruby gem to interface
|
7
|
+
# with the Semantics3 Products API.
|
8
|
+
#
|
9
|
+
# Quickstart guide: https://semantics3.com/quickstart
|
10
|
+
# API Documentation: https://semantics3.com/docs
|
11
|
+
#
|
12
|
+
# Author: Sivamani VARUN <varun@semantics3.com>
|
13
|
+
# Copyright (c) 2013 Semantics3 Inc.
|
14
|
+
#
|
15
|
+
# The MIT License from http://www.opensource.org/licenses/mit-license.php
|
16
|
+
#
|
17
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
18
|
+
# of this software and associated documentation files (the "Software"), to deal
|
19
|
+
# in the Software without restriction, including without limitation the rights
|
20
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
21
|
+
# copies of the Software, and to permit persons to whom the Software is
|
22
|
+
# furnished to do so, subject to the following conditions:
|
23
|
+
#
|
24
|
+
# The above copyright notice and this permission notice shall be included in
|
25
|
+
# all copies or substantial portions of the Software.
|
26
|
+
#
|
27
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
28
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
29
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
30
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
31
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
32
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
33
|
+
# THE SOFTWARE.
|
34
|
+
#
|
35
|
+
#################################################################################
|
36
|
+
|
37
|
+
API_KEY = 'YOUR_SEM3_API_KEY'
|
38
|
+
API_SECRET = 'YOU_SEM3_API_SECRET'
|
39
|
+
sem3 = Semantics3::Products.new(API_KEY,API_SECRET)
|
40
|
+
|
41
|
+
sem3.products_field( "cat_id", 4992 )
|
42
|
+
sem3.products_field( "brand", "Toshiba" )
|
43
|
+
sem3.products_field( "weight", "gte", 1000000 )
|
44
|
+
sem3.products_field( "weight", "lt", 1500000 )
|
45
|
+
sem3.products_field( "sitedetails", "name", "newegg.com" )
|
46
|
+
sem3.products_field( "sitedetails", "latestoffers", "currency", "USD" )
|
47
|
+
sem3.products_field( "sitedetails", "latestoffers", "price", "gte", 100 )
|
48
|
+
|
49
|
+
# Let's view the JSON query we just constructed. This is a good starting point to debug, if you are getting incorrect
|
50
|
+
# results for your query
|
51
|
+
constructedJson = sem3.get_query_json( "products" )
|
52
|
+
puts constructedJson
|
53
|
+
|
54
|
+
# Make the query
|
55
|
+
productsHash = sem3.get_products
|
56
|
+
|
57
|
+
# Iterate throught the result of the pages
|
58
|
+
page = 0
|
59
|
+
while (productsHash = sem3.iterate_products) do
|
60
|
+
page = page + 1
|
61
|
+
puts "Iterating through page: #{page}"
|
62
|
+
end
|
data/lib/semantics3.rb
ADDED
@@ -0,0 +1,262 @@
|
|
1
|
+
# Ruby bindings for the Semantics3 APIs
|
2
|
+
# Quickstart: https://semantics3.com/quickstart
|
3
|
+
# Docs: https://semantics3.com/docs
|
4
|
+
#
|
5
|
+
# Author: Sivamani Varun (varun@semantics3.com)
|
6
|
+
# Copyright 2013 Semantics3 Inc., see LICENSE
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'oauth'
|
10
|
+
require 'uri'
|
11
|
+
require 'json'
|
12
|
+
|
13
|
+
module Semantics3
|
14
|
+
@auth={}
|
15
|
+
|
16
|
+
class Base
|
17
|
+
def initialize(api_key,api_secret)
|
18
|
+
@api_key = api_key
|
19
|
+
@api_secret = api_secret
|
20
|
+
|
21
|
+
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 == ''
|
22
|
+
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 == ''
|
23
|
+
|
24
|
+
consumer = OAuth::Consumer.new(@api_key, @api_secret)
|
25
|
+
@auth = OAuth::AccessToken.new(consumer)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
#returns a value
|
31
|
+
def _make_request(endpoint, params)
|
32
|
+
url = 'https://api.semantics3.com/v1/' + endpoint + '?q=' + URI.escape(params)
|
33
|
+
|
34
|
+
#puts "url = #{url}"
|
35
|
+
response = @auth.get(url)
|
36
|
+
|
37
|
+
#-- Response.code - TBD
|
38
|
+
JSON.parse response.body
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
class Products < Base
|
44
|
+
@@data_query = {}
|
45
|
+
@@query_result = {}
|
46
|
+
|
47
|
+
MAX_LIMIT = 10
|
48
|
+
|
49
|
+
#Offers
|
50
|
+
|
51
|
+
def offers_field(*fields)
|
52
|
+
add("offers",*fields)
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_offers
|
56
|
+
run_query("offers")
|
57
|
+
end
|
58
|
+
|
59
|
+
#Categories
|
60
|
+
|
61
|
+
def categories_field(*fields)
|
62
|
+
add("categories",*fields)
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_categories
|
66
|
+
run_query("categories")
|
67
|
+
end
|
68
|
+
|
69
|
+
#Products
|
70
|
+
|
71
|
+
def products_field(*fields)
|
72
|
+
add("products",*fields)
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_products
|
76
|
+
run_query("products")
|
77
|
+
end
|
78
|
+
|
79
|
+
def all_products
|
80
|
+
if not @@query_result.has_key?(results)
|
81
|
+
raise Error.new('Undefined Query','Query result is undefined. You need to run a query first.')
|
82
|
+
end
|
83
|
+
@@query_result['results']
|
84
|
+
end
|
85
|
+
|
86
|
+
def iterate_products
|
87
|
+
limit = MAX_LIMIT
|
88
|
+
prodRef = @@data_query['products']
|
89
|
+
|
90
|
+
if (not ( @@query_result.has_key?('total_results_count') ) ) or ( @@query_result['offset'] >= @@query_result['total_results_count'] )
|
91
|
+
return
|
92
|
+
end
|
93
|
+
|
94
|
+
if prodRef.has_key?('limit')
|
95
|
+
limit = prodRef['limit']
|
96
|
+
end
|
97
|
+
|
98
|
+
if not prodRef.has_key?('offset')
|
99
|
+
prodRef['offset'] = limit
|
100
|
+
else
|
101
|
+
prodRef['offset'] = prodRef['offset'] + limit
|
102
|
+
end
|
103
|
+
|
104
|
+
get_products()
|
105
|
+
end
|
106
|
+
|
107
|
+
#General
|
108
|
+
|
109
|
+
def add(endpoint,*fields)
|
110
|
+
|
111
|
+
#-- If not defined endpoint, throw error
|
112
|
+
if not ( endpoint.kind_of? String and endpoint != '')
|
113
|
+
raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products','endpoint')
|
114
|
+
end
|
115
|
+
|
116
|
+
if not @@data_query.has_key?(endpoint)
|
117
|
+
@@data_query[endpoint] = {}
|
118
|
+
end
|
119
|
+
|
120
|
+
prodRef = @@data_query[endpoint]
|
121
|
+
|
122
|
+
for i in 1..(fields.length - 1)
|
123
|
+
if not prodRef.has_key?(fields[i-1])
|
124
|
+
prodRef[fields[i-1]] = {}
|
125
|
+
end
|
126
|
+
if i != (fields.length - 1)
|
127
|
+
prodRef = prodRef[fields[i-1]]
|
128
|
+
else
|
129
|
+
prodRef[ fields[i-1] ] = fields[i]
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
#-- To be removed
|
134
|
+
#puts @@data_query.inspect
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
def remove(endpoint,*fields)
|
139
|
+
|
140
|
+
#-- If not defined endpoint, throw error
|
141
|
+
if not ( endpoint.kind_of? String and endpoint != '')
|
142
|
+
raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products','endpoint')
|
143
|
+
end
|
144
|
+
|
145
|
+
valid = 0
|
146
|
+
prodRef = {}
|
147
|
+
arrayCt = 0
|
148
|
+
|
149
|
+
if @@data_query.has_key?(endpoint)
|
150
|
+
prodRef = data_query[endpoint]
|
151
|
+
arrayCt = fields.length-1
|
152
|
+
valid = 1
|
153
|
+
|
154
|
+
for i in 0..(arrayCt-1)
|
155
|
+
if prodRef.has_key?(fields[i])
|
156
|
+
prodRef = prodRef[ fields[i] ]
|
157
|
+
prodRef[ fields[i-1] ] = {}
|
158
|
+
else
|
159
|
+
valid = 0
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
if valid == 1
|
165
|
+
prodRef.delete(fields[arrayCt])
|
166
|
+
else
|
167
|
+
#-- Throw error
|
168
|
+
raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products', 'endpoint')
|
169
|
+
end
|
170
|
+
|
171
|
+
#-- To be removed
|
172
|
+
#puts @@data_query.inspect
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
def get_query(endpoint)
|
177
|
+
if not @@data_query.has_key?(endpoint)
|
178
|
+
raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products', 'endpoint')
|
179
|
+
end
|
180
|
+
@@data_query[endpoint]
|
181
|
+
end
|
182
|
+
|
183
|
+
def get_query_json(endpoint)
|
184
|
+
if not @@data_query.has_key?(endpoint)
|
185
|
+
raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products', 'endpoint')
|
186
|
+
end
|
187
|
+
@@data_query[endpoint].to_json
|
188
|
+
end
|
189
|
+
|
190
|
+
def get_results
|
191
|
+
@@query_result
|
192
|
+
end
|
193
|
+
|
194
|
+
def get_results_json
|
195
|
+
@@query_result.to_json
|
196
|
+
end
|
197
|
+
|
198
|
+
def clear
|
199
|
+
@@data_query={}
|
200
|
+
@@query_result={}
|
201
|
+
end
|
202
|
+
|
203
|
+
def run_query(endpoint,*params)
|
204
|
+
|
205
|
+
#-- If not defined endpoint, throw error
|
206
|
+
if not ( endpoint.kind_of? String and endpoint != '')
|
207
|
+
raise Error.new('Undefined Endpoint','Query Endpoint was not defined. You need to provide one. Eg: products','endpoint')
|
208
|
+
end
|
209
|
+
|
210
|
+
data = params[0]
|
211
|
+
|
212
|
+
if data == nil
|
213
|
+
@@query_result = _make_request(endpoint,@@data_query[endpoint].to_json)
|
214
|
+
else
|
215
|
+
if not data.is_a?(Hash) and not data.is_a?(String)
|
216
|
+
#-- Throw error - neither string nor hash
|
217
|
+
raise Error.new('Invalid Input','You submitted an invalid input. Input has to be either JSON string or hash')
|
218
|
+
else
|
219
|
+
#-- Data is Hash ref. Great just send it.
|
220
|
+
if data.is_a?(Hash)
|
221
|
+
@@query_result = _make_request(endpoint,data.to_json)
|
222
|
+
#-- Data is string
|
223
|
+
elsif data.is_a?(String)
|
224
|
+
#-- Check if it's valid JSON
|
225
|
+
if JSON.is_json?(data)
|
226
|
+
@@query_result = _make_request(endpoint,data)
|
227
|
+
else
|
228
|
+
raise Error.new('Invalid Input','You submitted an invalid JSON query string')
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
@@query_result
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
237
|
+
|
238
|
+
class Error < StandardError
|
239
|
+
attr_reader :type
|
240
|
+
attr_reader :message
|
241
|
+
attr_reader :param
|
242
|
+
|
243
|
+
def initialize(type=nil, message=nil, param=nil)
|
244
|
+
@message = 'Error: ' + type + ' Message: ' + message
|
245
|
+
@message += ' Failed at parameter: ' + param if not param.nil?
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
module JSON
|
252
|
+
def self.is_json?(foo)
|
253
|
+
begin
|
254
|
+
return false unless foo.is_a?(String)
|
255
|
+
JSON.parse(foo).all?
|
256
|
+
rescue JSON::ParserError
|
257
|
+
false
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
|
data/semantics3.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
2
|
+
|
3
|
+
spec = Gem::Specification.new do |s|
|
4
|
+
s.name = 'semantics3'
|
5
|
+
s.version = '0.01'
|
6
|
+
s.summary = 'Ruby bindings for the Semantics3 API'
|
7
|
+
s.description = 'Get access to a constantly updated database of product and price ifnormations. See https://semantics3.com/ for more information.'
|
8
|
+
s.authors = ['Sivamani Varun']
|
9
|
+
s.email = ['varun@semantics3.com']
|
10
|
+
s.homepage = 'https://semantics3.com'
|
11
|
+
s.require_paths = %w{lib}
|
12
|
+
|
13
|
+
s.add_dependency('json', '~> 1.7.0')
|
14
|
+
s.add_dependency('oauth', '~> 0.4.6')
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: semantics3
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.01"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Sivamani Varun
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-02-13 00:00:00 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: json
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 11
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 7
|
31
|
+
- 0
|
32
|
+
version: 1.7.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: oauth
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
- 4
|
47
|
+
- 6
|
48
|
+
version: 0.4.6
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Get access to a constantly updated database of product and price ifnormations. See https://semantics3.com/ for more information.
|
52
|
+
email:
|
53
|
+
- varun@semantics3.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Changes
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- examples/example.rb
|
67
|
+
- lib/semantics3.rb
|
68
|
+
- semantics3.gemspec
|
69
|
+
homepage: https://semantics3.com
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.8.24
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Ruby bindings for the Semantics3 API
|
102
|
+
test_files: []
|
103
|
+
|