Skip to content

Commit

Permalink
Merge 80763a4 into 5f29fbd
Browse files Browse the repository at this point in the history
  • Loading branch information
ssshun committed Mar 4, 2018
2 parents 5f29fbd + 80763a4 commit 8185376
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/coinmarketcap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ def self.coins(limit = nil)
end
end

def self.coins_sort(limit = nil, key = 'rank', order = 'asc')
if limit.nil?
response = HTTParty.get('https://api.coinmarketcap.com/v1/ticker/')
else
response = HTTParty.get("https://api.coinmarketcap.com/v1/ticker/?limit=#{limit}")
end
if ['id', 'name', 'symbol'].include?(key)
sorted_response = response.sort{ |a, b| a[key] <=> b[key] }
elsif response[0].keys.include?(key)
sorted_response = response.sort{ |a, b| a[key].to_i <=> b[key].to_i }
else
raise ArgumentError, "wrong argument: '#{key}'"
end
if order == 'asc'
sorted_response
elsif order == 'desc'
sorted_response.reverse
else
raise ArgumentError, "wrong argument: '#{order}'"
end
end

def self.coin(id, currency = 'USD')
HTTParty.get("https://api.coinmarketcap.com/v1/ticker/#{id}/?convert=#{currency}")
end
Expand Down

0 comments on commit 8185376

Please sign in to comment.