Class: Sevennet::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/sevennet/api.rb,
lib/sevennet/api/version.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

SERVICE_URL =
'http://api.7netshopping.jp/ws/affiliate/rest'
VERSION =
"0.0.2"
@@options =
{
  :Version => "2010-08-01",
  :ResponseFormat => "XML"
}

Class Method Summary collapse

Class Method Details

.configure {|@@options| ... } ⇒ Object

Yields:

Raises:

  • (ArgumentError)


29
30
31
32
# 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 = {}) ⇒ Object

Search categories by category code.



51
52
53
54
55
56
# 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 = {}) ⇒ Object

Search spc categories by category code.



35
36
37
38
39
40
# 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

.optionsObject

Default search options



20
21
22
# File 'lib/sevennet/api.rb', line 20

def self.options
  @@options
end

.options=(opts) ⇒ Object

Set default search options



25
26
27
# File 'lib/sevennet/api.rb', line 25

def self.options=(opts)
  @@options = opts
end

.search_content_match_product(content, opts = {}) ⇒ Object

Search products by contents.

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
# 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] = CGI.escape(content)

  self.send_request(opts)
end

.search_content_match_ranking(category_code, content, opts = {}) ⇒ Object

Search products by category code and contents.

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
# 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] = CGI.escape(content)
  opts[:TopCategoryCode] = category_code

  
  self.send_request(opts)
end

.search_product(terms, opts = {}) ⇒ Object

Search products with search terms.



59
60
61
62
63
64
65
66
67
68
# 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] = CGI.escape(terms.to_s)

  self.send_request(opts)
end

.search_product_review(product_code, opts = {}) ⇒ Object

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

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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 = {}) ⇒ Object

Search products by category code.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
# 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 = {}) ⇒ Object

Search spc shops with search terms.



43
44
45
46
47
48
# File 'lib/sevennet/api.rb', line 43

def self.search_spc_shop(terms, opts = {})
  opts[:operation] = 'SearchSpcShop'
  opts[:KeywordIn] = CGI.escape(terms.to_s)

  self.send_request(opts)
end

.send_request(opts) ⇒ Object

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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