Skip to content

Commit a0ddc70

Browse files
committed
Merge pull request #39 from BlockScore/fix/person-question_sets-array
Fix person-questions_sets-array
2 parents 1e428e2 + ffb5320 commit a0ddc70

26 files changed

+790
-118
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# rcov generated
23
coverage
34
coverage.data
@@ -15,7 +16,7 @@ doc
1516
# jeweler generated
1617
pkg
1718

18-
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
19+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
1920
#
2021
# * Create a file at ~/.gitignore
2122
# * Include files you want ignored

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ gem install blockscore
1313
If you are using Rails, add the following to your `Gemfile`:
1414

1515
```ruby
16-
gem 'blockscore', '~> 4.1.2'
16+
gem 'blockscore', '~> 4.2.0'
1717
```
1818

1919
## Getting Started
@@ -60,5 +60,5 @@ To see the list of calls you can make, please visit our [full Ruby API reference
6060
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:
6161

6262
```shell
63-
$ rake test
63+
$ rspec spec
6464
```

blockscore.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
2929
spec.add_development_dependency 'bundler', '~> 1.0'
3030
spec.add_development_dependency 'simplecov', '~> 0'
3131
spec.add_development_dependency 'rspec', '~> 3'
32+
spec.add_development_dependency 'rspec-its', '~> 1'
3233
spec.add_development_dependency 'webmock', '~> 1.21'
3334
spec.add_development_dependency 'faker', '~> 1.4'
3435
spec.add_development_dependency 'factory_girl', '~> 4.5'

lib/blockscore.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'delegate'
12
require 'forwardable'
23
require 'httparty'
34
require 'json'
@@ -26,6 +27,7 @@
2627
require 'blockscore/watchlist_hit'
2728

2829
require 'blockscore/collection'
30+
require 'blockscore/collection/member'
2931
require 'blockscore/connection'
3032
require 'blockscore/dispatch'
3133
require 'blockscore/fingerprint'
@@ -34,11 +36,7 @@
3436
require 'blockscore/version'
3537

3638
module BlockScore
37-
def self.api_key=(api_key)
38-
@api_key = api_key
39-
end
40-
41-
def self.api_key
42-
@api_key
39+
class << self
40+
attr_accessor :api_key
4341
end
4442
end

lib/blockscore/actions/retrieve.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ module Actions
1111
# => #<BlockScore::Person:0x007fe39c424410>
1212
module Retrieve
1313
module ClassMethods
14-
def retrieve(id)
15-
get("#{endpoint}/#{id}", {})
14+
def retrieve(id, options = {})
15+
fail ArgumentError if id.empty?
16+
req = ->() { get("#{endpoint}/#{id}", options) }
17+
new(id: id, &req)
1618
end
1719
end
1820

lib/blockscore/actions/update.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Update
2828
def_delegators 'self.class', :endpoint, :patch
2929

3030
def save!
31-
if respond_to? :id
31+
if persisted?
3232
patch("#{endpoint}/#{id}", filter_params)
3333
true
3434
else

lib/blockscore/base.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,37 @@ module BlockScore
44
class Base
55
extend Connection
66

7-
attr_reader :attributes
8-
9-
def initialize(options = {})
7+
def initialize(options = {}, &block)
8+
@loaded = !(block)
9+
@proc = block
1010
@attributes = options
1111
end
1212

13+
def attributes
14+
return @attributes if @loaded
15+
force!
16+
@attributes
17+
end
18+
19+
def force!
20+
res = @proc.call
21+
@attributes = res.attributes.merge(@attributes)
22+
@loaded = true
23+
self
24+
end
25+
26+
def id
27+
@attributes.fetch(:id, nil)
28+
end
29+
1330
def inspect
14-
"#<#{self.class}:0x#{object_id.to_s(16)} JSON: " + JSON.pretty_generate(attributes)
31+
str_attr = "JSON:#{JSON.pretty_generate(attributes)}"
32+
"#<#{self.class}:0x#{object_id.to_s(16)} #{str_attr}>"
1533
end
1634

1735
def refresh
18-
r = self.class.retrieve(id)
19-
@attributes = r.attributes
36+
res = self.class.retrieve(id)
37+
@attributes = res.attributes
2038

2139
true
2240
rescue Error
@@ -45,16 +63,18 @@ def self.api_url
4563
end
4664

4765
def self.endpoint
48-
if self == Base
49-
fail NotImplementedError, 'Base is an abstract class, not an API resource'
50-
end
66+
fail NotImplementedError, 'Base is an abstract class, not an API resource' if equal?(Base)
5167

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

71+
def persisted?
72+
!id.nil?
73+
end
74+
5575
protected
5676

57-
def add_accessor(symbol, *args)
77+
def add_accessor(symbol, *_args)
5878
singleton_class.instance_eval do
5979
define_method(symbol) do
6080
wrap_attribute(attributes[symbol])

0 commit comments

Comments
 (0)