Skip to content

Commit

Permalink
I'm up in yr VCRz, recordin yr HTTPz.
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke committed Aug 29, 2011
1 parent 3dd6506 commit ab30272
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 58 deletions.
32 changes: 16 additions & 16 deletions Gemfile.lock
Expand Up @@ -12,38 +12,38 @@ PATH
GEM
remote: http://rubygems.org/
specs:
ZenTest (4.6.0)
activemodel (3.0.9)
activesupport (= 3.0.9)
ZenTest (4.6.2)
activemodel (3.0.10)
activesupport (= 3.0.10)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activesupport (3.0.9)
addressable (2.2.4)
activesupport (3.0.10)
addressable (2.2.6)
autotest (4.4.6)
ZenTest (>= 4.4.1)
autotest-rails-pure (4.1.2)
builder (2.1.2)
crack (0.1.8)
diff-lcs (1.1.2)
diff-lcs (1.1.3)
htmlentities (4.3.0)
i18n (0.5.0)
json (1.5.3)
mime-types (1.16)
nokogiri (1.5.0)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
rspec-mocks (2.6.0)
typhoeus (0.2.4)
mime-types
mime-types
vcr (1.5.1)
webmock (1.6.2)
addressable (>= 2.2.2)
vcr (1.11.2)
webmock (1.7.5)
addressable (~> 2.2, > 2.2.5)
crack (>= 0.1.7)

PLATFORMS
Expand Down
8 changes: 3 additions & 5 deletions lib/wordnik/response.rb
Expand Up @@ -36,11 +36,9 @@ def error_message
# TODO: If body is XML, parse it
# Otherwise return raw string
def body
begin
JSON.parse raw.body
rescue
raw.body
end
JSON.parse raw.body
rescue
raw.body
end

def headers
Expand Down
5 changes: 1 addition & 4 deletions spec/endpoint_spec.rb
Expand Up @@ -3,10 +3,7 @@
describe Wordnik::Endpoint do

before(:each) do
# VCR.use_cassette('words', :record => :new_episodes) do
@response = Wordnik::Request.new(:get, "word.json").response.raw
# end
@resource = Wordnik::Resource.new(:name => "word", :raw_data => JSON.parse(@response.body))
@resource = Wordnik::Resource.new(:name => "word", :raw_data => JSON.parse(sample_resource_body))
@endpoint = @resource.endpoints.first
end

Expand Down
6 changes: 1 addition & 5 deletions spec/operation_parameter_spec.rb
Expand Up @@ -3,11 +3,7 @@
describe Wordnik::OperationParameter do

before(:each) do
VCR.use_cassette('words', :record => :new_episodes) do
@response = Typhoeus::Request.get("http://localhost:8001/admin/api/word.json")
end

@operation_parameter = Wordnik::OperationParameter.new(JSON.parse(@response.body)['apis'].first['operations'].first['parameters'].first)
@operation_parameter = Wordnik::OperationParameter.new(JSON.parse(sample_resource_body)['apis'].first['operations'].first['parameters'].first)
end

it "initializes" do
Expand Down
5 changes: 1 addition & 4 deletions spec/operation_spec.rb
Expand Up @@ -3,10 +3,7 @@
describe Wordnik::Operation do

before(:each) do
VCR.use_cassette('words', :record => :new_episodes) do
@response = Typhoeus::Request.get("http://localhost:8001/admin/api/word.json")
end
@resource = Wordnik::Resource.new(:name => "word", :raw_data => JSON.parse(@response.body))
@resource = Wordnik::Resource.new(:name => "word", :raw_data => JSON.parse(sample_resource_body))
@endpoint = @resource.endpoints.first
@operation = @endpoint.operations.first
end
Expand Down
14 changes: 7 additions & 7 deletions spec/resource_spec.rb
Expand Up @@ -3,13 +3,9 @@
describe Wordnik::Resource do

before(:each) do
VCR.use_cassette('words', :record => :new_episodes) do
@response = Typhoeus::Request.get("http://localhost:8001/admin/api/word.json")
end

@default_params = {
:name => "word",
:raw_data => JSON.parse(@response.body)
:raw_data => JSON.parse(sample_resource_body)
}

@resource = Wordnik::Resource.new(@default_params)
Expand Down Expand Up @@ -39,7 +35,9 @@

before(:each) do
configure_wordnik
Wordnik.authenticate
VCR.use_cassette('wordnik_authenticate', :record => :new_episodes) do
Wordnik.authenticate
end
end

it "builds requests but doesn't run them if :request_only is passed" do
Expand All @@ -48,7 +46,9 @@
end

