Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix - Changing from singleton model to returning query object (which is lazy eval'ed) #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/helloblock/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Configuration


def network
@network ||= :testnet
@network ||= :mainnet
end

def version
Expand Down
2 changes: 1 addition & 1 deletion lib/helloblock/http/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module HelloBlock
module Connection
def connection
@connection ||= Faraday.new(base_url, connection_options) do |connection|
@connection = Faraday.new(base_url, connection_options) do |connection|
connection.request :json
connection.response :json
connection.use FaradayMiddleware::Rashify
Expand Down
26 changes: 16 additions & 10 deletions lib/helloblock/resources/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
require 'helloblock/api_interface/endpoints'

module HelloBlock
class Address
extend HelloBlock::Query
include HelloBlock::Endpoints

def self.unspents
query[:path] += ENDPOINTS[:unspents]
query[:path].squeeze!('/')
self
end
end
class Address < HelloBlock::Query

class << self
def unspents
query = self.new
query.unspents
end
end

def unspents
self.query[:path] += ENDPOINTS[:unspents]
self.query[:path].squeeze!('/')
self
end

end
end
3 changes: 1 addition & 2 deletions lib/helloblock/resources/block.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'helloblock/resources/query'

module HelloBlock
class Block
extend HelloBlock::Query
class Block < HelloBlock::Query
end
end
12 changes: 5 additions & 7 deletions lib/helloblock/resources/faucet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
require 'helloblock/api_interface/api_parameters'

module HelloBlock
class Faucet
extend HelloBlock::Query
include HelloBlock::Endpoints
include HelloBlock::APIParameters
class Faucet < HelloBlock::Query

def self.withdraw(conditions)
where(conditions.merge({post: true}))
query[:path] = ENDPOINTS[:withdraw]
self
faucetQuery = self.new
faucetQuery.where(conditions.merge({post: true}))
faucetQuery.query[:path] = ENDPOINTS[:withdraw]
faucetQuery
end
end
end
63 changes: 47 additions & 16 deletions lib/helloblock/resources/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,52 @@
require 'helloblock/api_interface/api_parameters'

module HelloBlock
module Query
class Query
attr_accessor :query, :executed, :result
include HelloBlock::Endpoints
include HelloBlock::APIParameters
extend HelloBlock::APIParameters # Some class methods requrie this

def query
@query ||= default_query
def initialize
@query = { path: ENDPOINTS[parent_class], params: {} }
@executed = false
@result = {}
end

def default_query
{ path: ENDPOINTS[parent_class], params: {} }
class << self
def parent_class
self.to_s.split('::').last.downcase.to_sym
end

def find(id)
query = self.new
query.find(id)
end

def where(conditions)
query = self.new
query.where(conditions)
end

def limit(limit)
query = self.new
query.limit(limit)
end

def offset(number)
query = self.new
query.offset(number)
end

alias_method :last, :limit
end

def parent_class
self.to_s.split('::').last.downcase.to_sym
self.class.to_s.split('::').last.downcase.to_sym
end

def find(id)
query[:path] += id
self.query[:path] += id
self
end

Expand All @@ -34,23 +62,26 @@ def where(conditions)
end

def limit(limit)
query[:path] = ENDPOINTS[parent_class] + ENDPOINTS[:latest]
query[:params][:limit] = limit
self.query[:path] = ENDPOINTS[parent_class] + ENDPOINTS[:latest]
self.query[:params][:limit] = limit
self
end

alias_method :last, :limit

def offset(number)
query[:params][:offset] = number
self.query[:params][:offset] = number
self
end

def to_hash
(query_copy = query.clone) and (@query = default_query)
method = query_copy[:params][:post] ? :post : :get
query_copy[:params].delete(:post)
HelloBlock.send(method, query_copy[:path], query_copy[:params])
if self.executed == true
return self.result
end

method = self.query[:params][:post] ? :post : :get
self.executed = true
self.result = HelloBlock.send(method, self.query[:path], self.query[:params])
end

def [](attribute)
Expand All @@ -68,8 +99,8 @@ def inspect
# exceptions: querying transactions with addresses actually hits
# /addresses/transactions endpoint
def determine_parent_resource
if query[:path] == ENDPOINTS[:transaction] && query[:params][:addresses]
query[:path] = ENDPOINTS[:addresses_transactions]
if self.query[:path] == ENDPOINTS[:transaction] && self.query[:params][:addresses]
self.query[:path] = ENDPOINTS[:addresses_transactions]
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/helloblock/resources/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# this is a temporary class that exists for compatibility with bitcoin-ruby

module HelloBlock
class RPC
extend HelloBlock::Query
include HelloBlock::Endpoints
class RPC < HelloBlock::Query
end
end
17 changes: 9 additions & 8 deletions lib/helloblock/resources/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
require 'helloblock/api_interface/api_parameters'

module HelloBlock
class Transaction
extend HelloBlock::Query
include HelloBlock::APIParameters
class Transaction < HelloBlock::Query

def self.propagate(raw_tx_hex)
api_parameter = API_PARAMETERS[:propagate]
query[:params][api_parameter] = raw_tx_hex
query[:params][:post] = true
self
class << self
def propagate(raw_tx_hex)
txQuery = self.new
api_parameter = API_PARAMETERS[:propagate]
txQuery.query[:params][api_parameter] = raw_tx_hex
txQuery.query[:params][:post] = true
txQuery
end
end

# Class Method Alias
Expand Down
3 changes: 1 addition & 2 deletions lib/helloblock/resources/wallet.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'helloblock/resources/query'

module HelloBlock
class Wallet
extend HelloBlock::Query
class Wallet < HelloBlock::Query
end
end
31 changes: 14 additions & 17 deletions spec/fixture/vcr_cassettes/batch_address_unspents.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 15 additions & 16 deletions spec/fixture/vcr_cassettes/batch_addresses.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.