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
41 changes: 31 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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what is going on here. I looked at the inheriting classes and I can't seem to find a block being provided

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the block is provided by retrieve

@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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MethodCallParentheses: Do not use parentheses for method calls with no arguments.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you didn't write this part but these two lines could likely just be one, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that looks good


true
rescue Error
Expand All @@ -31,6 +49,7 @@ def save

def save!
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While looking at this I noticed the other save method has a bare rescue which means it just swallows up ALL errors:

def save
  save!
rescue
  false
end

I thought I caught this back when the gem was first released but I guess not. Would you mind fixing that and double checking this isn't done elsewhere in the lib?

response = self.class.post(self.class.endpoint, attributes)
# binding.pry
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pry

@attributes = response.attributes

true
Expand All @@ -45,16 +64,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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just simplify this if we don't even use _args?

singleton_class.instance_eval do
define_method(symbol) do
wrap_attribute(attributes[symbol])
Expand Down
Loading