it "runs requests and returns their body if :request_only is absent" do
@response_body = Wordnik.word.get_word('dynamo')
VCR.use_cassette('get_word_dynamo', :record => :new_episodes) do
@response_body = Wordnik.word.get_word('dynamo')
end
@response_body.class.should == Hash
@response_body.keys.sort.should == %w(canonicalForm word)
end
Expand Down
22 changes: 8 additions & 14 deletions spec/response_spec.rb
Expand Up @@ -4,7 +4,7 @@

before(:each) do

VCR.use_cassette('default_response', :record => :new_episodes) do
VCR.use_cassette('word_resource', :record => :new_episodes) do
@raw = Typhoeus::Request.get("http://localhost:8001/admin/api/word.json")
end

Expand All @@ -25,21 +25,15 @@
@response.headers.class.should == Hash
@response.headers['Wordnik-Api-Version'].to_s.should =~ /4\.0/
end

end

describe "unauthorized" do

before do
VCR.use_cassette('unauthorized_response', :record => :new_episodes) do
describe "unauthorized" do
it "raises an error when initialized" do
VCR.use_cassette('get_dog_images', :record => :new_episodes) do
@unauthorized_raw = Typhoeus::Request.get("http://localhost:8001/admin/api/word.json/dog/images/flickr")
expect { Wordnik::Response.new(@unauthorized_raw) }.to raise_error(ClientError)
end
end

it "raises an error when initialized" do
expect { Wordnik::Response.new(@unauthorized_raw) }.to raise_error(ClientError)
end

end
end

describe "format" do
Expand Down Expand Up @@ -69,10 +63,10 @@
it "has a pretty XML body even in the face of adverse characters" do
configure_wordnik
VCR.use_cassette('crazier_json_request', :record => :new_episodes) do
# @request = Wordnik::Request.new(:get, "word.xml/cat/definitions", :params => {:source_dictionaries => "century"})
@request = Wordnik::Request.new(:get, "word.xml/hero/pronunciations", :params => {:limit => 1})
@response = @request.response
end
@request.response.pretty_body.should =~ /\?xml/
@response.pretty_body.should =~ /\?xml/
end

it "has a pretty xml body" do
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -52,6 +52,12 @@ def configure_wordnik

configure_wordnik

def sample_resource_body
@sample_resource_body ||= begin
File.open(File.join(File.dirname(__FILE__), "./swagger/word.json"), "r").read
end
end

# A random string to tack onto stuff to ensure we're not seeing
# data from a previous test run
RAND = ("a".."z").to_a.sample(8).join
31 changes: 31 additions & 0 deletions spec/vcr/crazier_json_request.yml
@@ -0,0 +1,31 @@
---
- !ruby/struct:VCR::HTTPInteraction
request: !ruby/struct:VCR::Request
method: :get
uri: http://localhost:8001/admin/api/word.xml/hero/pronunciations?limit=1
body:
headers:
content-type:
- application/json
api-key:
- b39ee8d5f05d0f566a0080b4c310ceddf5dc5f7606a616f53
user-agent:
- ruby-4.06.06
response: !ruby/struct:VCR::Response
status: !ruby/struct:VCR::ResponseStatus
code: 200
message: OK
headers:
date:
- Mon, 29 Aug 2011 16:28:44 GMT
wordnik-api-version:
- 4.07.67
access-control-allow-origin:
- "*"
content-type:
- application/xml
content-length:
- "170"
body: "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><textProns><textPron seq=\"0\"><id>0</id><raw>hir'\xC5\x8D, h\xC4\x93'r\xC5\x8D'</raw><rawType>webster</rawType></textPron></textProns>"
http_version: "1.1"
:@ignored: false
25 changes: 25 additions & 0 deletions spec/vcr/default_response.yml

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions spec/vcr/get_word_dynamo.yml
@@ -0,0 +1,33 @@
---
- !ruby/struct:VCR::HTTPInteraction
request: !ruby/struct:VCR::Request
method: :get
uri: http://localhost:8001/admin/api/word.json/dynamo
body:
headers:
content-type:
- application/json
api-key:
- b39ee8d5f05d0f566a0080b4c310ceddf5dc5f7606a616f53
auth-token:
- 8f9de26cd0b2c8292d52f78d5082b00073d6ea74d848e3c
user-agent:
- ruby-4.06.06
response: !ruby/struct:VCR::Response
status: !ruby/struct:VCR::ResponseStatus
code: 200
message: OK
headers:
date:
- Mon, 29 Aug 2011 16:33:12 GMT
wordnik-api-version:
- 4.07.67
access-control-allow-origin:
- "*"
content-type:
- application/json
transfer-encoding:
- chunked
body: "{\"canonicalForm\":\"dynamo\",\"word\":\"dynamo\"}"
http_version: "1.1"
:@ignored: false
25 changes: 25 additions & 0 deletions spec/vcr/unauthorized_response.yml
@@ -0,0 +1,25 @@
---
- !ruby/struct:VCR::HTTPInteraction
request: !ruby/struct:VCR::Request
method: :get
uri: http://localhost:8001/admin/api/word.json/dog/images/flickr
body:
headers:
response: !ruby/struct:VCR::Response
status: !ruby/struct:VCR::ResponseStatus
code: 200
message: OK
headers:
date:
- Mon, 29 Aug 2011 16:28:43 GMT
wordnik-api-version:
- 4.07.67
access-control-allow-origin:
- "*"
content-type:
- application/json
transfer-encoding:
- chunked
body: "[{\"id\":3210838977,\"owner\":\"29454428@N08\",\"server\":3464,\"title\":\"Police Dog, Tess, 29/1/35 / by Sam Hood\",\"ownername\":\"State Library of New South Wales collection\",\"secret\":\"5bba45254c\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/29454428@N08/\",\"linkUri\":\"http://farm4.static.flickr.com/3464/3210838977_5bba45254c_b.jpg\"},{\"id\":2741653860,\"owner\":\"83636496@N00\",\"server\":3065,\"title\":\"My little dog\",\"ownername\":\"-=RoBeE=-\",\"secret\":\"468fda7e7a\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/83636496@N00/\",\"linkUri\":\"http://farm4.static.flickr.com/3065/2741653860_468fda7e7a_m.jpg\"},{\"id\":2233632419,\"owner\":\"41754875@N00\",\"server\":2180,\"title\":\"the black box relevation:I think I like you\",\"ownername\":\"visualpanic\",\"secret\":\"409e943e69\",\"farm\":3,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/41754875@N00/\",\"linkUri\":\"http://farm3.static.flickr.com/2180/2233632419_409e943e69_m.jpg\"},{\"id\":32815499,\"owner\":\"35237093637@N01\",\"server\":21,\"title\":\"anti botox brigade\",\"ownername\":\"emdot\",\"secret\":\"222fefb30d\",\"farm\":1,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/35237093637@N01/\",\"linkUri\":\"http://farm1.static.flickr.com/21/32815499_222fefb30d_m.jpg\"},{\"id\":4737490919,\"owner\":\"31308183@N06\",\"server\":4116,\"title\":\"Katy Perry - Goddess\",\"ownername\":\"Daniel Suarez\xE2\x84\xA2\",\"secret\":\"c738598de3\",\"farm\":5,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/31308183@N06/\",\"linkUri\":\"http://farm5.static.flickr.com/4116/4737490919_c738598de3_m.jpg\"},{\"id\":4401299142,\"owner\":\"17039753@N06\",\"server\":2711,\"title\":\"SuperPup\",\"ownername\":\"Emmanuel_D.Photography\",\"secret\":\"336f16f50e\",\"farm\":3,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/17039753@N06/\",\"linkUri\":\"http://farm3.static.flickr.com/2711/4401299142_336f16f50e_m.jpg\"},{\"id\":4159767301,\"owner\":\"8070463@N03\",\"server\":2641,\"title\":\"For the wolf fans...\",\"ownername\":\"Tambako the Jaguar\",\"secret\":\"70e5d09f4b\",\"farm\":3,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/8070463@N03/\",\"linkUri\":\"http://farm3.static.flickr.com/2641/4159767301_70e5d09f4b_m.jpg\"},{\"id\":3310906188,\"owner\":\"24785917@N03\",\"server\":3561,\"title\":\"A man and woman outside a bush hut\",\"ownername\":\"Powerhouse Museum Collection\",\"secret\":\"5f39d0cbe0\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/24785917@N03/\",\"linkUri\":\"http://farm4.static.flickr.com/3561/3310906188_5f39d0cbe0_m.jpg\"},{\"id\":3211654136,\"owner\":\"29454428@N08\",\"server\":3454,\"title\":\"Margaret Shaffhauser with bull terrier dog at the Canine Association Show, 3 Nov 1934 / by Ted Hood\",\"ownername\":\"State Library of New South Wales collection\",\"secret\":\"9916e9cd54\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/29454428@N08/\",\"linkUri\":\"http://farm4.static.flickr.com/3454/3211654136_9916e9cd54_m.jpg\"},{\"id\":3211639268,\"owner\":\"29454428@N08\",\"server\":3416,\"title\":\"Study of a small girl with a prize Scottish terrier dog, c. 1935 / by Sam Hood\",\"ownername\":\"State Library of New South Wales collection\",\"secret\":\"88b64f463c\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/29454428@N08/\",\"linkUri\":\"http://farm4.static.flickr.com/3416/3211639268_88b64f463c_m.jpg\"},{\"id\":3211518494,\"owner\":\"29454428@N08\",\"server\":3425,\"title\":\"Artist and dog arrive by Melbourne Express (taken for J.C. Williamson), 10/12/1937 / byTed hood\",\"ownername\":\"State Library of New South Wales collection\",\"secret\":\"03b156807a\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/29454428@N08/\",\"linkUri\":\"http://farm4.static.flickr.com/3425/3211518494_03b156807a_m.jpg\"},{\"id\":3194993477,\"owner\":\"24785917@N03\",\"server\":3411,\"title\":\"Portrait view of child wearing a sun hat, with dog and toy cat (different angle)\",\"ownername\":\"Powerhouse Museum Collection\",\"secret\":\"444f03eee3\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/24785917@N03/\",\"linkUri\":\"http://farm4.static.flickr.com/3411/3194993477_444f03eee3_m.jpg\"},{\"id\":3169118372,\"owner\":\"24785917@N03\",\"server\":3089,\"title\":\"Bare foot boy wearing a hat, seated on a log\",\"ownername\":\"Powerhouse Museum Collection\",\"secret\":\"7097baccd9\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/24785917@N03/\",\"linkUri\":\"http://farm4.static.flickr.com/3089/3169118372_7097baccd9_m.jpg\"},{\"id\":2959326615,\"owner\":\"29454428@N08\",\"server\":3190,\"title\":\"Blizzard, the pup in Antarctica / photograph by Frank Hurley\",\"ownername\":\"State Library of New South Wales collection\",\"secret\":\"d7ae66b193\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/29454428@N08/\",\"linkUri\":\"http://farm4.static.flickr.com/3190/2959326615_d7ae66b193_m.jpg\"},{\"id\":2868980227,\"owner\":\"29454428@N08\",\"server\":3193,\"title\":\"Huskies pulling sledge\",\"ownername\":\"State Library of New South Wales collection\",\"secret\":\"cf01a70c46\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/29454428@N08/\",\"linkUri\":\"http://farm4.static.flickr.com/3193/2868980227_cf01a70c46_m.jpg\"},{\"id\":409313262,\"owner\":\"72922449@N00\",\"server\":152,\"title\":\"Es tu pa\xC3\xADs\",\"ownername\":\"Rodrigo Basaure\",\"secret\":\"60fc5d95a5\",\"farm\":1,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/72922449@N00/\",\"linkUri\":\"http://farm1.static.flickr.com/152/409313262_60fc5d95a5_m.jpg\"},{\"id\":4516905723,\"owner\":\"91695677@N00\",\"server\":4055,\"title\":\"Good Morning, Dear Dog\",\"ownername\":\"Helga Weber\",\"secret\":\"93091a8850\",\"farm\":5,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/91695677@N00/\",\"linkUri\":\"http://farm5.static.flickr.com/4055/4516905723_93091a8850_m.jpg\"},{\"id\":3019776218,\"owner\":\"26611570@N04\",\"server\":3280,\"title\":\"0216\",\"ownername\":\"Cia de Foto\",\"secret\":\"9cfddfea31\",\"farm\":4,\"ispublic\":1,\"isfriend\":0,\"isfamily\":0,\"ownerLink\":\"http://www.flickr.com/photos/26611570@N04/\",\"linkUri\":\"http://farm4.static.flickr.com/3280/3019776218_9cfddfea31_m.jpg\"}]"
http_version: "1.1"
:@ignored: false
31 changes: 31 additions & 0 deletions spec/vcr/wordnik_authenticate.yml
@@ -0,0 +1,31 @@
---
- !ruby/struct:VCR::HTTPInteraction
request: !ruby/struct:VCR::Request
method: :get
uri: http://localhost:8001/admin/api/account.json/authenticate/zeke?password=gomer
body:
headers:
content-type:
- application/json
api-key:
- b39ee8d5f05d0f566a0080b4c310ceddf5dc5f7606a616f53
user-agent:
- ruby-4.06.06
response: !ruby/struct:VCR::Response
status: !ruby/struct:VCR::ResponseStatus
code: 200
message: OK
headers:
date:
- Mon, 29 Aug 2011 16:28:39 GMT
wordnik-api-version:
- 4.07.67
access-control-allow-origin:
- "*"
content-type:
- application/json
transfer-encoding:
- chunked
body: "{\"token\":\"8f9de26cd0b2c8292d52f78d5082b00073d6ea74d848e3c\",\"userId\":1010037,\"userSignature\":\"79ea28fbc85f00e35ad5a5bc58ddf99b\"}"
http_version: "1.1"
:@ignored: false

0 comments on commit ab30272

Please sign in to comment.