Compare commits
2 Commits
a6c656b4ab
...
d68b42e864
Author | SHA1 | Date |
---|---|---|
Arthur POULET | d68b42e864 | |
Arthur POULET | 4aaa60bc01 |
1
.env
1
.env
|
@ -1 +0,0 @@
|
|||
SPACETRADERS_TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGlmaWVyIjoiU0NFUFRJUVVFIiwidmVyc2lvbiI6InYyLjEuNSIsInJlc2V0X2RhdGUiOiIyMDI0LTAxLTI4IiwiaWF0IjoxNzA2NzgyODI2LCJzdWIiOiJhZ2VudC10b2tlbiJ9.LuCMUNdd3twgh34nNFc4DIXm0BNjtvV6ptFO053ipczAwGxOp_TiGkTGdXZIdFJiS_BcBvWTwsp-cyJUqX_2nCFcylQTJgIxL0HirKlyLAdLzOU2NcQQSc9iuwU7tkAxDSEv1kMH_K96xkVgIerClmeudFTccYGx7pU4nsaieKaGvs5hHx06gCJ4MNOG56Pt6J5uR72Y3FTn0O-naqWY7OcudLYb0x4fCn6HjpkMm1mivkhPclyc1YEMmqIU4Wmhk8-lmAVFdpYFloJ60wrl_OZGuWHs7pzov4csgCe6htlZDW-7PG2ymjbgUL5KyzocDxV0YWmACe-mjQ2nk83fMw
|
|
@ -0,0 +1 @@
|
|||
.env
|
22
Gemfile
22
Gemfile
|
@ -1,15 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
source "https://rubygems.org"
|
||||
gem "pry", "~> 0.14.1", :group => :develop
|
||||
gem "rake", "~> 13.0", :group => :develop
|
||||
gem "minitest", "~> 5.16", :group => :develop
|
||||
gem "dotenv", "~> 2.8"
|
||||
gem "activesupport", "~> 7.1"
|
||||
gem "colorize", "~> 1.1"
|
||||
source 'https://rubygems.org'
|
||||
gem 'activesupport', '~> 7.1'
|
||||
gem 'colorize', '~> 1.1'
|
||||
gem 'dotenv', '~> 2.8'
|
||||
gem 'minitest', '~> 5.16', group: :develop
|
||||
gem 'pry', '~> 0.14.1', group: :develop
|
||||
gem 'rake', '~> 13.0', group: :develop
|
||||
|
||||
gem "m", "~> 1.6", :group => :test
|
||||
gem 'm', '~> 1.6', group: :test
|
||||
|
||||
gem "sequel", "~> 5.63"
|
||||
gem "sqlite3", "~> 1.5"
|
||||
gem "pg", "~> 1.4"
|
||||
gem 'pg', '~> 1.4'
|
||||
gem 'sequel', '~> 5.63'
|
||||
gem 'sqlite3', '~> 1.5'
|
||||
|
|
24
Rakefile
24
Rakefile
|
@ -1,6 +1,6 @@
|
|||
require "rake"
|
||||
require "minitest/test_task"
|
||||
require "dotenv"
|
||||
require 'rake'
|
||||
require 'minitest/test_task'
|
||||
require 'dotenv'
|
||||
|
||||
Dotenv.load!
|
||||
|
||||
|
@ -8,25 +8,25 @@ Minitest::TestTask.create(:test) do |t|
|
|||
# t.libs << "test"
|
||||
# t.libs << "lib"
|
||||
t.warning = false
|
||||
t.test_globs = ["test/**/test_*.rb"]
|
||||
t.test_globs = ['test/**/test_*.rb']
|
||||
end
|
||||
|
||||
namespace "db" do
|
||||
desc "Migrate the database to the lasted schema"
|
||||
task "migrate" do
|
||||
namespace 'db' do
|
||||
desc 'Migrate the database to the lasted schema'
|
||||
task 'migrate' do
|
||||
exec("bundle exec sequel -E -m db/migrations #{ENV['DB_URL']}")
|
||||
end
|
||||
|
||||
desc "Reset all tables, schema, data"
|
||||
task "reset" do
|
||||
desc 'Reset all tables, schema, data'
|
||||
task 'reset' do
|
||||
exec(%[sequel "postgres://postgres@localhost" -c "puts DB.run('DROP DATABASE IF EXISTS spacetraders_debug'); puts DB.run('CREATE DATABASE spacetraders_debug;')"])
|
||||
|
||||
# `rm #{ENV['DB_URL']} && bundle exec rake db:migrate`
|
||||
end
|
||||
|
||||
desc "Seed some default dataset"
|
||||
task "seed" do
|
||||
exec("ruby bin/seed")
|
||||
desc 'Seed some default dataset'
|
||||
task 'seed' do
|
||||
exec('ruby bin/seed')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
18
bin/console
18
bin/console
|
@ -1,10 +1,20 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "dotenv"
|
||||
require 'dotenv'
|
||||
Dotenv.load!
|
||||
|
||||
$LOAD_PATH << File.join(Dir.pwd, "lib")
|
||||
$LOAD_PATH << File.join(Dir.pwd, 'lib')
|
||||
|
||||
require 'pry'
|
||||
require 'space_traders'
|
||||
|
||||
c = SpaceTraders::Client.load_from_env
|
||||
pp c.contract
|
||||
contract = c.contract.my_contracts { _1 }.first
|
||||
|
||||
require "models/contract"
|
||||
|
||||
pp SpaceTraders::Contract.new(**contract)
|
||||
|
||||
require "pry"
|
||||
require "space_traders"
|
||||
binding.pry
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
require 'json'
|
||||
|
||||
class SpaceTraders::Client
|
||||
DEFAULT_BASE_URL = ENV['SPACETRADERS_DEFAULT_BASE_URL'] || 'https://api.spacetraders.io/v2'
|
||||
|
||||
def initialize(token:, base_url: DEFAULT_BASE_URL)
|
||||
raise 'Invalid Nil token' if token.nil?
|
||||
|
||||
@token = token
|
||||
@base_url = base_url
|
||||
end
|
||||
|
||||
def self.register(name:, faction: 'COSMIC', base_url: DEFAULT_BASE_URL)
|
||||
res = Net::HTTP.post(
|
||||
URI("#{base_url}/register"),
|
||||
{ symbol: name, faction: faction }.to_json,
|
||||
'Content-Type' => 'application/json'
|
||||
)
|
||||
token = JSON.parse(res.body).dig('data', 'token')
|
||||
new(token: token, base_url: base_url)
|
||||
end
|
||||
|
||||
def self.load_from_env
|
||||
new(token: ENV['SPACETRADERS_TOKEN'], base_url: DEFAULT_BASE_URL)
|
||||
end
|
||||
|
||||
def post(endpoint:, body:)
|
||||
res = Net::HTTP.post(
|
||||
URI("#{base_url}/#{endpoint}"),
|
||||
body.to_json,
|
||||
'Content-Type' => 'application/json'
|
||||
)
|
||||
if block_given?
|
||||
yield JSON.parse(res.body), res
|
||||
else
|
||||
res
|
||||
end
|
||||
end
|
||||
|
||||
def get(endpoint:)
|
||||
res = Net::HTTP.get_response(
|
||||
URI("#{@base_url}/#{endpoint}"),
|
||||
'Authorization' => "Bearer #{@token}",
|
||||
'Content-Type' => 'application/json'
|
||||
)
|
||||
if block_given?
|
||||
yield JSON.parse(res.body)["data"], res
|
||||
else
|
||||
res
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'endpoint'
|
||||
|
||||
class SpaceTraders::Client
|
||||
def self.add_endpoint(name)
|
||||
define_method(name.to_s.downcase) do
|
||||
variable_name = "@memoize_#{name}"
|
||||
variable = instance_variable_get(variable_name)
|
||||
if variable.nil?
|
||||
endpoint = SpaceTraders::Endpoint::const_get(name.to_s.camelcase).new(client: self)
|
||||
instance_variable_set(variable_name, endpoint)
|
||||
endpoint
|
||||
else
|
||||
variable
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.add_endpoints(*names)
|
||||
names.each { add_endpoint(_1) }
|
||||
end
|
||||
|
||||
add_endpoints :agent, :contract
|
||||
end
|
|
@ -1,62 +0,0 @@
|
|||
require 'net/http'
|
||||
require 'uri'
|
||||
require "json"
|
||||
|
||||
class SpaceTraders::Client
|
||||
DEFAULT_BASE_URL = ENV["SPACETRADERS_DEFAULT_BASE_URL"] || "https://api.spacetraders.io/v2"
|
||||
|
||||
def initialize(token:, base_url: DEFAULT_BASE_URL)
|
||||
raise "Invalid Nil token" if token.nil?
|
||||
|
||||
@token = token
|
||||
@base_url = base_url
|
||||
end
|
||||
|
||||
def self.register(name:, faction: "COSMIC", base_url: DEFAULT_BASE_URL)
|
||||
res = Net::HTTP.post(
|
||||
URI("#{base_url}/register"),
|
||||
{ symbol: name, faction: faction }.to_json,
|
||||
"Content-Type" => "application/json",
|
||||
)
|
||||
token = JSON.parse(res.body).dig("data", "token")
|
||||
new(token: token, base_url: base_url)
|
||||
end
|
||||
|
||||
def self.load_from_env
|
||||
new(token: ENV["SPACETRADERS_TOKEN"], base_url: DEFAULT_BASE_URL)
|
||||
end
|
||||
|
||||
def post(endpoint:, body:)
|
||||
res = Net::HTTP.post(
|
||||
URI("#{base_url}/#{endpoint}"),
|
||||
body.to_json,
|
||||
"Content-Type" => "application/json",
|
||||
)
|
||||
if block_given?
|
||||
yield JSON.parse(res.body), res
|
||||
else
|
||||
res
|
||||
end
|
||||
end
|
||||
|
||||
def get(endpoint:)
|
||||
res = Net::HTTP.get_response(
|
||||
URI("#{@base_url}/#{endpoint}"),
|
||||
"Authorization" => "Bearer #{@token}",
|
||||
"Content-Type" => "application/json",
|
||||
)
|
||||
if block_given?
|
||||
yield JSON.parse(res.body), res
|
||||
else
|
||||
res
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require "endpoint"
|
||||
|
||||
class SpaceTraders::Client
|
||||
def agent
|
||||
@agent ||= SpaceTraders::Endpoint::Agent.new(client: self)
|
||||
end
|
||||
end
|
|
@ -1,4 +1,6 @@
|
|||
require "active_support/all"
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'active_support/all'
|
||||
|
||||
class SpaceTraders::Endpoint
|
||||
attr_reader :client
|
||||
|
@ -12,24 +14,27 @@ class SpaceTraders::Endpoint
|
|||
|
||||
def self.define_endpoint(method_name, get: nil, post: nil)
|
||||
if get
|
||||
define_method(method_name) do |&block|
|
||||
define_method(method_name) do |params={}, &block|
|
||||
endpoint = params.reduce(get) { _1.gsub(/:#{_2[0]}/, _2[1]) }
|
||||
pp get
|
||||
pp endpoint
|
||||
if block
|
||||
get(endpoint: get, &block)
|
||||
get(endpoint: endpoint, &block)
|
||||
else
|
||||
get(endpoint: get)
|
||||
get(endpoint: endpoint)
|
||||
# method get from client(endpoint url get from params)
|
||||
end
|
||||
end
|
||||
elsif post
|
||||
define_method(method_name) do |body, &block|
|
||||
define_method(method_name) do |params={}, body, &block|
|
||||
if block
|
||||
post(endpoint: post, body: body, &block)
|
||||
post(endpoint: endpoint, body: body, &block)
|
||||
else
|
||||
post(endpoint: post, body: body)
|
||||
post(endpoint: endpoint, body: body)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Dir["lib/endpoint/*.rb"].each { load _1 }
|
||||
Dir['lib/endpoint/*.rb'].each { load _1; warn "loaded #{_1}" }
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SpaceTraders::Endpoint::Agent < SpaceTraders::Endpoint
|
||||
define_endpoint :my_agent, get: "my/agent"
|
||||
define_endpoint :agents, get: "agents"
|
||||
define_endpoint :agents, get: "agent/:id" # TODO: fix the framework to handle this
|
||||
define_endpoint :my_agent, get: 'my/agent'
|
||||
define_endpoint :agents, get: 'agents'
|
||||
define_endpoint :agents, get: 'agent/:id'
|
||||
end
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SpaceTraders::Endpoint::Contract < SpaceTraders::Endpoint
|
||||
define_endpoint :my_contracts, get: 'my/contracts'
|
||||
define_endpoint :my_contract_accept, get: 'my/contract/:id/accept'
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
class SpaceTraders::Contract < Struct.new(:id, :factionSymbol, :type, :terms, :accepted, :fulfilled, :expiration, :deadlineToAccept, keyword_init: true)
|
||||
end
|
|
@ -1,4 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module SpaceTraders
|
||||
end
|
||||
|
||||
Dir["lib/*.rb"].filter { _1 != "lib/space_traders.rb" }.each { |f| load f; puts "loaded #{f}" }
|
||||
Dir['lib/*.rb'].filter { _1 != 'lib/space_traders.rb' }.each do |f|
|
||||
load f
|
||||
warn "loaded #{f}"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue