Class: SnapdealApi
- Inherits:
-
Object
- Object
- SnapdealApi
- Defined in:
- lib/snapdeal_api.rb,
lib/snapdeal_api/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#get_all_products(rest_url) ⇒ Object
Get all the products from given rest url returns an array of products.
-
#get_categories(format = nil) ⇒ Object
Get all the categories list, in xml/json format.
-
#get_parsed_categories ⇒ Object
Get all the categories list, as key=>value pair.
-
#get_products(rest_url, format = nil) ⇒ Object
Get the products from given rest url.
-
#get_products_api(category) ⇒ Object
Get the rest api url for the given category name.
-
#get_products_by_category(category) ⇒ Object
Get the first 500 products from the category name Output will also contain “nextUrl” which inturn returns next 500 products.
-
#initialize(sd_affiliate_id, sd_affiliate_token, format = "json") ⇒ SnapdealApi
constructor
Initialise object with affiliate id, token and format to send api calls.
- #parse_json(categories) ⇒ Object
- #parse_xml(categories) ⇒ Object
- #set_parsed_categories(categories) ⇒ Object
Constructor Details
#initialize(sd_affiliate_id, sd_affiliate_token, format = "json") ⇒ SnapdealApi
Initialise object with affiliate id, token and format to send api calls
10 11 12 13 14 15 16 17 |
# File 'lib/snapdeal_api.rb', line 10 def initialize(sd_affiliate_id, sd_affiliate_token, format="json") @api = "http://affiliate-feeds.snapdeal.com/feed/#{sd_affiliate_id}" @header = {"Snapdeal-Affiliate-Id" => sd_affiliate_id, "Snapdeal-Token-Id" => sd_affiliate_token} @format = format @categories = {} #cache: avoid subsequent requests @parsed_categories = {} @version = "v1" end |
Instance Method Details
#get_all_products(rest_url) ⇒ Object
Get all the products from given rest url returns an array of products
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/snapdeal_api.rb', line 94 def get_all_products(rest_url) json_arr = [] json_data = JSON.parse(get_products(rest_url, "json")) json_arr << [json_data] while json_data["nextUrl"] json_data = JSON.parse(get_products(json_data["nextUrl"], "json")) json_arr << [json_data] break end jsonout = json_arr end |
#get_categories(format = nil) ⇒ Object
Get all the categories list, in xml/json format
20 21 22 23 24 |
# File 'lib/snapdeal_api.rb', line 20 def get_categories(format = nil) ext = format.nil? ? @format : format rest_url="#{@api}.#{ext}" @categories = RestClient.get rest_url end |
#get_parsed_categories ⇒ Object
Get all the categories list, as key=>value pair
27 28 29 30 31 32 33 |
# File 'lib/snapdeal_api.rb', line 27 def get_parsed_categories if @categories.empty? get_categories end set_parsed_categories(@categories) return @parsed_categories end |
#get_products(rest_url, format = nil) ⇒ Object
Get the products from given rest url
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/snapdeal_api.rb', line 80 def get_products(rest_url, format = nil) format = format.nil? ? @format : format header = @header if format == "json" || format == "xml" header['Accept'] = "application/#{format}" else raise "Wrong format: #{format} provided" end rest_output = RestClient.get rest_url, header end |
#get_products_api(category) ⇒ Object
Get the rest api url for the given category name
66 67 68 69 70 71 |
# File 'lib/snapdeal_api.rb', line 66 def get_products_api(category) if @parsed_categories.empty? get_parsed_categories end @parsed_categories[category] end |
#get_products_by_category(category) ⇒ Object
Get the first 500 products from the category name Output will also contain “nextUrl” which inturn returns next 500 products.
75 76 77 |
# File 'lib/snapdeal_api.rb', line 75 def get_products_by_category(category) get_products(get_products_api(category)) end |
#parse_json(categories) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/snapdeal_api.rb', line 45 def parse_json(categories) cats = JSON.parse(categories) categories_hash = {} cats['apiGroups']['Affiliate']['listingsAvailable'].each do |k,v| categories_hash[k] = v['listingVersions'][@version]['get'] end return categories_hash end |
#parse_xml(categories) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/snapdeal_api.rb', line 54 def parse_xml(categories) categories_hash = {} @doc = Nokogiri::XML(categories) @doc.xpath("//groupListings/apiGroups/entry/value/listingsAvailable/entry").each do |list| if list.xpath("value/listingVersions/entry/key").text == @version categories_hash[list.xpath("key").text] = list.xpath("value/listingVersions/entry/value/get").text end end return categories_hash end |
#set_parsed_categories(categories) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/snapdeal_api.rb', line 35 def set_parsed_categories(categories) if categories.start_with? "{" @parsed_categories = parse_json categories elsif categories.start_with? "<?xml" @parsed_categories = parse_xml categories else raise "Wrong categories format, can't parse" end end |