1
0
mirror of https://github.com/bumi/blockstream_satellite synced 2026-02-14 05:07:49 +00:00

hello space

This commit is contained in:
2019-02-08 21:13:57 +01:00
commit db7a23d827
17 changed files with 476 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
require "blockstream_satellite/version"
require 'lnrpc'
module BlockstreamSatellite
class Error < StandardError; end
autoload :Order, 'blockstream_satellite/order'
autoload :Client, 'blockstream_satellite/client'
autoload :Configuration, 'blockstream_satellite/configuration'
autoload :Response, 'blockstream_satellite/response'
API_HOST = 'https://satellite.blockstream.com'
def self.lnd_client=(value)
@lnd_client = value
end
def self.lnd_client
@lnd_client ||= Lnrpc::Client.new({})
end
def http_client=(value)
@http_client = value
end
def self.http_client
@http_client ||= Faraday.new(url: API_HOST) do |faraday|
faraday.request :multipart
faraday.request :url_encoded
faraday.response :json, :content_type => /\bjson$/
faraday.adapter Faraday.default_adapter
end
end
def self.client
@client ||= Client.new(lnd_client: self.lnd_client, http_client: self.http_client)
end
def self.info
self.client.get('/info')
end
end
BSat = BlockstreamSatellite

View File

@@ -0,0 +1,40 @@
require "faraday"
require 'faraday_middleware'
module BlockstreamSatellite
class Client
attr_accessor :http_client, :lnd_client
def initialize(options)
self.lnd_client = options[:lnd_client]
self.http_client = options[:http_client]
end
def get(path, params = nil, &block)
request(:get, path, params, &block)
end
def post(path, body = nil, &block)
puts body.inspect
request(:post, path, nil, body, &block)
end
def pay(payreq)
self.lnd_client.send_payment_sync(Lnrpc::SendRequest.new(payment_request: payreq))
end
def request(http_method, path, params=nil, body=nil)
response = http_client.send(http_method) do |req|
req.url "/api/#{path}"
req.params = params if params
req.body = body if body
yield req if block_given?
end
Response.new(response)
rescue Faraday::ClientError => error
return Response.new(error)
end
end
end

View File

@@ -0,0 +1,78 @@
require 'faraday'
require 'faraday_middleware'
require "active_support/core_ext/hash/indifferent_access"
require 'tempfile'
require 'securerandom'
module BlockstreamSatellite
class Order
attr_accessor :attributes
def initialize(attributes)
self.attributes = attributes.with_indifferent_access
end
# defining accessors
[:auth_token, :uuid, :lightning_invoice, :sha256_message_digest, :message_digest, :status, :bid, :unpaid_bid].each do |attr|
define_method(attr) do
self.attributes[attr]
end
end
def payreq
lightning_invoice['payreq']
end
def self.create(options)
if data = options.delete(:data)
file = Tempfile.new(SecureRandom.hex(5))
file.write(data)
file.rewind
options[:file] = file
elsif path = options.delete(:path)
options[:file] = File.open(path)
end
options[:bid] ||= options[:file].size * 51 # default price
options[:file] = UploadIO.new(options[:file], 'text/plain')
response = BlockstreamSatellite.client.post('order', options)
if response.success?
Order.new(response.body)
else
response
end
end
def self.get(order)
Order.new(order).tap do |o|
o.refresh
end
end
def refresh
response = BlockstreamSatellite.client.get("order/#{self.uuid}") do |req|
req.headers['X-Auth-Token'] = self.auth_token
end
if response.success?
self.attributes.merge!(response.body)
end
end
def pay
BlockstreamSatellite.client.pay(self.payreq)
self.refresh
end
def bump(bid_increase)
response = BlockstreamSatellite.client.post("order/#{self.uuid}/bump", bid_increase: bid_increase) do |req|
req.headers['X-Auth-Token'] = self.auth_token
end
if response.success?
self.attributes['lightning_invoice'] = response['lightning_invoice']
else
response
end
end
end
end

View File

@@ -0,0 +1,23 @@
require "faraday"
module BlockstreamSatellite
class Response
attr_accessor :response, :body, :error
def initialize(response)
if response.is_a?(Faraday::ClientError)
@error = response
else
@response = response
@body = response.body
end
end
def success?
self.error.nil? && !self.response.nil? && self.response.success?
end
def [](key)
self.body[key.to_s]
end
end
end

View File

@@ -0,0 +1,3 @@
module BlockstreamSatellite
VERSION = "0.1.0"
end