class Sevennet::Api

Constants

SERVICE_URL
VERSION

Public Class Methods

configure() { |options| ... } click to toggle source
# File lib/sevennet/api.rb, line 29
def self.configure(&proc)
  raise ArgumentError, "Block is required." unless block_given?
  yield @@options
end
get_shopping_category(category_code, opts = {}) click to toggle source

Search categories by category code.

# File lib/sevennet/api.rb, line 51
def self.get_shopping_category(category_code, opts = {})
  opts[:operation] = 'GetShoppingCategory'
  opts[:CategoryCode] = category_code

  self.send_request(opts)
end
get_spc_category(category_code, opts = {}) click to toggle source

Search spc categories by category code.

# File lib/sevennet/api.rb, line 35
def self.get_spc_category(category_code, opts = {})
  opts[:operation] = 'GetSpcCategory'
  opts[:CategoryCode] = category_code

  self.send_request(opts)
end
options() click to toggle source

Default search options

# File lib/sevennet/api.rb, line 20
def self.options
  @@options
end
options=(opts) click to toggle source

Set default search options

# File lib/sevennet/api.rb, line 25
def self.options=(opts)
  @@options = opts
end
search_content_match_product(content, opts = {}) click to toggle source

Search products by contents.

# File lib/sevennet/api.rb, line 98
def self.search_content_match_product(content, opts = {})

  raise ArgumentError, "Content is required." if content.to_s.empty?

  opts[:operation] = 'SearchContentMatchProduct'
  opts[:Content] = content

  self.send_request(opts)
end
search_content_match_ranking(category_code, content, opts = {}) click to toggle source

Search products by category code and contents.

# File lib/sevennet/api.rb, line 109
def self.search_content_match_ranking(category_code, content, opts = {})

  raise ArgumentError, "TopCategoryCode and Content is required." if content.to_s.empty? || category_code.to_s.empty?

  opts[:operation] = 'SearchContentMatchRanking'
  opts[:Content] = content
  opts[:TopCategoryCode] = category_code

  
  self.send_request(opts)
end
search_product(terms, opts = {}) click to toggle source

Search products with search terms.

# File lib/sevennet/api.rb, line 59
def self.search_product(terms, opts = {})
  if terms.to_s.empty? && (options[:CategoryCode].to_s.empty? && opts[:CategoryCode].to_s.empty?)
    raise ArgumentError, "CategoryCode or KeywordIn is required."
  end

  opts[:operation] = 'SearchProduct'
  opts[:KeywordIn] = terms.to_s

  self.send_request(opts)
end
search_product_review(product_code, opts = {}) click to toggle source

Search a product reviews with product code. For other search type other than keywords, please specify :type => ‘ProductStandardCode’.

# File lib/sevennet/api.rb, line 82
def self.search_product_review(product_code, opts = {})
  opts[:operation] = 'SearchProductReview'

  type = (opts.delete(:type) || options.delete(:type))
  if type
    opts[type.to_sym] = product_code
  else 
    opts[:ProductCode] = product_code
  end

  raise ArgumentError, "ProductCode is required." if product_code.to_s.empty?
  
  self.send_request(opts)
end
search_ranking(category_code, opts = {}) click to toggle source

Search products by category code.

# File lib/sevennet/api.rb, line 71
def self.search_ranking(category_code, opts = {})
  opts[:operation] = 'SearchRanking'
  opts[:CategoryCode] = category_code
  
  raise ArgumentError, "CategoryCode is required." if category_code.to_s.empty?
  
  self.send_request(opts)
end
search_spc_shop(terms, opts = {}) click to toggle source

Search spc shops with search terms.

# File lib/sevennet/api.rb, line 43
def self.search_spc_shop(terms, opts = {})
  opts[:operation] = 'SearchSpcShop'
  opts[:KeywordIn] = terms.to_s

  self.send_request(opts)
end
send_request(opts) click to toggle source

Generic send request to API REST service. You have to specify the :operation parameter.

# File lib/sevennet/api.rb, line 122
def self.send_request(opts)
  opts = self.options.merge(opts) if self.options
  
  # Include other required options
  opts[:Timestamp] = Time.now.utc.strftime('%Y-%m-%dT%XZ')

  request_url = prepare_url(opts)

  res = Net::HTTP.get_response(URI::parse(request_url))
  unless res.kind_of? Net::HTTPSuccess
    raise Sevennet::RequestError, "HTTP Response: #{res.code} #{res.message}"
  end
  Response.new(res.body)
end

Private Class Methods

prepare_url(opts) click to toggle source
# File lib/sevennet/api.rb, line 193
def self.prepare_url(opts)
  secret_key = opts.delete(:APISecretKey)
  operation = opts.delete(:operation)
  request_url = "#{SERVICE_URL}/#{operation}"
  
  qs = []
  qr = []
  
  opts = opts.collect do |a,b| 
    [a.to_s, b.to_s] 
  end
  
  opts = opts.sort do |c,d| 
    c[0].to_s <=> d[0].to_s
  end
  
  opts.each do |e| 
    next if e[1].empty? || e[1].nil?
    qs << "#{e[0]}=#{e[1]}"
    qr << "#{e[0]}=#{CGI.escape(e[1])}"
  end
  
  request_to_sign ="GET|#{request_url}|#{qs.join('|')}"
  signature = "&Signature=#{sign_request(request_to_sign, secret_key)}"
  "#{request_url}?#{qr.join('&')}#{signature}"
end
sign_request(url, key) click to toggle source
# File lib/sevennet/api.rb, line 220
def self.sign_request(url, key)
  signature = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, key, CGI.escape(url))
  signature = Base64.encode64(signature).chomp
  signature
end