Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 7d31c89

Browse files
committed
Added gem files.
1 parent b313708 commit 7d31c89

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source 'https://rubygems.org'
2+
3+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4+
5+
# Specify your gem's dependencies in fortniteapi.gemspec
6+
gemspec

fortniteapi.gemspec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
lib = File.expand_path('lib', __dir__)
2+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3+
4+
Gem::Specification.new do |spec|
5+
spec.name = 'fortniteapi'
6+
spec.version = '0.0.1'
7+
spec.authors = ['xMistt']
8+
spec.summary = 'Ruby implementation for https://fortnite-api.com'
9+
spec.homepage = 'https://github.com/Fortnite-API/ruby-wrapper'
10+
11+
spec.files = Dir['bin/*'] +
12+
Dir['lib/**/*.rb']
13+
14+
spec.require_paths = ['lib']
15+
16+
17+
spec.add_runtime_dependency 'httparty', '~> 0.17.1'
18+
end

lib/fortniteapi.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require_relative 'fortniteapi/api.rb'

lib/fortniteapi/api.rb

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
require 'httparty'
2+
3+
class FortniteAPI
4+
BRCosmetic = Struct.new(:id, :type, :backendType, :rarity, :displayRarity, :backendRarity, :name, :shortDescription, :description, :set, :setText, :backendSeries, :images, :variants, :gameplayTags, :displayAssetPath, :definition, :requiredItemId, :builtInEmoteId, :path, :lastUpdate, :added)
5+
BRNews = Struct.new(:image, :hidden, :messageType, :type, :adspace, :spotlight, :title, :body)
6+
STWNews = Struct.new(:image, :hidden, :messageType, :type, :adspace, :spotlight, :title, :body)
7+
CreativeNews = Struct.new(:image, :hidden, :messageType, :type, :adspace, :spotlight, :title, :body)
8+
ShopItem = Struct.new(:regularPrice, :finalPrice, :isBundle, :giftable, :refundable, :panel, :sortPriority, :banner, :items)
9+
10+
def search_cosmetic(searchQuery, tag='name', language='en', searchLanguage='en')
11+
response = HTTParty.get("https://fortnite-api.com/cosmetics/br/search?#{tag}=#{searchQuery}&language=#{language}&searchLanguage=#{searchLanguage}")
12+
body = JSON.parse(response.body)['data']
13+
if response.code == 200
14+
searchResult = BRCosmetic.new(body['id'], body['type'], body['backendType'], body['rarity'], body['displayRarity'], body['backendRarity'], body['name'], body['shortDescription'], body['description'], body['set'], body['setText'], body['backendSeries'], body['images'], body['variants'], body['gameplayTags'], body['displayAssetPath'], body['definition'], body['requiredItemId'], body['builtInEmoteId'], body['path'], body['lastUpdate'], body['added'])
15+
searchResult
16+
elsif response.code == 404
17+
raise "Error when trying to fetch cosmetic: #{searchQuery} - #{body['error']}"
18+
searchResult = BRCosmetic.new()
19+
searchResult
20+
elsif response.code == 400
21+
raise "Error when using parameter, it might be that it is invalid.: #{tag} - #{body['error']}"
22+
searchResult = BRCosmetic.new()
23+
searchResult
24+
else
25+
raise "An unexpected error occured. Status code: #{response.code}, Request body: #{response.body}"
26+
searchResult = BRCosmetic.new()
27+
searchResult
28+
end
29+
end
30+
31+
def search_cosmetic_id(searchQuery, language='en')
32+
response = HTTParty.get("https://fortnite-api.com/cosmetics/br/#{searchQuery}&language=#{language}")
33+
body = JSON.parse(response.body)['data']
34+
if response.code == 200
35+
searchResult = BRCosmetic.new(body['id'], body['type'], body['backendType'], body['rarity'], body['displayRarity'], body['backendRarity'], body['name'], body['shortDescription'], body['description'], body['set'], body['setText'], body['backendSeries'], body['images'], body['variants'], body['gameplayTags'], body['displayAssetPath'], body['definition'], body['requiredItemId'], body['builtInEmoteId'], body['path'], body['lastUpdate'], body['added'])
36+
searchResult
37+
elsif response.code == 404
38+
raise "Error when trying to fetch cosmetic: #{searchQuery} - #{body['error']}"
39+
searchResult = BRCosmetic.new()
40+
searchResult
41+
elsif response.code == 400
42+
raise "Error when using parameter, it might be that it is invalid.: #{tag} - #{body['error']}"
43+
searchResult = BRCosmetic.new()
44+
searchResult
45+
else
46+
raise "An unexpected error occured. Status code: #{response.code}, Request body: #{response.body}"
47+
searchResult = BRCosmetic.new()
48+
searchResult
49+
end
50+
end
51+
52+
def search_cosmetics(searchQuery, tag='name', language='en', searchLanguage='en')
53+
cosmetics = []
54+
response = HTTParty.get("https://fortnite-api.com/cosmetics/br/search/all?#{tag}=#{searchQuery}&language=#{language}&searchLanguage=#{searchLanguage}")
55+
body = JSON.parse(response.body)['data']
56+
if response.code == 200
57+
for x in body
58+
searchResult = BRCosmetic.new(x['id'], x['type'], x['backendType'], x['rarity'], x['displayRarity'], x['backendRarity'], x['name'], x['shortDescription'], x['description'], x['set'], x['setText'], x['backendSeries'], x['images'], x['variants'], x['gameplayTags'], x['displayAssetPath'], x['definition'], x['requiredItemId'], x['builtInEmoteId'], x['path'], x['lastUpdate'], x['added'])
59+
cosmetics.push(searchResult)
60+
end
61+
cosmetics
62+
elsif response.code == 404
63+
raise "Error when trying to fetch cosmetic: #{searchQuery} - #{body['error']}"
64+
searchResult = BRCosmetic.new()
65+
searchResult
66+
elsif response.code == 400
67+
raise "Error when using parameter, it might be that it is invalid.: #{tag} - #{body['error']}"
68+
searchResult = BRCosmetic.new()
69+
searchResult
70+
else
71+
raise "An unexpected error occured. Status code: #{response.code}, Request body: #{response.body}"
72+
searchResult = BRCosmetic.new()
73+
searchResult
74+
end
75+
end
76+
77+
def all_cosmetics()
78+
response = HTTParty.get("https://fortnite-api.com/cosmetics/br")
79+
JSON.parse(response.body)
80+
end
81+
82+
def get_br_news(language='en')
83+
br_news_list = []
84+
response = HTTParty.get("https://fortnite-api.com/news/br?language=#{language}")
85+
messages = JSON.parse(response.body)['data']['messages']
86+
for x in messages
87+
newsResult = BRNews.new(x['image'], x['hidden'], x['messageType'], x['type'], x['adspace'], x['spotlight'], x['title'], x['body'])
88+
br_news_list.push(newsResult)
89+
end
90+
br_news_list
91+
end
92+
93+
def get_stw_news(language='en')
94+
stw_news_list = []
95+
response = HTTParty.get("https://fortnite-api.com/news/stw?language=#{language}")
96+
messages = JSON.parse(response.body)['data']['messages']
97+
for x in messages
98+
newsResult = STWNews.new(x['image'], x['hidden'], x['messageType'], x['type'], x['adspace'], x['spotlight'], x['title'], x['body'])
99+
stw_news_list.push(newsResult)
100+
end
101+
stw_news_list
102+
end
103+
104+
def get_creative_news(language='en')
105+
creative_news_list = []
106+
response = HTTParty.get("https://fortnite-api.com/news/creative?language=#{language}")
107+
messages = JSON.parse(response.body)['data']['messages']
108+
for x in messages
109+
newsResult = CreativeNews.new(x['image'], x['hidden'], x['messageType'], x['type'], x['adspace'], x['spotlight'], x['title'], x['body'])
110+
creative_news_list.push(newsResult)
111+
end
112+
creative_news_list
113+
end
114+
115+
def get_br_store(language='en')
116+
featured_items = []
117+
daily_items = []
118+
response = HTTParty.get("https://fortnite-api.com/shop/br?language=#{language}")
119+
featured = JSON.parse(response.body)['data']['featured']
120+
for x in featured
121+
featuredItem = ShopItem.new(x['regularPrice'], x['finalPrice'], x['isBundle'], x['giftable'], x['refundable'], x['panel'], x['sortPriority'], x['banner'], x['items'])
122+
featured_items.push(featuredItem)
123+
end
124+
daily = JSON.parse(response.body)['data']['daily']
125+
for x in daily
126+
dailyItem = ShopItem.new(x['regularPrice'], x['finalPrice'], x['isBundle'], x['giftable'], x['refundable'], x['panel'], x['sortPriority'], x['banner'], x['items'])
127+
daily_items.push(dailyItem)
128+
end
129+
br_store = [featured_items, daily_items]
130+
br_store
131+
end
132+
end

0 commit comments

Comments
 (0)