shopsense-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/shopsense.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'net/http'
2
+ require 'yajl'
3
+ =begin rdoc
4
+ Shopsense is an easy to use Ruby interface for the {Shopstyle API}[https://shopsense.shopstyle.com/page/ShopSenseHome],
5
+ also known a ShopSense. The ShopStyle API is a free service from ShopStyle that pays you for sending traffic
6
+ to online retailers from your blog, website, or application.
7
+
8
+ The ShopStyle API allows client applications to retrieve the underlying data for all the basic elements of the
9
+ ShopStyle websites, including products, brands, retailers, categories, and looks. For ease of development,
10
+ the API is a REST-style web service, composed of simple HTTP GET requests. Data is returned to the client in
11
+ either XML or JSON formats. The API is client-language independent and easy to use from PHP, Java, JavaScript,
12
+ or any other modern development context.
13
+
14
+ Shopsense provides a number of helpful classes and methods for integrating data aquired from the ShopStyle API into
15
+ your application.
16
+
17
+ == Roadmap
18
+ * If you think you found a bug in Shopsense see DEVELOPERS@Bugs
19
+ * If you want to use Shopsense to interface with the ShopStyle API
20
+ see Shopsense::API
21
+ =end
22
+
23
+ require 'shopsense/client'
24
+ require 'shopsense/api'
@@ -0,0 +1,200 @@
1
+ module Shopsense
2
+
3
+ class API < Client
4
+
5
+ # Searches the shopsense API
6
+ # @param [String] search_string
7
+ # The string to be in the query.
8
+ # @param [Integer] index
9
+ # The start index of results returned.
10
+ # @param [Integer] num_results
11
+ # The number of results to be returned.
12
+ # @return A list of Product objects. Each Product has an id, name,
13
+ # description, price, retailer, brand name, categories, images in small/medium/large,
14
+ # and a URL that forwards to the retailer's site.
15
+ def search( search_string = nil, index = 0, num_results = 10)
16
+ raise "no search string provieded!" if( search_string === nil)
17
+
18
+ fts = "fts=" + search_string.split().join( '+').to_s
19
+ min = "min=" + index.to_s
20
+ count = "count=" + num_results.to_s
21
+ args = [fts, min, count].join( '&')
22
+
23
+ return call_api( __method__, args)
24
+ end
25
+
26
+
27
+ # This method returns a list of categories and product counts that describe the results
28
+ # of a given product query. The query is specified using the product query parameters.
29
+ # @param [String] search_string The string to be in the query.
30
+ # @return [String] A list of Category objects. Each Category has an id, name, and count
31
+ # of the number of query results in that category.
32
+ def get_category_histogram( search_string = nil)
33
+ raise "no search string provieded!" if( search_string === nil)
34
+
35
+ fts = "fts=" + search_string.split().join( '+').to_s
36
+
37
+ return call_api( __method__, fts)
38
+ end
39
+
40
+ # This method returns a list of categories and product counts that describe the results
41
+ # of a given product query. The query is specified using the product query parameters.
42
+ # @param [String] filter_type
43
+ # The type of filter data to return. Possible values are
44
+ # Brand, Retailer, Price, Discount, Size and Color.
45
+ # @param [String] search_string The string to be in the query.
46
+ # @return [String] A list of Category objects. Each Category has an id, name, and count
47
+ # of the number of query results in that category.
48
+ def get_filter_histogram( filter_type = nil, search_string = nil)
49
+ raise "no search string provieded!" if( search_string === nil)
50
+ raise "invalid filter type" if( !self.filter_types.include?( filter_type))
51
+
52
+ filterType = "filterType=" + filter_type.to_s
53
+ fts = "fts=" + search_string.split().join( '+').to_s
54
+ args = [filterType, fts].join( '&')
55
+
56
+ return call_api( __method__, args)
57
+ end
58
+
59
+ # This method returns a list of brands that have live products. Brands that have
60
+ # very few products will be omitted.
61
+ # @return [String] A list of all Brands, with id, name, url, and synonyms of each.
62
+ def get_brands
63
+ return call_api( __method__)
64
+ end
65
+
66
+ # This method returns information about a particular look and its products.
67
+ # @param [Integer] look_id
68
+ # The ID number of the look. An easy way to get a look's ID is
69
+ # to go to the Stylebook page that contains the look at the ShopStyle website and
70
+ # right-click on the button that you use to edit the look. From the popup menu, select
71
+ # "Copy Link" and paste that into any text editor. The "lookId" query parameter of that
72
+ # URL is the value to use for this API method.
73
+ # @return [String] single look, with title, description, a set of tags, and a list of products.
74
+ # The products have the fields listed (see #search)
75
+ def get_look( look_id = nil)
76
+ raise "no look_id provieded!" if( look_id === nil)
77
+
78
+ look = "look=" + look_id.to_s
79
+
80
+ return call_api( __method__, look)
81
+ end
82
+
83
+ # This method returns a list of retailers that have live products.
84
+ # @return [Sting] A list of all Retailers, with id, name, and url of each.
85
+ def get_retailers
86
+ return call_api( __method__)
87
+ end
88
+
89
+ # This method returns information about a particular user's Stylebook, the
90
+ # looks within that Stylebook, and the title and description associated with each look.
91
+ # @param [String] username
92
+ # The username of the Stylebook owner.
93
+ # @param [Integer] index
94
+ # The start index of results returned.
95
+ # @param [Integer] num_results
96
+ # The number of results to be returned.
97
+ # @return [String]
98
+ # A look id of the user's Stylebook, the look id of each individual look within that Stylebook,
99
+ # and the title and description associated with each look.
100
+ def get_stylebook( user_name = nil, index = 0, num_results = 10)
101
+ raise "no user_name provieded!" if( user_name === nil)
102
+
103
+ handle = "handle=" + user_name.to_s
104
+ min = "min=" + index.to_s
105
+ count = "count=" + num_results.to_s
106
+ args = [handle, min, count].join( '&')
107
+
108
+ return call_api( __method__, args)
109
+ end
110
+
111
+ # This method returns information about looks that match different kinds of searches.
112
+ # @param [Integer] look_type
113
+ # The type of search to perform. Supported values are:
114
+ # New - Recently created looks.
115
+ # TopRated - Recently created looks that are highly rated.
116
+ # Celebrities - Looks owned by celebrity users.
117
+ # Featured - Looks from featured stylebooks.
118
+ # @param [String] username
119
+ # The username of the Stylebook owner.
120
+ # @param [Integer] index
121
+ # The start index of results returned.
122
+ # @param [Integer] num_results
123
+ # The number of results to be returned.
124
+ # @return [String]
125
+ # A list of looks of the given type.
126
+ def get_looks( look_type = nil, index = 0, num_results = 10)
127
+ raise "invalid filter type must be one of the following: #{self.look_types}" if( !self.look_types.include?( look_type))
128
+
129
+ type = "type=" + look_type.to_s
130
+ min = "min=" + index.to_s
131
+ count = "count=" + num_results.to_s
132
+ args = [type, min, count].join( '&')
133
+
134
+ return call_api( __method__, args)
135
+ end
136
+
137
+ # TODO:
138
+ # This method does not return a reponse of XML or JSON data like the other elements of the API.
139
+ # Instead, it forwards the user to the retailer's product page for a given product. It is the
140
+ # typical behavior to offer when the user clicks on a product. The apiSearch method returns URLs
141
+ # that call this method for each of the products it returns.
142
+ # @param [Integer]
143
+ # The ID number of the product. An easy way to get a product's ID is to find the product somewhere
144
+ # in the ShopStyle UI and right-click on the product image. From the popup menu, select "Copy Link"
145
+ # ("Copy link location" or "Copy shortcut" depending on your browser) and paste that into any text
146
+ # editor. The "id" query parameter of that URL is the value to use for this API method.
147
+ # @return [String]
148
+ # A web link to the retailer. $1 of the $0
149
+ def visit_retailer( id)
150
+
151
+ end
152
+
153
+ # This method returns the popular brands for a given category along with a sample product for the
154
+ # brand-category combination.
155
+ # @param [String] category
156
+ # Category you want to restrict the popularity search for. This is an optional
157
+ # parameter. If category is not supplied, all the popular brands regardless of category will be returned.
158
+ # @return [String] A list of trends in the given category. Each trend has a brand, category, url, and
159
+ # optionally the top-ranked product for each brand/category.
160
+ def get_trends( category = "", products = 0)
161
+ cat = "cat=" + category.to_s
162
+ products = "products=" + products.to_s
163
+ args = [cat, products].join( '&')
164
+
165
+ return call_api( __method__, args)
166
+ end
167
+ private
168
+
169
+ # This method is used for making the http calls building off the DSL of this module.
170
+ # @param [String] method
171
+ # The method which is to be used in the call to Shopsense
172
+ # @param [String] args
173
+ # A concatenated group of arguments seperated by a an & symbol and spces substitued with a + symbol.
174
+ # @return [String] A list of the data returned
175
+ def call_api( method, args = nil)
176
+ method_url = self.api_url + self.send( "#{method}_path")
177
+ pid = "pid=" + self.partner_id
178
+ format = "format=" + self.format
179
+ site = "site=" + self.site
180
+
181
+ if( args === nil) then
182
+ uri = URI.parse( method_url.to_s + [pid, format, site].join('&').to_s)
183
+ else
184
+ uri = URI.parse( method_url.to_s + [pid, format, site, args].join('&').to_s)
185
+ end
186
+
187
+ return Net::HTTP.get( uri)
188
+ end
189
+ end
190
+ end
191
+
192
+ # @macro [new] api.index
193
+ # @param [Integer] index
194
+ # The start index of results returned.
195
+ # @macro [new] api.num_results
196
+ # @param [Integer] num_results
197
+ # The number of results to be returned.
198
+ # @macro [new] api.search_string
199
+ # @param [String] search_string
200
+ # The string to be in the query.
@@ -0,0 +1,43 @@
1
+ module Shopsense
2
+ class Client
3
+ def initialize( args = { })
4
+ raise "No partner_id" if( !args.has_key?( 'partner_id'))
5
+
6
+ attr_accessors = {
7
+ #:
8
+ 'format' => 'json',
9
+ 'site' => 'us'}
10
+ attr_accessors.each_key{ |key| (class << self; self; end).send( :attr_accessor, key.to_sym)}
11
+
12
+ attr_readers = {
13
+ 'partner_id' => nil,
14
+ 'api_url' => 'http://api.shopstyle.com',
15
+ 'search_path' => '/action/apiSearch?',
16
+ 'get_brands_path' => '/action/apiGetBrands?',
17
+ 'get_look_path' => '/action/apiGetLook?',
18
+ 'get_looks_path' => '/action/apiGetLooks?',
19
+ 'get_retailers_path' => '/action/apiGetRetailers?',
20
+ 'get_stylebook_path' => '/action/apiGetStylebook?',
21
+ 'visit_retailers_path' => '/action/apiVisitRetailer?',
22
+ 'get_trends_path' => '/action/apiGetTrends?',
23
+ 'get_category_histogram_path' => '/action/apiGetCategoryHistogram?',
24
+ 'get_filter_histogram_path' => '/action/apiGetFilterHistogram?',
25
+ 'filter_types' => ['Brands', 'Retailer', 'Price', 'Discount', 'Size', 'Color'],
26
+ 'look_types' => ['New', 'TopRated', 'Celebrities', 'Featured'],
27
+ 'formats' => ['xml', 'json', 'json2', 'jsonvar', 'jsonvar2', 'jsonp', 'rss'],
28
+ 'sites' => ['www.shopstyle.com', 'www.shopstyle.co.uk']}
29
+ attr_readers.each_key{ |key| (class << self; self; end).send( :attr_reader, key.to_sym)}
30
+
31
+ attr_writers = {}
32
+ attr_writers.each_key{ |key| (class << self; self; end).send( :attr_writer, key.to_sym)}
33
+
34
+ attrs = attr_accessors.merge( attr_readers).merge( attr_writers)
35
+ attrs.each_key do |key|
36
+ attrs[ key] = args[ key] if( args.has_key?( key))
37
+ end
38
+
39
+ attrs.each {|key, value| instance_variable_set( "@#{key}", value)}
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,8 @@
1
+ module Shopsense
2
+ major = 0
3
+ minor = 1
4
+ tiny = 0
5
+ #pre = ""
6
+
7
+ VERSION = [major, minor, tiny].compact.join('.')
8
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopsense-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kurt Rudolph
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70300918516620 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.8'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70300918516620
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &70300918514760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.2'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70300918514760
36
+ description: This Gem provides an easy to use interface for the ShopStyleAPI commonly
37
+ known as ShopSense. ShopSense-Ruby includes a set of convent classes and methods
38
+ designed to make accessing the ShopStyle API from your Ruby application seamless.
39
+ email:
40
+ - ShopSense-Ruby.RubyGems@RudyIndustries.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - lib/shopsense/api.rb
46
+ - lib/shopsense/client.rb
47
+ - lib/shopsense/version.rb
48
+ - lib/shopsense.rb
49
+ homepage: http://RudyIndustries.GitHub.com/shopsense-ruby
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.15
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: ShopSense API! For Ruby! So Great.
73
+ test_files: []
74
+ has_rdoc: