Class: BlockstreamSatellite::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/blockstream_satellite/order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Order

Returns a new instance of Order.



12
13
14
# File 'lib/blockstream_satellite/order.rb', line 12

def initialize(attributes)
  self.attributes = attributes.with_indifferent_access
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



10
11
12
# File 'lib/blockstream_satellite/order.rb', line 10

def attributes
  @attributes
end

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/blockstream_satellite/order.rb', line 10

def file
  @file
end

Class Method Details

.create(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/blockstream_satellite/order.rb', line 27

def self.create(options)
  if path = options.delete(:path)
    options[:file] = UploadIO.new(options[:file], 'text/plain') # TODO: get rid of this content type here
    options[:file] = File.open(path)
  end
  options[:bid] ||= (options[:file] || options[:message]).size * 51 # default price
  response = BlockstreamSatellite.client.post('order', options)
  if response.success?
    Order.new(response.body).tap { |o| o.file = options[:file] }
  else
    response
  end
end

.get(order) ⇒ Object



41
42
43
44
45
# File 'lib/blockstream_satellite/order.rb', line 41

def self.get(order)
  Order.new(order).tap do |o|
    o.refresh
  end
end

Instance Method Details

#bump(bid_increase) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/blockstream_satellite/order.rb', line 65

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

#payObject



56
57
58
59
60
61
62
63
# File 'lib/blockstream_satellite/order.rb', line 56

def pay
  response = BlockstreamSatellite.client.pay(self.payreq)
  if response.payment_error == ''
    self.refresh
  else
    response
  end
end

#payreqObject



23
24
25
# File 'lib/blockstream_satellite/order.rb', line 23

def payreq
  lightning_invoice['payreq']
end

#refreshObject



47
48
49
50
51
52
53
54
# File 'lib/blockstream_satellite/order.rb', line 47

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