class RestGW2::View
Constants
- COINS
- FAVICONS
- GEM
- HTML
Public Instance Methods
path(str, q={})
click to toggle source
FIXME: controller shouldn't call this directly
# File lib/rest-gw2/server/view.rb, line 62 def path str, q={} RC::Middleware.request_uri( RC::REQUEST_PATH => "#{ENV['RESTGW2_PREFIX']}#{str}", RC::REQUEST_QUERY => q) end
query_p()
click to toggle source
FIXME: controller shouldn't call this directly
# File lib/rest-gw2/server/view.rb, line 57 def query_p @query_p ||= zero_is_nil(request.GET['p']) end
render(name, arg=nil)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 27 def render name, arg=nil erb(:layout){ erb(name, arg) } end
show_guild(g)
click to toggle source
FIXME: controller shouldn't call this directly
# File lib/rest-gw2/server/view.rb, line 51 def show_guild g HTML.new(menu("/guilds/#{g['id']}", h("#{g['name']} [#{g['tag']}]"))) end
sum_items(items)
click to toggle source
FIXME: controller shouldn't call this directly
# File lib/rest-gw2/server/view.rb, line 39 def sum_items items items.inject([0, 0]) do |sum, i| next sum unless i b = i['buys'] s = i['sells'] sum[0] += b['unit_price'] * i['count'] if b sum[1] += s['unit_price'] * i['count'] if s sum end end
sum_trans(trans)
click to toggle source
FIXME: controller shouldn't call this directly
# File lib/rest-gw2/server/view.rb, line 32 def sum_trans trans trans.inject(0) do |sum, t| sum + t['price'] * t['quantity'] end end
Private Instance Methods
abbr_time_ago(time, precision=1)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 275 def abbr_time_ago time, precision=1 return unless time ago = time_ago(time) short = ago.take(precision).join(' ') %Q{(<abbr title="#{time}, #{ago.join(' ')} ago">#{short} ago</abbr>)} end
blank_icon()
click to toggle source
HELPER
# File lib/rest-gw2/server/view.rb, line 148 def blank_icon %Q{<img class="icon" alt="blank" src="https://upload.wikimedia.org/wikipedia/commons/d/d2/Blank.png"/>} end
duration(delta)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 286 def duration delta result = [] [[ 60, :seconds], [ 60, :minutes], [ 24, :hours ], [365, :days ], [999, :years ]]. inject(delta) do |length, (divisor, name)| quotient, remainder = length.divmod(divisor) result.unshift("#{remainder} #{name}") break if quotient == 0 quotient end result end
dye_color(dye)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 263 def dye_color dye %w[cloth leather metal].map do |kind| rgb = dye[kind]['rgb'] rgb && dye_preview(kind, rgb.join(', ')) end.join("\n") end
dye_preview(kind, rgb)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 270 def dye_preview kind, rgb %Q{<span class="icon" title="#{kind}, rgb(#{rgb})"} + %Q{ style="background-color: rgb(#{rgb})"></span>} end
erb(name, arg=nil, &block)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 69 def erb name, arg=nil, &block ERB.new(views(name)).result(binding, &block) end
favicon()
click to toggle source
# File lib/rest-gw2/server/view.rb, line 73 def favicon FAVICONS[Zlib.crc32(query_t.to_s) % FAVICONS.size] end
h(str)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 77 def h str case str when String CGI.escape_html(str) when HTML str.to_s end end
item_attributes(stats)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 236 def item_attributes stats stats.map do |name, value| "#{name}: #{value}" end.join(', ') end
item_class(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 228 def item_class item missing = if item['count'] == 0 then ' missing' end rarity = if item['rarity'] " rarity rarity-#{item['rarity'].downcase}" end "icon#{rarity}#{missing}" end
item_count(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 205 def item_count item c = item['count'] "(#{c})" if c > 1 end
item_link(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 173 def item_link item name = item_name(item) type = item_type(item) title = %Q{ title="#{type}"} if type if item['nolink'] %Q{<span#{title}>#{name}</span>} else menu("/items/#{item['id']}", name) do title end end end
item_name(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 187 def item_name item name = item['name'].to_s h(!name.empty? && name || "?#{item['id']}?") end
item_price(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 210 def item_price item b = item['buys'] s = item['sells'] bb = b && price(b['unit_price']) ss = s && price(s['unit_price']) %Q{#{bb} / #{ss}#{item_total_price(item, b, s)}} if bb || ss end
item_title(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 200 def item_title item d = item['description'] d && d.unpack('U*').map{ |c| "&##{c};" }.join end
item_total_price(item, b, s)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 218 def item_total_price item, b, s count = item['count'] if count > 1 bb = b && price(b['unit_price'] * count) ss = s && price(s['unit_price'] * count) " (#{bb} / #{ss})" end end
item_type(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 193 def item_type item type = [item.dig('details', 'damage_type'), item.dig('details', 'type')].join(' ') !type.empty? && type || nil end
item_wiki(item)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 156 def item_wiki item if item['name'] && item['icon'] name = item['name'].tr(' ', '_') img = %Q{<img class="#{item_class(item)}"} + %Q{ alt="#{item_name(item)}"} + %Q{ title="#{item_title(item)}"} + %Q{ src="#{h item['icon']}"/>} if name.empty? img else %Q{<a href="http://wiki.guildwars2.com/wiki/#{u name}">#{img}</a>} end else blank_icon end end
item_wiki_list(items)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 152 def item_wiki_list items items.map(&method(:item_wiki)).join("\n") end
page(num)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 143 def page num menu(request.path, num.to_s, :p => zero_is_nil(num)) end
price(copper)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 242 def price copper g = copper / 100_00 s = copper % 100_00 / 100 c = copper % 100 l = [g, s, c] n = l.index(&:nonzero?) return '-' unless n l.zip(COINS).drop(n).map do |(num, (name, src))| price_tag(num, name, src) end.join(' ') end
price_gem(num)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 254 def price_gem num price_tag(num, 'gem', GEM) end
price_tag(num, name, src)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 258 def price_tag num, name, src %Q{#{num}<img class="price"} + %Q{ alt="#{name}" title="#{name}" src="#{src}"/>} end
refresh_path()
click to toggle source
# File lib/rest-gw2/server/view.rb, line 94 def refresh_path path(request.path, :p => query_p, :r => '1', :t => query_t) end
time_ago(time)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 282 def time_ago time duration((Time.now - Time.parse(time)).to_i) end
u(str)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 86 def u str CGI.escape(str) if str.kind_of?(String) end
views(name)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 90 def views name File.read("#{__dir__}/view/#{name}.erb") end
zero_is_nil(n)
click to toggle source
# File lib/rest-gw2/server/view.rb, line 304 def zero_is_nil n r = n.to_i r if r != 0 end