Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# rcov generated
coverage
coverage.data
Expand All @@ -15,7 +16,7 @@ doc
# jeweler generated
pkg

# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
# * Create a file at ~/.gitignore
# * Include files you want ignored
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gem install blockscore
If you are using Rails, add the following to your `Gemfile`:

```ruby
gem 'blockscore', '~> 4.1.2'
gem 'blockscore', '~> 4.2.0'
```

## Getting Started
Expand Down Expand Up @@ -60,5 +60,5 @@ To see the list of calls you can make, please visit our [full Ruby API reference
The test suite uses a public BlockScore API key that was created specifically to ease the testing and contribution processes. **Please do not enter personal details for tests.** In order to run the test suite:

```shell
$ rake test
$ rspec spec
```
1 change: 1 addition & 0 deletions blockscore.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.0'
spec.add_development_dependency 'simplecov', '~> 0'
spec.add_development_dependency 'rspec', '~> 3'
spec.add_development_dependency 'rspec-its', '~> 1'
spec.add_development_dependency 'webmock', '~> 1.21'
spec.add_development_dependency 'faker', '~> 1.4'
spec.add_development_dependency 'factory_girl', '~> 4.5'
Expand Down
10 changes: 4 additions & 6 deletions lib/blockscore.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'delegate'
require 'forwardable'
require 'httparty'
require 'json'
Expand Down Expand Up @@ -26,6 +27,7 @@
require 'blockscore/watchlist_hit'

require 'blockscore/collection'
require 'blockscore/collection/member'
require 'blockscore/connection'
require 'blockscore/dispatch'
require 'blockscore/fingerprint'
Expand All @@ -34,11 +36,7 @@
require 'blockscore/version'

module BlockScore
def self.api_key=(api_key)
@api_key = api_key
end

def self.api_key
@api_key
class << self
attr_accessor :api_key
end
end
6 changes: 4 additions & 2 deletions lib/blockscore/actions/retrieve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ module Actions
# => #<BlockScore::Person:0x007fe39c424410>
module Retrieve
module ClassMethods
def retrieve(id)
get("#{endpoint}/#{id}", {})
def retrieve(id, options = {})
fail ArgumentError if id.empty?
req = ->() { get("#{endpoint}/#{id}", options) }
new(id: id, &req)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/blockscore/actions/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Update
def_delegators 'self.class', :endpoint, :patch

def save!
if respond_to? :id
if persisted?
patch("#{endpoint}/#{id}", filter_params)
true
else
Expand Down
40 changes: 30 additions & 10 deletions lib/blockscore/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,37 @@ module BlockScore
class Base
extend Connection

attr_reader :attributes

def initialize(options = {})
def initialize(options = {}, &block)
@loaded = !(block)
@proc = block
@attributes = options
end

def attributes
return @attributes if @loaded
force!
@attributes
end

def force!
res = @proc.call
@attributes = res.attributes.merge(@attributes)
@loaded = true
self
end

def id
@attributes.fetch(:id, nil)
end

def inspect
"#<#{self.class}:0x#{object_id.to_s(16)} JSON: " + JSON.pretty_generate(attributes)
str_attr = "JSON:#{JSON.pretty_generate(attributes)}"
"#<#{self.class}:0x#{object_id.to_s(16)} #{str_attr}>"
end

def refresh
r = self.class.retrieve(id)
@attributes = r.attributes
res = self.class.retrieve(id)
@attributes = res.attributes

true
rescue Error
Expand Down Expand Up @@ -45,16 +63,18 @@ def self.api_url
end

def self.endpoint
if self == Base
fail NotImplementedError, 'Base is an abstract class, not an API resource'
end
fail NotImplementedError, 'Base is an abstract class, not an API resource' if equal?(Base)

"#{api_url}#{Util.to_plural(resource)}"
end

def persisted?
!id.nil?
end

protected

def add_accessor(symbol, *args)
def add_accessor(symbol, *_args)
singleton_class.instance_eval do
define_method(symbol) do
wrap_attribute(attributes[symbol])
Expand Down
Loading