Class: OBarc::Session
- Inherits:
-
Object
- Object
- OBarc::Session
- Defined in:
- lib/obarc/session.rb
Constant Summary
- OPTIONS_KEYS =
protocol server_host server_port api_version username password base_url logger verify_ssl
- DEFAULT_OPTIONS =
{ protocol: 'http', server_host: 'localhost', server_port: '18469', api_version: 'v1', verify_ssl: true }
Instance Attribute Summary (collapse)
- - (Object) base_url
-
- (Object) cookies
Returns the value of attribute cookies.
- - (Object) logger
-
- (Object) password
Returns the value of attribute password.
-
- (Object) username
Returns the value of attribute username.
-
- (Object) verify_ssl
Returns the value of attribute verify_ssl.
Instance Method Summary (collapse)
-
- (Hash) add_social_account(social_account = {})
Adds a social account to the user profile data of the user.
-
- (Hash) broadcast(message = {})
Sends some kind of “Twitter-like” message to all nodes that are following you.
-
- (Hash) btc_price
Undocumented.
-
- (Hash) cases
Undocumented.
-
- (Hash) chat_conversations
Retreives a list of outstandng conversations.
-
- (Hash) chat_messages(chat_messages = {})
Retrieves all chat message received from other nodes.
-
- (Hash) check_for_payment(check_for_payment)
Sends a Twitter-like message to all nodes that are following you.
-
- (Hash) close_dispute(close_dispute = nil)
Undocumented.
-
- (Hash) complete_order(complete_order = {})
Sends a message with a partially signed transaction releasing funds from escrow to the Vendor as well as review data.
-
- (Hash) confirm_order(confirm_order = {})
Sends the order confirmation and shipping information to the Buyer.
-
- (Hash) connected_peers
Undocumented.
-
- (Hash) contracts(contracts = {})
Retrieves the listings created by either your node or a target node.
-
- (Hash) create_contract(contract = {})
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.
-
- (Hash) delete_chat_conversation(delete_chat_conversation)
Undocumented.
-
- (Hash) delete_contract(contract = {})
Undocumented.
-
- (Hash) delete_social_account(social_account = {})
Undocumented.
-
- (Hash) dispute_contract(dispute_contract = nil)
Undocumented.
-
- (Hash) follow(follow)
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).
-
- (Hash) followers(followers = nil)
Returns the followers of the user’s node, or that of a target node.
-
- (Hash) following(following = nil)
Returns the following of the user’s node, or that of a target node.
-
- (Object) image(image)
Returns the image for the hash specified.
-
- (Session) initialize(options = {})
constructor
A new instance of Session.
-
- (Hash) listings(listings = nil)
Returns the listings of the user’s node, or that of a target node.
-
- (Hash) make_moderator
Sets your node as a Moderator, which is discoverable on the network.
-
- (Hash) mark_chat_message_as_read(mark_chat_message_as_read = nil)
Marks all chat messages with a specific node as read in the database.
-
- (Hash) mark_discussion_as_read(mark_discussion_as_read = nil)
Undocumented.
-
- (Hash) mark_notification_as_read(notification = nil)
Marks a notification as read in the database.
-
- (Hash) notifications
Retreive a history of all notifications your node has received.
-
- (Hash) order(order)
Undocumented.
-
- (Hash) order_messages(order_messages)
Undocumented.
-
- (Boolean) ping
Check if there's a valid session.
-
- (Hash) profile(profile = nil)
Returns the profile data of the user’s node, or that of a target node.
-
- (Hash) purchase_contract(purchase_contract = {})
Purchases a contract by sending the purchase into the Vendor.
-
- (Hash) purchases
Retrieves any purchases made by the node.
-
- (Hash) query_listings(options = {})
Finds the listings of the user’s node, or that of a target node.
-
- (Hash) ratings(ratings)
Undocumented.
-
- (Hash) refund(refund = nil)
Undocumented.
-
- (Hash) release_funds(release_funds = nil)
Undocumented.
-
- (Hash) routing_table
Undocumented.
-
- (Hash) sales
Retrieves any sales made by the node.
-
- (Hash) settings
Returns the settings of your node.
-
- (Object) shutdown!
API call to cleanly disconnect from connected nodes and shutsdown the OpenBazaar server component.
-
- (Hash) social_accounts(profile = nil)
Returns just the profile's social accounts of the user’s node, or that of a target node.
-
- (Hash) unfollow(unfollow)
Stop following a target node, will cease to receive notifications and sharing metadata.
-
- (Hash) unmake_moderator
Removes the node as a Moderator and is no longer discoverable on the network as a Moderator.
-
- (Hash) update_profile(profile = {})
Add data related to the node's profile into the database, which will be visible to other nodes.
-
- (Hash) update_settings(settings = {})
Changes the settings of the node and pushes them to the database.
- - (Object) upload_contract_images_with(symbol, contract = {})
-
- (Hash) upload_image(image = {})
Saves the image in the file system and a pointer to it in the db.
Constructor Details
- (Session) initialize(options = {})
Returns a new instance of Session
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/obarc/session.rb', line 21 def initialize( = {}) OPTIONS_KEYS.each do |k| value = if .key?(k) [k] else DEFAULT_OPTIONS[k] end instance_variable_set "@#{k}".to_sym, value end @base_url ||= base_url @logger ||= logger @cookies ||= Api::post_login(self). end |
Instance Attribute Details
- (Object) base_url
37 38 39 |
# File 'lib/obarc/session.rb', line 37 def base_url @base_url ||= "#{@protocol}://#{@server_host}:#{@server_port}/api/#{@api_version}" end |
- (Object) cookies
Returns the value of attribute cookies
10 11 12 |
# File 'lib/obarc/session.rb', line 10 def @cookies end |
- (Object) logger
41 42 43 |
# File 'lib/obarc/session.rb', line 41 def logger @logger ||= Logging.logger(STDOUT) end |
- (Object) password
Returns the value of attribute password
10 11 12 |
# File 'lib/obarc/session.rb', line 10 def password @password end |
- (Object) username
Returns the value of attribute username
10 11 12 |
# File 'lib/obarc/session.rb', line 10 def username @username end |
- (Object) verify_ssl
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
- (Hash) add_social_account(social_account = {})
Adds a social account to the user profile data of the user.
191 |
# File 'lib/obarc/session.rb', line 191 def ( = {}); JSON[Api::(, self)]; end |
- (Hash) broadcast(message = {})
Sends some kind of “Twitter-like” message to all nodes that are following you. This call can take a while to complete.
442 |
# File 'lib/obarc/session.rb', line 442 def broadcast( = {}); JSON[Api::post_broadcast(, self)]; end |
- (Hash) btc_price
Undocumented
447 |
# File 'lib/obarc/session.rb', line 447 def btc_price; JSON[Api::get_btc_price(self)]; end |
- (Hash) cases
Undocumented
414 |
# File 'lib/obarc/session.rb', line 414 def cases; JSON[Api::get_cases(self)]; end |
- (Hash) chat_conversations
Retreives a list of outstandng conversations.
387 |
# File 'lib/obarc/session.rb', line 387 def chat_conversations; JSON[Api::get_chat_conversations(self)]; end |
- (Hash) chat_messages(chat_messages = {})
Retrieves all chat message received from other nodes.
381 |
# File 'lib/obarc/session.rb', line 381 def ( = {}); JSON[Api::(, self)]; end |
- (Hash) check_for_payment(check_for_payment)
Sends a Twitter-like message to all nodes that are following you.
470 |
# File 'lib/obarc/session.rb', line 470 def check_for_payment(check_for_payment); JSON[Api::post_check_for_payment(check_for_payment, self)]; end |
- (Hash) close_dispute(close_dispute = nil)
Undocumented
489 |
# File 'lib/obarc/session.rb', line 489 def close_dispute(close_dispute = nil); JSON[Api::post_close_dispute(close_dispute, self)]; end |
- (Hash) complete_order(complete_order = {})
Sends a message with a partially signed transaction releasing funds from escrow to the Vendor as well as review data.
342 |
# File 'lib/obarc/session.rb', line 342 def complete_order(complete_order = {}); JSON[Api::post_complete_order(complete_order, self)]; end |
- (Hash) confirm_order(confirm_order = {})
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.
322 |
# File 'lib/obarc/session.rb', line 322 def confirm_order(confirm_order = {}); JSON[Api::post_confirm_order(confirm_order, self)]; end |
- (Hash) connected_peers
Undocumented
360 |
# File 'lib/obarc/session.rb', line 360 def connected_peers; JSON[Api::get_connected_peers(self)]; end |
- (Hash) contracts(contracts = {})
Retrieves the listings created by either your node or a target node.
205 |
# File 'lib/obarc/session.rb', line 205 def contracts(contracts = {}); JSON[Api::get_contracts(contracts, self)]; end |
- (Hash) create_contract(contract = {})
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.
260 261 262 263 264 265 266 267 268 269 |
# File 'lib/obarc/session.rb', line 260 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 |
- (Hash) delete_chat_conversation(delete_chat_conversation)
Undocumented
392 |
# File 'lib/obarc/session.rb', line 392 def delete_chat_conversation(delete_chat_conversation); JSON[Api::delete_chat_conversation(delete_chat_conversation, self)]; end |
- (Hash) delete_contract(contract = {})
Undocumented.
286 |
# File 'lib/obarc/session.rb', line 286 def delete_contract(contract = {}); JSON[Api::delete_contract(contract, self)]; end |
- (Hash) delete_social_account(social_account = {})
Undocumented.
196 |
# File 'lib/obarc/session.rb', line 196 def ( = {}); JSON[Api::(, self)]; end |
- (Hash) dispute_contract(dispute_contract = nil)
Undocumented
477 |
# File 'lib/obarc/session.rb', line 477 def dispute_contract(dispute_contract = nil); JSON[Api::post_dispute_contract(dispute_contract, self)]; end |
- (Hash) follow(follow)
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).
168 |
# File 'lib/obarc/session.rb', line 168 def follow(follow); JSON[Api::post_follow(follow, self)]; end |
- (Hash) followers(followers = nil)
Returns the followers of the user’s node, or that of a target node.
151 |
# File 'lib/obarc/session.rb', line 151 def followers(followers = nil); JSON[Api::get_followers(followers, self)]; end |
- (Hash) following(following = nil)
Returns the following of the user’s node, or that of a target node.
159 |
# File 'lib/obarc/session.rb', line 159 def following(following = nil); JSON[Api::get_following(following, self)]; end |
- (Object) image(image)
Returns the image for the hash specified.
60 |
# File 'lib/obarc/session.rb', line 60 def image(image); Api::get_image(image, self); end |
- (Hash) listings(listings = nil)
Returns the listings of the user’s node, or that of a target node.
94 |
# File 'lib/obarc/session.rb', line 94 def listings(listings = nil); JSON[Api::get_listings(listings, self)]; end |
- (Hash) make_moderator
Sets your node as a Moderator, which is discoverable on the network.
292 |
# File 'lib/obarc/session.rb', line 292 def make_moderator; JSON[Api::post_make_moderator(self)]; end |
- (Hash) mark_chat_message_as_read(mark_chat_message_as_read = nil)
Marks all chat messages with a specific node as read in the database.
461 |
# File 'lib/obarc/session.rb', line 461 def ( = nil); JSON[Api::(, self)]; end |
- (Hash) mark_discussion_as_read(mark_discussion_as_read = nil)
Undocumented
504 |
# File 'lib/obarc/session.rb', line 504 def mark_discussion_as_read(mark_discussion_as_read = nil); JSON[Api::post_mark_discussion_as_read(mark_discussion_as_read, self)]; end |
- (Hash) mark_notification_as_read(notification = nil)
Marks a notification as read in the database.
433 |
# File 'lib/obarc/session.rb', line 433 def mark_notification_as_read(notification = nil); JSON[Api::post_mark_notification_as_read(notification, self)]; end |
- (Hash) notifications
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
371 |
# File 'lib/obarc/session.rb', line 371 def notifications; JSON[Api::get_notifications(self)]; end |
- (Hash) order(order)
Undocumented
409 |
# File 'lib/obarc/session.rb', line 409 def order(order); JSON[Api::get_order(order, self)]; end |
- (Hash) order_messages(order_messages)
Undocumented
419 |
# File 'lib/obarc/session.rb', line 419 def (); JSON[Api::(, self)]; end |
- (Boolean) ping
Check if there's a valid session.
48 49 50 51 52 53 54 |
# File 'lib/obarc/session.rb', line 48 def ping return false if !(json = Api::ping(self)) !!JSON[json]['num_peers'] rescue RestClient::Unauthorized => e logger.warn(e) false end |
- (Hash) profile(profile = nil)
Returns the profile data of the user’s node, or that of a target node.
69 |
# File 'lib/obarc/session.rb', line 69 def profile(profile = nil); JSON[Api::get_profile(profile, self)]; end |
- (Hash) purchase_contract(purchase_contract = {})
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.
313 |
# File 'lib/obarc/session.rb', line 313 def purchase_contract(purchase_contract = {}); JSON[Api::post_purchase_contract(purchase_contract, self)]; end |
- (Hash) purchases
Retrieves any purchases made by the node.
404 |
# File 'lib/obarc/session.rb', line 404 def purchases; JSON[Api::get_purchases(self)]; end |
- (Hash) query_listings(options = {})
Finds the listings of the user’s node, or that of a target node.
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 138 139 140 141 142 143 |
# File 'lib/obarc/session.rb', line 103 def query_listings( = {}) pattern = .delete(:pattern) all_listings = JSON[Api::get_listings(, 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(.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 |
- (Hash) ratings(ratings)
Undocumented
424 |
# File 'lib/obarc/session.rb', line 424 def (); JSON[Api::(, self)]; end |
- (Hash) refund(refund = nil)
Undocumented
499 |
# File 'lib/obarc/session.rb', line 499 def refund(refund = nil); JSON[Api::post_refund(refund, self)]; end |
- (Hash) release_funds(release_funds = nil)
Undocumented
494 |
# File 'lib/obarc/session.rb', line 494 def release_funds(release_funds = nil); JSON[Api::post_release_funds(release_funds, self)]; end |
- (Hash) routing_table
Undocumented
452 |
# File 'lib/obarc/session.rb', line 452 def routing_table; JSON[Api::get_routing_table(self)]; end |
- (Hash) sales
Retrieves any sales made by the node.
398 |
# File 'lib/obarc/session.rb', line 398 def sales; JSON[Api::get_sales(self)]; end |
- (Hash) settings
Returns the settings of your node.
355 |
# File 'lib/obarc/session.rb', line 355 def settings; JSON[Api::get_settings(self)]; end |
- (Object) shutdown!
API call to cleanly disconnect from connected nodes and shutsdown the OpenBazaar server component.
510 511 512 |
# File 'lib/obarc/session.rb', line 510 def shutdown! Api::get_shutdown(self) end |
- (Hash) social_accounts(profile = nil)
Returns just the profile's social accounts of the user’s node, or that of a target node.
78 79 80 81 82 83 84 85 86 |
# File 'lib/obarc/session.rb', line 78 def (profile = nil) result = JSON[Api::get_profile(profile, self)] if !!result && !!result['profile'] && !!result['profile']['social_accounts'] result['profile']['social_accounts'] else [] end end |
- (Hash) unfollow(unfollow)
Stop following a target node, will cease to receive notifications and sharing metadata.
176 |
# File 'lib/obarc/session.rb', line 176 def unfollow(unfollow); JSON[Api::post_unfollow(unfollow, self)]; end |
- (Hash) unmake_moderator
Removes the node as a Moderator and is no longer discoverable on the network as a Moderator.
299 |
# File 'lib/obarc/session.rb', line 299 def unmake_moderator; JSON[Api::post_unmake_moderator(self)]; end |
- (Hash) update_profile(profile = {})
Add data related to the node's profile into the database, which will be visible to other nodes.
184 |
# File 'lib/obarc/session.rb', line 184 def update_profile(profile = {}); JSON[Api::post_profile(profile, self)]; end |
- (Hash) update_settings(settings = {})
Changes the settings of the node and pushes them to the database.
349 |
# File 'lib/obarc/session.rb', line 349 def update_settings(settings = {}); JSON[Api::post_settings(settings, self)]; end |
- (Object) upload_contract_images_with(symbol, contract = {})
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/obarc/session.rb', line 271 def upload_contract_images_with(symbol, contract = {}) contract[:images] = [contract.delete(symbol)].flatten.map do |image| response = if image.size < 2000 && image =~ URI::ABS_URI upload_image(image: open(image, 'rb')) else upload_image(image: image) end response['image_hashes'] if response['success'] end.flatten end |
- (Hash) upload_image(image = {})
Saves the image in the file system and a pointer to it in the db.
334 |
# File 'lib/obarc/session.rb', line 334 def upload_image(image = {}); JSON[Api::post_upload_image(image.merge(cookies: , base_url: base_url, verify_ssl: verify_ssl))]; end |