Class: OBarc::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/obarc/session.rb

Constant Summary collapse

OPTIONS_KEYS =
protocol server_host server_port api_version username
password base_url logger cookies verify_ssl
DEFAULT_OPTIONS =
{
  protocol: 'http',
  server_host: 'localhost',
  server_port: '18469',
  api_version: 'v1',
  verify_ssl: true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session


21
22
23
24
25
26
27
28
29
# File 'lib/obarc/session.rb', line 21

def initialize(options = {})
  OPTIONS_KEYS.each do |k|
    instance_variable_set "@#{k}".to_sym, options[k] || DEFAULT_OPTIONS[k]
  end
  
  @base_url ||= base_url
  @logger ||= logger
  @cookies ||= Api::(self).cookies
end

Instance Attribute Details

#base_urlObject


31
32
33
# File 'lib/obarc/session.rb', line 31

def base_url
  @base_url ||= "#{@protocol}://#{@server_host}:#{@server_port}/api/#{@api_version}"
end

#cookiesObject

Returns the value of attribute cookies


10
11
12
# File 'lib/obarc/session.rb', line 10

def cookies
  @cookies
end

#loggerObject


35
36
37
# File 'lib/obarc/session.rb', line 35

def logger
  @logger ||= Logging.logger(STDOUT)
end

#passwordObject

Returns the value of attribute password


10
11
12
# File 'lib/obarc/session.rb', line 10

def password
  @password
end

#usernameObject

Returns the value of attribute username


10
11
12
# File 'lib/obarc/session.rb', line 10

def username
  @username
end

#verify_sslObject

Returns the value of attribute verify_ssl


10
11
12
# File 'lib/obarc/session.rb', line 10

def verify_ssl
  @verify_ssl
end

Instance Method Details

#add_social_account(social_account = {}) ⇒ Hash

Adds a social account to the user profile data of the user.


185
# File 'lib/obarc/session.rb', line 185

def ( = {}); JSON[Api::post_social_accounts(, self)]; end

#broadcast(message = {}) ⇒ Hash

Sends some kind of “Twitter-like” message to all nodes that are following you. This call can take a while to complete.


436
# File 'lib/obarc/session.rb', line 436

def broadcast(message = {}); JSON[Api::post_broadcast(message, self)]; end

#btc_priceHash

Undocumented


441
# File 'lib/obarc/session.rb', line 441

def btc_price; JSON[Api::get_btc_price(self)]; end

#casesHash

Undocumented


408
# File 'lib/obarc/session.rb', line 408

def cases; JSON[Api::get_cases(self)]; end

#chat_conversationsHash

Retreives a list of outstandng conversations.


381
# File 'lib/obarc/session.rb', line 381

def chat_conversations; JSON[Api::get_chat_conversations(self)]; end

#chat_messages(chat_messages = {}) ⇒ Hash

Retrieves all chat message received from other nodes.


375
# File 'lib/obarc/session.rb', line 375

def chat_messages(chat_messages = {}); JSON[Api::get_chat_messages(chat_messages, self)]; end

#check_for_payment(check_for_payment) ⇒ Hash

Sends a Twitter-like message to all nodes that are following you.


464
# File 'lib/obarc/session.rb', line 464

def check_for_payment(check_for_payment); JSON[Api::post_check_for_payment(check_for_payment, self)]; end

#close_dispute(close_dispute = nil) ⇒ Hash

Undocumented


483
# File 'lib/obarc/session.rb', line 483

def close_dispute(close_dispute = nil); JSON[Api::post_close_dispute(close_dispute, self)]; end

#complete_order(complete_order = {}) ⇒ Hash

Sends a message with a partially signed transaction releasing funds from escrow to the Vendor as well as review data.


336
# File 'lib/obarc/session.rb', line 336

def complete_order(complete_order = {}); JSON[Api::post_complete_order(complete_order, self)]; end

#confirm_order(confirm_order = {}) ⇒ Hash

Sends the order confirmation and shipping information to the Buyer. If he’s offline, it will embed this data into the dht. The API call also updates the status of the order in the database.


316
# File 'lib/obarc/session.rb', line 316

def confirm_order(confirm_order = {}); JSON[Api::post_confirm_order(confirm_order, self)]; end

#connected_peersHash

Undocumented


354
# File 'lib/obarc/session.rb', line 354

def connected_peers; JSON[Api::get_connected_peers(self)]; end

#contracts(contracts = {}) ⇒ Hash

Retrieves the listings created by either your node or a target node.


199
# File 'lib/obarc/session.rb', line 199

def contracts(contracts = {}); JSON[Api::get_contracts(contracts, self)]; end

#create_contract(contract = {}) ⇒ Hash

Creates a listing contract, which is saved to the database and local file system, as well as publish the keywords in the distributed hash table.


254
255
256
257
258
259
260
261
262
263
# File 'lib/obarc/session.rb', line 254

def create_contract(contract = {})
  # Note, passing contract_id appears to create a clone that re-uses the
  # original contract_id.
  
  %i(image_urls image_data).each do |symbol|
    upload_contract_images_with(symbol, contract) if !!contract[symbol]
  end
  
  JSON[Api::post_contract(contract, self)]
end

#delete_chat_conversation(delete_chat_conversation) ⇒ Hash

Undocumented


386
# File 'lib/obarc/session.rb', line 386

def delete_chat_conversation(delete_chat_conversation); JSON[Api::delete_chat_conversation(delete_chat_conversation, self)]; end

#delete_contract(contract = {}) ⇒ Hash

Undocumented.


280
# File 'lib/obarc/session.rb', line 280

def delete_contract(contract = {}); JSON[Api::delete_contract(contract, self)]; end

#delete_social_account(social_account = {}) ⇒ Hash

Undocumented.


190
# File 'lib/obarc/session.rb', line 190

def ( = {}); JSON[Api::delete_social_accounts(, self)]; end

#dispute_contract(dispute_contract = nil) ⇒ Hash

Undocumented


471
# File 'lib/obarc/session.rb', line 471

def dispute_contract(dispute_contract = nil); JSON[Api::post_dispute_contract(dispute_contract, self)]; end

#follow(follow) ⇒ Hash

Follows a target node and will cause you to receive notifications from that node after certain event (e.g. new listing, broadcast messages) and share some metadata (in future).


162
# File 'lib/obarc/session.rb', line 162

def follow(follow); JSON[Api::post_follow(follow, self)]; end

#followers(followers = nil) ⇒ Hash

Returns the followers of the user’s node, or that of a target node.


145
# File 'lib/obarc/session.rb', line 145

def followers(followers = nil); JSON[Api::get_followers(followers, self)]; end

#following(following = nil) ⇒ Hash

Returns the following of the user’s node, or that of a target node.


153
# File 'lib/obarc/session.rb', line 153

def following(following = nil); JSON[Api::get_following(following, self)]; end

#image(image) ⇒ Object

Returns the image for the hash specified.


54
# File 'lib/obarc/session.rb', line 54

def image(image); Api::get_image(image, self); end

#listings(listings = nil) ⇒ Hash

Returns the listings of the user’s node, or that of a target node.


88
# File 'lib/obarc/session.rb', line 88

def listings(listings = nil); JSON[Api::get_listings(listings, self)]; end

#make_moderatorHash

Sets your node as a Moderator, which is discoverable on the network.


286
# File 'lib/obarc/session.rb', line 286

def make_moderator; JSON[Api::post_make_moderator(self)]; end

#mark_chat_message_as_read(mark_chat_message_as_read = nil) ⇒ Hash

Marks all chat messages with a specific node as read in the database.


455
# File 'lib/obarc/session.rb', line 455

def mark_chat_message_as_read(mark_chat_message_as_read = nil); JSON[Api::post_mark_chat_message_as_read(mark_chat_message_as_read, self)]; end

#mark_discussion_as_read(mark_discussion_as_read = nil) ⇒ Hash

Undocumented


498
# File 'lib/obarc/session.rb', line 498

def mark_discussion_as_read(mark_discussion_as_read = nil); JSON[Api::post_mark_discussion_as_read(mark_discussion_as_read, self)]; end

#mark_notification_as_read(notification = nil) ⇒ Hash

Marks a notification as read in the database.


427
# File 'lib/obarc/session.rb', line 427

def mark_notification_as_read(notification = nil); JSON[Api::post_mark_notification_as_read(notification, self)]; end

#notificationsHash

Retreive a history of all notifications your node has received. Notifications can be sent due to:

  • A node following you

  • Events related to a purchase or sale


365
# File 'lib/obarc/session.rb', line 365

def notifications; JSON[Api::get_notifications(self)]; end

#order(order) ⇒ Hash

Undocumented


403
# File 'lib/obarc/session.rb', line 403

def order(order); JSON[Api::get_order(order, self)]; end

#order_messages(order_messages) ⇒ Hash

Undocumented


413
# File 'lib/obarc/session.rb', line 413

def order_messages(order_messages); JSON[Api::get_order_messages(order_messages, self)]; end

#pingBoolean

Check if there's a valid session.


42
43
44
45
46
47
48
# File 'lib/obarc/session.rb', line 42

def ping
  return false if !(json = Api::ping(self))
  !!JSON[json]['num_peers']
rescue RestClient::Unauthorized => e
  logger.warn(e)
  false
end

#profile(profile = nil) ⇒ Hash

Returns the profile data of the user’s node, or that of a target node.


63
# File 'lib/obarc/session.rb', line 63

def profile(profile = nil); JSON[Api::get_profile(profile, self)]; end

#purchase_contract(purchase_contract = {}) ⇒ Hash

Purchases a contract by sending the purchase into the Vendor. The Buyer waits for a response to indicate whether the purchase is successful or not. If successful, the Buyer needs to fund the direct or multisig address.


307
# File 'lib/obarc/session.rb', line 307

def purchase_contract(purchase_contract = {}); JSON[Api::post_purchase_contract(purchase_contract, self)]; end

#purchasesHash

Retrieves any purchases made by the node.


398
# File 'lib/obarc/session.rb', line 398

def purchases; JSON[Api::get_purchases(self)]; end

#query_listings(options = {}) ⇒ Hash

Finds the listings of the user’s node, or that of a target node.


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/obarc/session.rb', line 97

def query_listings(options = {})
  pattern = options.delete(:pattern)
  all_listings = JSON[Api::get_listings(options, self)]
  listings = all_listings['listings']
  
  if !!pattern
    listings = listings.select do |l|
      [l['contract_hash'], l['category'], l['title'],
        l['price'].to_s, l['origin'], l['currency_code'],
        l['ships_to'].join].join(' ') =~ pattern
    end
  end
  
  return {'listings' => listings} if listings === all_listings
  
  start = Time.now.to_i
  
  @cache_timestamp = if (start - (@cache_timestamp ||= 0)) > 300
    @contracts_cache = {}
    start
  else
    @cache_timestamp
  end
  
  @contracts_cache ||= {}
  (all_listings['listings'] - listings).each do |listing|
    contract_hash = listing['contract_hash']
    contract = @contracts_cache[contract_hash] ||= contracts(options.merge(id: listing['contract_hash']))
    next unless !!contract
    
    l = contract['vendor_offer']['listing']
    
    if [l['metadata']['expiry'], l['item']['category'], l['item']['sku'],
      l['item']['description'], l['item']['process_time'],
      l['item']['keywords'].join].join(' ') =~ pattern
      listings << listing && next
    end
  end

  {'listings' => listings}
end

#ratings(ratings) ⇒ Hash

Undocumented


418
# File 'lib/obarc/session.rb', line 418

def ratings(ratings); JSON[Api::get_ratings(ratings, self)]; end

#refund(refund = nil) ⇒ Hash

Undocumented


493
# File 'lib/obarc/session.rb', line 493

def refund(refund = nil); JSON[Api::post_refund(refund, self)]; end

#release_funds(release_funds = nil) ⇒ Hash

Undocumented


488
# File 'lib/obarc/session.rb', line 488

def release_funds(release_funds = nil); JSON[Api::post_release_funds(release_funds, self)]; end

#routing_tableHash

Undocumented


446
# File 'lib/obarc/session.rb', line 446

def routing_table; JSON[Api::get_routing_table(self)]; end

#salesHash

Retrieves any sales made by the node.


392
# File 'lib/obarc/session.rb', line 392

def sales; JSON[Api::get_sales(self)]; end

#settingsHash

Returns the settings of your node.


349
# File 'lib/obarc/session.rb', line 349

def settings; JSON[Api::get_settings(self)]; end

#shutdown!Object

API call to cleanly disconnect from connected nodes and shutsdown the OpenBazaar server component.


504
505
506
507
508
509
510
# File 'lib/obarc/session.rb', line 504

def shutdown!
  begin
    Api::get_shutdown(self)
  rescue Errno::ECONNREFUSED => e
    logger.warn(e)
  end
end

#social_accounts(profile = nil) ⇒ Hash

Returns just the profile's social accounts of the user’s node, or that of a target node.


72
73
74
75
76
77
78
79
80
# File 'lib/obarc/session.rb', line 72

def social_accounts(profile = nil)
  result = JSON[Api::get_profile(profile, self)]
  
  if !!result && !!result['profile'] && !!result['profile']['social_accounts']
    result['profile']['social_accounts']
  else
    []
  end
end

#unfollow(unfollow) ⇒ Hash

Stop following a target node, will cease to receive notifications and sharing metadata.


170
# File 'lib/obarc/session.rb', line 170

def unfollow(unfollow); JSON[Api::post_unfollow(unfollow, self)]; end

#unmake_moderatorHash

Removes the node as a Moderator and is no longer discoverable on the network as a Moderator.


293
# File 'lib/obarc/session.rb', line 293

def unmake_moderator; JSON[Api::post_unmake_moderator(self)]; end

#update_profile(profile = {}) ⇒ Hash

Add data related to the node's profile into the database, which will be visible to other nodes.


178
# File 'lib/obarc/session.rb', line 178

def update_profile(profile = {}); JSON[Api::post_profile(profile, self)]; end

#update_settings(settings = {}) ⇒ Hash

Changes the settings of the node and pushes them to the database.


343
# File 'lib/obarc/session.rb', line 343

def update_settings(settings = {}); JSON[Api::post_settings(settings, self)]; end

#upload_contract_images_with(symbol, contract = {}) ⇒ Object


265
266
267
268
269
270
271
272
273
274
275
# File 'lib/obarc/session.rb', line 265

def upload_contract_images_with(symbol, contract = {})
  contract[:images] = [contract.delete(symbol)].flatten.map do |image|
    response = if image =~ URI::regexp
      upload_image(image: open(image, 'rb'))
    else
      upload_image(image: image )
    end
             
    response['image_hashes'] if response['success']
  end.flatten
end

#upload_image(image = {}) ⇒ Hash

Saves the image in the file system and a pointer to it in the db.


328
# File 'lib/obarc/session.rb', line 328

def upload_image(image = {}); JSON[Api::post_upload_image(image.merge(cookies: cookies, base_url: base_url, verify_ssl: verify_ssl))]; end