Module: YahooStoreApi::Helper
Constant Summary collapse
- ENDPOINT =
"https://circus.shopping.yahooapis.jp/ShoppingWebService/V1/".freeze
Instance Method Summary collapse
- #connection(method, with_seller_id: false) ⇒ Object
-
#error_parser(rexml) ⇒ Object
def response_parser.
- #handler(response) ⇒ Object
- #response_parser(rexml, xpoint:) ⇒ Object
Instance Method Details
#connection(method, with_seller_id: false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/yahoo_store_api/helper.rb', line 5 def connection(method, with_seller_id: false) url = ENDPOINT + method if with_seller_id url += "?seller_id=#{@seller_id}" if with_seller_id end Faraday.new(url: url) do |c| c.request :multipart c.request :url_encoded c.adapter :net_http c.headers["Authorization"] = "Bearer " + @access_token end end |
#error_parser(rexml) ⇒ Object
def response_parser
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/yahoo_store_api/helper.rb', line 65 def error_parser(rexml) result = { status: "NG", message: rexml.elements["Error/Message"].text, } result.each do |k, v| self.define_singleton_method(k) { v } end self.define_singleton_method("all") { result } self end |
#handler(response) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/yahoo_store_api/helper.rb', line 19 def handler(response) rexml = REXML::Document.new(response.body) if rexml.elements["ResultSet/Result"] response_parser(rexml, xpoint: "ResultSet/Result") elsif rexml.elements["ResultSet/Status"] response_parser(rexml, xpoint: "ResultSet") elsif rexml.elements["Error/Message"] error_parser(rexml) else puts rexml end end |
#response_parser(rexml, xpoint:) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/yahoo_store_api/helper.rb', line 32 def response_parser(rexml, xpoint:) attributes = {} rexml.elements.each(xpoint) do |result| result.children.each do |el| next if el.to_s.strip.blank? if el.has_elements? el_ary = el.children.reject { |v| v.to_s.blank? }.map { |v| v.text.try!(:force_encoding, "utf-8") }.reject { |v| v.to_s.blank? } attributes[el.name.underscore] = el_ary begin self.define_singleton_method(el.name.underscore) { el_ary } rescue => e puts e end # begin else attributes[el.name.underscore] = el.text begin self.define_singleton_method(el.name.underscore) { el.text.try!(:force_encoding, "utf-8") } rescue => e puts e end end # if el.has_elements? end # result.children.each self.define_singleton_method("all") { attributes } end # xml.elements.each(xpoint) self end |