Class: FortniteAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/fortniteapi/api.rb

Defined Under Namespace

Classes: AES, BRCosmetic, BRNews, CreativeNews, CreatorCode, STWNews, ShopItem

Instance Method Summary collapse

Constructor Details

#initialize(apikey = nil) ⇒ FortniteAPI

Returns a new instance of FortniteAPI.



12
13
14
15
16
17
18
19
# File 'lib/fortniteapi/api.rb', line 12

def initialize(apikey=nil)
    @apikey = apikey
    @headers = {"x-api-key" => @apikey}

    if apikey == nil
        raise 'Not API key provided, this is now required. You can get one at: fortnite-api.com.'
    end
end

Instance Method Details

#all_cosmeticsObject



95
96
97
98
# File 'lib/fortniteapi/api.rb', line 95

def all_cosmetics()
    response = HTTParty.get("https://fortnite-api.com/cosmetics/br", :headers => @headers)
    JSON.parse(response.body)
end

#get_aesObject



151
152
153
154
155
156
# File 'lib/fortniteapi/api.rb', line 151

def get_aes()
    response = HTTParty.get("https://fortnite-api.com/aes", :headers => @headers)
    data = JSON.parse(response.body)['data']
    aes = AES.new(data['aes'], data['build'], x['lastUpdate'])
    aes
end

#get_br_news(language = 'en') ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/fortniteapi/api.rb', line 100

def get_br_news(language='en')
    br_news_list = []
    response = HTTParty.get("https://fortnite-api.com/news/br?language=#{language}", :headers => @headers)
    messages = JSON.parse(response.body)['data']['messages']
    for x in messages
        newsResult = BRNews.new(x['image'], x['hidden'], x['messageType'], x['type'], x['adspace'], x['spotlight'], x['title'], x['body'])
        br_news_list.push(newsResult)
    end
    br_news_list
end

#get_br_store(language = 'en') ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fortniteapi/api.rb', line 133

def get_br_store(language='en')
    featured_items = []
    daily_items = []
    response = HTTParty.get("https://fortnite-api.com/shop/br?language=#{language}", :headers => @headers)
    featured = JSON.parse(response.body)['data']['featured']
    for x in featured
        featuredItem = ShopItem.new(x['regularPrice'], x['finalPrice'], x['isBundle'], x['giftable'], x['refundable'], x['panel'], x['sortPriority'], x['banner'], x['items'])
        featured_items.push(featuredItem)
    end
    daily = JSON.parse(response.body)['data']['daily']
    for x in daily
        dailyItem = ShopItem.new(x['regularPrice'], x['finalPrice'], x['isBundle'], x['giftable'], x['refundable'], x['panel'], x['sortPriority'], x['banner'], x['items'])
        daily_items.push(dailyItem)
    end 
    br_store = [featured_items, daily_items]
    br_store
end

#get_creative_news(language = 'en') ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/fortniteapi/api.rb', line 122

def get_creative_news(language='en')
    creative_news_list = []
    response = HTTParty.get("https://fortnite-api.com/news/creative?language=#{language}", :headers => @headers)
    messages = JSON.parse(response.body)['data']['messages']
    for x in messages
        newsResult = CreativeNews.new(x['image'], x['hidden'], x['messageType'], x['type'], x['adspace'], x['spotlight'], x['title'], x['body'])
        creative_news_list.push(newsResult)
    end
    creative_news_list
end

#get_creator_code(slug) ⇒ Object



42
43
44
45
46
47
# File 'lib/fortniteapi/api.rb', line 42

def get_creator_code(slug)
    response = HTTParty.get("https://fortnite-api.com/creatorcode/search?slug=#{slug}", :headers => @headers)
    body = JSON.parse(response.body)['data']
    supportACreator = CreatorCode.new(body['id'], body['slug'], body['displayName'], body['status'], body['verified'])
    supportACreator
end

#get_stw_news(language = 'en') ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/fortniteapi/api.rb', line 111

def get_stw_news(language='en')
    stw_news_list = []
    response = HTTParty.get("https://fortnite-api.com/news/stw?language=#{language}", :headers => @headers)
    messages = JSON.parse(response.body)['data']['messages']
    for x in messages
        newsResult = STWNews.new(x['image'], x['hidden'], x['messageType'], x['type'], x['adspace'], x['spotlight'], x['title'], x['body'])
        stw_news_list.push(newsResult)
    end
    stw_news_list
end

#search_cosmetic(searchQuery, tag = 'name', language = 'en', searchLanguage = 'en') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fortniteapi/api.rb', line 21

def search_cosmetic(searchQuery, tag='name', language='en', searchLanguage='en')
    response = HTTParty.get("https://fortnite-api.com/cosmetics/br/search?#{tag}=#{searchQuery}&language=#{language}&searchLanguage=#{searchLanguage}", :headers => @headers)
    body = JSON.parse(response.body)['data']
    if response.code == 200
        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'])
        searchResult
    elsif response.code == 404
        raise "Error when trying to fetch cosmetic: #{searchQuery} - #{body['error']}"
        searchResult = BRCosmetic.new()
        searchResult
    elsif response.code == 400
        raise "Error when using parameter, it might be that it is invalid.: #{tag} - #{body['error']}"
        searchResult = BRCosmetic.new()
        searchResult
    else
        raise "An unexpected error occured. Status code: #{response.code}, Request body: #{response.body}"
        searchResult = BRCosmetic.new()
        searchResult
    end
end

#search_cosmetic_id(searchQuery, language = 'en') ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fortniteapi/api.rb', line 49

def search_cosmetic_id(searchQuery, language='en')
    response = HTTParty.get("https://fortnite-api.com/cosmetics/br/#{searchQuery}&language=#{language}", :headers => @headers)
    body = JSON.parse(response.body)['data']
    if response.code == 200
        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'])
        searchResult
    elsif response.code == 404
        raise "Error when trying to fetch cosmetic: #{searchQuery} - #{body['error']}"
        searchResult = BRCosmetic.new()
        searchResult
    elsif response.code == 400
        raise "Error when using parameter, it might be that it is invalid.: #{tag} - #{body['error']}"
        searchResult = BRCosmetic.new()
        searchResult
    else
        raise "An unexpected error occured. Status code: #{response.code}, Request body: #{response.body}"
        searchResult = BRCosmetic.new()
        searchResult
    end
end

#search_cosmetics(searchQuery, tag = 'name', language = 'en', searchLanguage = 'en') ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fortniteapi/api.rb', line 70

def search_cosmetics(searchQuery, tag='name', language='en', searchLanguage='en')
    cosmetics = []
    response = HTTParty.get("https://fortnite-api.com/cosmetics/br/search/all?#{tag}=#{searchQuery}&language=#{language}&searchLanguage=#{searchLanguage}", :headers => @headers)
    body = JSON.parse(response.body)['data']
    if response.code == 200
        for x in body
            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'])
            cosmetics.push(searchResult)
        end
        cosmetics
    elsif response.code == 404
        raise "Error when trying to fetch cosmetic: #{searchQuery} - #{body['error']}"
        searchResult = BRCosmetic.new()
        searchResult
    elsif response.code == 400
        raise "Error when using parameter, it might be that it is invalid.: #{tag} - #{body['error']}"
        searchResult = BRCosmetic.new()
        searchResult
    else
        raise "An unexpected error occured. Status code: #{response.code}, Request body: #{response.body}"
        searchResult = BRCosmetic.new()
        searchResult
    end
end