Module: ResponseFormatter
- Includes:
- ApiErrors
- Included in:
- HotEndpoints, ItemEndpoints, PriceEndpoints, SearchEndpoints, WaitlistEndpoints
- Defined in:
- lib/utils/response_formatter.rb
Overview
used to format responses from the black desert api
Constant Summary
Constants included from ApiErrors
ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE
Class Method Summary collapse
-
.format_cat_response(data) ⇒ Object
for /item/:cat/:sub endpoint.
-
.format_hot_response(data) ⇒ Object
for /hot endpoint.
-
.format_item_response(data) ⇒ Object
for /item/:id endpoint.
-
.format_price_list_response(data) ⇒ Object
for /prices endpoint.
-
.format_search_response(data) ⇒ Object
for /search endpoint.
-
.format_waitlist_response(data) ⇒ Object
for /waitlist endpoint.
-
.try_error(json) ⇒ Object
returns an error message if the black desert api returns an error.
Class Method Details
.format_cat_response(data) ⇒ Object
for /item/:cat/:sub endpoint
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/utils/response_formatter.rb', line 41 def self.format_cat_response(data) json = JSON.parse(data) return try_error json if try_error json { data: json['marketList'].map { |e| { **e, id: e['mainKey'] } } }.to_json rescue NoMethodError, JSON::ParserError ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE end |
.format_hot_response(data) ⇒ Object
for /hot endpoint
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/utils/response_formatter.rb', line 84 def self.format_hot_response(data) json = JSON.parse(data) return try_error json if try_error json { data: json['hotList'].map do |e| { **e, id: e['mainKey'], fluctuationDirection: e['fluctuationType'] == 2 ? 'up' : 'down' } end }.to_json rescue NoMethodError, JSON::ParserError ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE end |
.format_item_response(data) ⇒ Object
for /item/:id endpoint
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/utils/response_formatter.rb', line 23 def self.format_item_response(data) json = JSON.parse(data) return try_error json if try_error json { data: json['detailList'].map do |e| { **e, id: e['mainKey'] }.except('mainKey') end }.to_json rescue NoMethodError, JSON::ParserError ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE end |
.format_price_list_response(data) ⇒ Object
for /prices endpoint
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/utils/response_formatter.rb', line 67 def self.format_price_list_response(data) json = JSON.parse(data) return try_error json if try_error json { data: { pricePoints: json['priceList'], buySellCounts: json['marketConditionList'], **json }.except('marketConditionList', 'priceList', 'resultCode') }.to_json rescue NoMethodError, JSON::ParserError ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE end |
.format_search_response(data) ⇒ Object
for /search endpoint
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/utils/response_formatter.rb', line 54 def self.format_search_response(data) json = JSON.parse(data) return try_error json if try_error json { data: json['list'].map { |e| { **e, id: e['mainKey'] } } }.to_json rescue NoMethodError, JSON::ParserError ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE end |
.format_waitlist_response(data) ⇒ Object
for /waitlist endpoint
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/utils/response_formatter.rb', line 103 def self.format_waitlist_response(data) json = JSON.parse(data) return try_error json if try_error json { data: json['_waitList'].map do |e| { **e, id: e['mainKey'], waitEndTime: DateTime.strptime(e['_waitEndTime'].to_s, '%Q'), waitEndTimestampMs: e['_waitEndTime'], pricePerOne: e['_pricePerOne'] }.except('_pricePerOne', '_waitEndTime') end }.to_json rescue NoMethodError, JSON::ParserError ApiErrors::UNEXPECTED_BLACK_DESERT_RESPONSE end |
.try_error(json) ⇒ Object
returns an error message if the black desert api returns an error
13 14 15 16 17 18 19 20 |
# File 'lib/utils/response_formatter.rb', line 13 def self.try_error(json) return false if json['resultCode'].zero? { resultCode: json['resultCode'], resultMsg: json['resultMsg'] }.to_json end |