Created
March 6, 2018 04:11
-
-
Save arinhouck/7e76d09ded62ba86c30b363e61e948cc to your computer and use it in GitHub Desktop.
Basic Stellar SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Stellar | |
| include HTTParty | |
| base_uri ENV.fetch('STELLAR_BASE_URI', 'https://horizon-testnet.stellar.org') | |
| attr_accessor :cursor | |
| def initialize(account_id, cursor) | |
| # account public key | |
| @account_id = account_id | |
| # number of records per call | |
| @limit = 1 | |
| # optional value to keep track of when we last processed a transaction | |
| @cursor = cursor | |
| end | |
| # GET call to retrieve all transactions | |
| # https://www.stellar.org/developers/horizon/reference/endpoints/transactions-for-account.html#request | |
| def transactions | |
| response = self.class.get("/accounts/#{@account_id}/transactions?limit=#{@limit}#{build_cursor}") | |
| to_object(response.body).try(:_embedded).try(:records) || [] | |
| end | |
| private | |
| # Set cursor param when retrieving transactions if exists | |
| def build_cursor | |
| @cursor.nil? ? '' : "&cursor=#{@cursor}" | |
| end | |
| # Fancy way to parse json into Ruby OpenStruct object | |
| def to_object(response_body) | |
| JSON.parse(response_body, object_class: OpenStruct) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment