Skip to content

Commit ec32151

Browse files
authored
Merge pull request #135 from ryanhertz/newsfeed-meta-requests
Newsfeed meta requests
2 parents 03d44a0 + decbd84 commit ec32151

File tree

7 files changed

+93
-86
lines changed

7 files changed

+93
-86
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
v1.4.14
2+
- fixed duplicate requests in NewsFeedMeta
3+
14
v1.4.13
25
- added SystemInfoSearch model
36

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.13
1+
1.4.14

lib/spark_api/models/news_feed_meta.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def core_fields
3131
private
3232

3333
def data
34-
@data ||= connection.get(self.path).first
34+
if attributes.empty?
35+
@data ||= connection.get(self.path).first
36+
else
37+
attributes
38+
end
3539
end
3640

3741
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def reset_config
4848
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
4949

5050
RSpec.configure do |config|
51+
52+
config.include WebMock::API
53+
config.include StubApiRequests
54+
5155
config.treat_symbols_as_metadata_keys_with_true_values = true
5256
config.alias_example_to :on_get_it, :method => 'GET'
5357
config.alias_example_to :on_put_it, :method => 'PUT'

spec/support/mock_helper.rb

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,3 @@
1-
RSpec.configure do |config|
2-
config.include WebMock::API
3-
end
4-
5-
$test_client = SparkApi::Client.new({:api_key=>"", :api_secret=>""})
6-
7-
def stub_api_get(service_path, stub_fixture="success.json", opts={})
8-
params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
9-
sig = $test_client.authenticator.sign_token("/#{SparkApi.version}#{service_path}", params)
10-
s=stub_request(:get, "#{SparkApi.endpoint}/#{SparkApi.version}#{service_path}").
11-
with(:query => {
12-
:ApiSig => sig
13-
}.merge(params))
14-
if(block_given?)
15-
yield s
16-
else
17-
s.to_return(:body => fixture(stub_fixture))
18-
end
19-
log_stub(s)
20-
end
21-
def stub_api_delete(service_path, stub_fixture="success.json", opts={})
22-
params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
23-
sig = $test_client.authenticator.sign_token("/#{SparkApi.version}#{service_path}", params)
24-
s=stub_request(:delete, "#{SparkApi.endpoint}/#{SparkApi.version}#{service_path}").
25-
with(:query => {
26-
:ApiSig => sig
27-
}.merge(params))
28-
if(block_given?)
29-
yield s
30-
else
31-
s.to_return(:body => fixture(stub_fixture))
32-
end
33-
log_stub(s)
34-
end
35-
def stub_api_post(service_path, body, stub_fixture="success.json", opts={})
36-
if body.is_a?(Hash)
37-
body = { :D => body } unless body.empty?
38-
elsif !body.nil?
39-
body = MultiJson.load(fixture(body).read)
40-
end
41-
body_str = body.nil? ? body : MultiJson.dump(body)
42-
params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
43-
sig = $test_client.authenticator.sign_token("/#{SparkApi.version}#{service_path}", params, body_str)
44-
s=stub_request(:post, "#{SparkApi.endpoint}/#{SparkApi.version}#{service_path}").
45-
with(:query => {
46-
:ApiSig => sig
47-
}.merge(params),
48-
:body => body
49-
)
50-
if(block_given?)
51-
yield s
52-
else
53-
s.to_return(:body => fixture(stub_fixture))
54-
end
55-
log_stub(s)
56-
end
57-
def stub_api_put(service_path, body, stub_fixture="success.json", opts={})
58-
if body.is_a? Hash
59-
body = { :D => body }
60-
elsif !body.nil?
61-
body = MultiJson.load(fixture(body).read)
62-
end
63-
body_str = body.nil? ? body : MultiJson.dump(body)
64-
params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
65-
sig = $test_client.authenticator.sign_token("/#{SparkApi.version}#{service_path}", params, body_str)
66-
full_path = "#{SparkApi.endpoint}/#{SparkApi.version}#{service_path}"
67-
s=stub_request(:put, full_path).with(:query => {
68-
:ApiSig => sig
69-
}.merge(params),
70-
:body => body
71-
)
72-
if(block_given?)
73-
yield s
74-
else
75-
s.to_return(:body => fixture(stub_fixture))
76-
end
77-
log_stub(s)
78-
end
79-
80-
def log_stub(s)
81-
SparkApi.logger.debug("Stubbed Request: #{s.inspect} \n\n")
82-
return s
83-
end
84-
851
def mock_session()
862
SparkApi::Authentication::Session.new("AuthToken" => "1234", "Expires" => (Time.now + 3600).to_s, "Roles" => "['idx']")
873
end

spec/support/stub_api_requests.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module StubApiRequests
2+
3+
def stub_api_get(service_path, stub_fixture="success.json", opts={}, &block)
4+
stub_api_request(:get, service_path, stub_fixture, nil, opts, &block)
5+
end
6+
7+
def stub_api_delete(service_path, stub_fixture="success.json", opts={}, &block)
8+
stub_api_request(:delete, service_path, stub_fixture, nil, opts, &block)
9+
end
10+
11+
def stub_api_post(service_path, body, stub_fixture="success.json", opts={}, &block)
12+
stub_api_request(:post, service_path, stub_fixture, body, opts, &block)
13+
end
14+
15+
def stub_api_put(service_path, body, stub_fixture="success.json", opts={}, &block)
16+
stub_api_request(:put, service_path, stub_fixture, body, opts, &block)
17+
end
18+
19+
def expect_api_request(meth, service_path, body=nil, opts={})
20+
args = with_args(service_path, opts, body)
21+
expect(a_request(meth, full_path(service_path)).with(args))
22+
end
23+
24+
private
25+
26+
def full_path(path)
27+
"#{SparkApi.endpoint}/#{SparkApi.version}#{path}"
28+
end
29+
30+
def stub_api_request(meth, service_path, stub_fixture, body, opts, &block)
31+
args = with_args(service_path, opts, body)
32+
s = stub_request(meth, full_path(service_path)).with(args)
33+
34+
if(block_given?)
35+
yield s
36+
else
37+
s.to_return(:body => fixture(stub_fixture))
38+
end
39+
log_stub(s)
40+
end
41+
42+
def with_args(service_path, opts, body=nil)
43+
if body.is_a?(Hash)
44+
body = { :D => body } unless body.empty?
45+
elsif !body.nil?
46+
body = MultiJson.load(fixture(body).read)
47+
end
48+
body_str = body.nil? ? body : MultiJson.dump(body)
49+
50+
params = {:ApiUser => "foobar", :AuthToken => "c401736bf3d3f754f07c04e460e09573"}.merge(opts)
51+
query = {:ApiSig => get_signature(service_path, params, body_str) }
52+
query.merge!(params)
53+
54+
ret = {query: query}
55+
ret[:body] = body unless body.nil?
56+
ret
57+
end
58+
59+
def test_client
60+
@test_client ||= SparkApi::Client.new({:api_key=>"", :api_secret=>""})
61+
end
62+
63+
def get_signature(service_path, params, body_str=nil)
64+
test_client.authenticator.sign_token("/#{SparkApi.version}#{service_path}", params, body_str)
65+
end
66+
67+
def log_stub(s)
68+
SparkApi.logger.debug("Stubbed Request: #{s.inspect} \n\n")
69+
return s
70+
end
71+
72+
end

spec/unit/spark_api/models/news_feed_meta_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
stub_api_get("/newsfeeds/meta", "newsfeeds/meta.json")
1111
end
1212

13+
describe 'fetching data' do
14+
it "only makes one api request" do
15+
news_feed_meta = NewsFeedMeta.get.first
16+
news_feed_meta.minimum_core_fields
17+
expect_api_request(:get, "/newsfeeds/meta").to have_been_made.once
18+
end
19+
end
20+
1321
describe 'minimum_core_fields' do
1422
it 'returns the minimum number of required fields' do
1523
expect(news_feed_meta.minimum_core_fields).to eq 3

0 commit comments

Comments
 (0)