Permalink
Browse files

cleaning things up.

  • Loading branch information...
1 parent 2e9c4f4 commit 2e37839c7131b09407097d4017f1d78efef5a639 @anthias anthias committed Oct 7, 2011
@@ -1,6 +1,6 @@
-class Response
+class Client
attr_accessor :raw, :base, :format, :unwrap
def initialize(base,format)
@@ -34,11 +34,11 @@ def response
end
def raw_response
- Response.hash2ostruct(@raw.parsed_response)
+ Client.hash2ostruct(@raw.parsed_response)
end
def headers
- Response.hash2ostruct(@raw.headers)
+ Client.hash2ostruct(@raw.headers)
end
def body
@@ -56,12 +56,12 @@ def self.hash2ostruct(object)
when Hash
object = object.clone
object.each do |key, value|
- object[key] = Response.hash2ostruct(value)
+ object[key] = Client.hash2ostruct(value)
end
OpenStruct.new(object)
when Array
object = object.clone
- object.map! { |i| Response.hash2ostruct(i) }
+ object.map! { |i| Client.hash2ostruct(i) }
else
object
end
@@ -1,43 +0,0 @@
-
-module DiscoveryHelper
-
- def resource
- @session.resource
- end
-
- def xml_endpoints
- resource.raw.xml.response.discovery.endpoints.endpoint
- end
-
- def json_endpoints
- resource.raw.json.response.endpoints
- end
-
- def all_endpoints
- xml_endpoints + json_endpoints
- end
-
- def v2_endpoints
- all_endpoints.select{|endpoint| endpoint.specification =~ /^.*_v2$/i }
- end
-
- def xml_discovery
- resource.raw.xml.response.discovery
- end
-
- def json_discovery
- resource.raw.json.response
- end
-
- def endpoint_array(point)
- if point.formats.class == Array
- point.formats
- elsif point.formats.format.class == Array
- point.formats.format
- else
- []
- end
- end
-
- extend self
-end
View
@@ -15,14 +15,14 @@ def get_next
def discovery_url
@raw = OpenStruct.new
- @raw.json = Response.new(@options[:discovery_url],'json').get(".json")
- @raw.xml = Response.new(@options[:discovery_url],'xml').get(".xml")
+ @raw.json = Client.new(@options[:discovery_url],'json').get(".json")
+ @raw.xml = Client.new(@options[:discovery_url],'xml').get(".xml")
end
def services_resource
@raw = []
@session.endpoints.each do |endpoint|
- response = Response.new(endpoint[0],endpoint[1])
+ response = Client.new(endpoint[0],endpoint[1])
response.get_and_unwrap("/services.#{endpoint[1]}?jurisdiction_id=#{@options[:jurisdiction_id]}",'services.service')
@raw << response
end
@@ -33,7 +33,7 @@ def definition_resource
@session.raw_services.each do |raw_service|
raw_service.response.each do |service|
next if !service.metadata or service.metadata == 'false'
- response = Response.new(raw_service.base,raw_service.format)
+ response = Client.new(raw_service.base,raw_service.format)
response.get_and_unwrap("/services/#{service.service_code}.#{raw_service.format}?jurisdiction_id=#{@options[:jurisdiction_id]}",'')
@raw << response
end
@@ -63,12 +63,12 @@ def create_resource_once
# For each endpoint...
@session.discovery.xml.response.discovery.endpoints.endpoint.each do |endpoint|
next if type(endpoint) == 'production' and !@options.production
- @raw.xml << Response.new.post("#{endpoint.url}/requests.xml",data_from_options)
+ @raw.xml << Client.new.post("#{endpoint.url}/requests.xml",data_from_options)
end
@session.discovery.json.response.endpoints.each do |endpoint|
next if type(endpoint) == 'production' and !@options.production
- @raw.json << Response.new.post("#{endpoint.url}/requests.json",data_from_options)
+ @raw.json << Client.new.post("#{endpoint.url}/requests.json",data_from_options)
end if @options.json
@raw.options = @options
View
@@ -7,6 +7,21 @@ def initialize(options)
@options = options
end
+ def run_tests
+ load 'tests/discovery.rb'
+ load 'tests/services.rb'
+ load 'tests/service_definition.rb'
+ #load 'tests/create.rb' if @options[:write]
+ end
+
+ #
+ # Resource mgmt.
+ def add_resource(options)
+ resource = Resource.new(self,@options.merge(options))
+ resource.get_next
+ @resources << resource
+ end
+
def resources
@resources
end
@@ -19,27 +34,7 @@ def resource
@resources.last
end
- def prev_resource
- @resources[@resources.count-2]
- end
-
- def add_resource(options)
- resource = Resource.new(self,@options.merge(options))
- resource.get_next
- @resources << resource
- end
-
- def run_tests
- load 'tests/discovery.rb'
-# endpoints
- load 'tests/services.rb'
- load 'tests/service_definition.rb'
- #load 'tests/create.rb' if @options[:write]
- end
-
- #
- # :url => format
- #
+ # Endpoint mgmt.
def endpoints
t = []
# We need to handle json and xml differently because xml sucks and can't be parsed predictably.
@@ -58,14 +53,6 @@ def endpoints
t
end
- def services
- @resources[1].raw.map{|r| r.response }.flatten
- end
-
- def raw_services
- @resources[1].raw
- end
-
def all_endpoints
json_endpoints + xml_endpoints
end
@@ -74,14 +61,22 @@ def json_endpoints
Session.unwrap(discovery.raw.json, 'response.endpoints').select{|endpoint| production_safe?(endpoint) }
end
- def production_safe?(endpoint)
- type(endpoint) != 'production' or @options[:production]
- end
-
def xml_endpoints
Session.ensure_array(Session.unwrap(discovery.raw.xml,'response.discovery.endpoints.endpoint')).select{|endpoint| production_safe?(endpoint) }
end
+ #
+ # Services Lookup
+ #
+ def services
+ @resources[1].raw.map{|r| r.response }.flatten
+ end
+
+ def raw_services
+ @resources[1].raw
+ end
+
+ # Class Methods
def self.unwrap(obj,methods)
methods.split('.').each do |method|
obj = obj.send method.to_sym
@@ -95,8 +90,9 @@ def self.ensure_array(result)
private
- def type(endpoint)
- endpoint.marshal_dump[:type]
+ # The marshal_dump is to avoid calling .type on the obj.
+ def production_safe?(endpoint)
+ endpoint.marshal_dump[:type] != 'production' or @options[:production]
end
end
View
@@ -4,6 +4,34 @@ module TestHelper
def validate_date_time(time)
return true if Time.iso8601(time) rescue false
end
+
+ def resource
+ @session.resource
+ end
+
+ def xml_endpoints
+ @session.discovery.raw.xml.response.discovery.endpoints.endpoint
+ end
+
+ def json_endpoints
+ @session.discovery.raw.json.response.endpoints
+ end
+
+ def all_endpoints
+ xml_endpoints + json_endpoints
+ end
+
+ def v2_endpoints
+ all_endpoints.select{|endpoint| endpoint.specification =~ /^.*_v2$/i }
+ end
+
+ def xml_discovery
+ @session.discovery.raw.xml.response.discovery
+ end
+
+ def json_discovery
+ @session.discovery.raw.json.response
+ end
extend self
end
@@ -1,10 +1,11 @@
-require 'rubygems'
-require 'response'
+
+require 'client'
require 'session'
require 'resource'
require 'ruby_spec'
+
+require 'rubygems'
require 'ostruct'
require 'httparty'
-require 'spec'
-require 'test_helper';include TestHelper
+require 'spec'
View
@@ -12,7 +12,7 @@
data = { :email => 'foo@bar.com', :description => 'I know' }
-test "Creating Response Once", { :with => :create_resource_once, :post => data } do
+test "Creating Client Once", { :with => :create_resource_once, :post => data } do
rule 'each should return something' do
@resource.xml.each do |request|
View
@@ -1,15 +1,4 @@
-require 'helpers/discovery_helper';include DiscoveryHelper
-
- # @session.resources = History of resources for entire session of tests.
- # @resource = currently tested resource.
- # @resource.options = options used for that resource
- # @resource.json.response = objectified response
- # @resource.json.headers = response headers
- # @resource.json.body = response body
- # @resource.json.request = httparty request object
- #
- #
-
+require 'test_helper';include TestHelper
test "Service Discovery", :with => :discovery_url do
@@ -88,14 +77,14 @@
end
rule "each endpoint should have at least one supported format" do
- all_endpoints.each do |endpoint|
- endpoint_array(endpoint).size.should > 0
+ @session.endpoints.each do |endpoint|
+ endpoint.formats.size.should > 0
end
end
rule "all v2 endpoints should support xml" do
v2_endpoints.each do |endpoint|
- endpoint_array(endpoint).select{|format| format == 'text/xml' }.size.should > 0
+ endpoint.formats.select{|format| format == 'text/xml' }.size.should > 0
end
end
@@ -1,13 +1,4 @@
-
-# @session.resources = History of resources for entire session of tests.
-# @resource = currently tested resource.
-# @resource.options = options used for that resource
-# @resource.json.response = objectified response
-# @resource.json.headers = response headers
-# @resource.json.body = response body
-# @resource.json.request = httparty request object
-#
-#
+require 'test_helper';include TestHelper
test "Service Definitions", :with => :definition_resource do
View
@@ -1,33 +1,18 @@
+require 'test_helper';include TestHelper
- # @session.resources = History of resources for entire session of tests.
- # @resource = currently tested resource.
- # @resource.options = options used for that resource
- # @resource.json.response = objectified response
- # @resource.json.headers = response headers
- # @resource.json.body = response body
- # @resource.json.request = httparty request object
- #
- #
test "Services", :with => :services_resource do
- rule 'each should return something' do
- # raise resource.raw.map{|r| r.response }.flatten.inspect
-
- resource.raw.each do |services|
- services.response
+ rule 'there should be at least one service right?' do
+ raw_services.each do |services|
+ services.response.size.should > 0
+ end
+ end
+
+ rule 'all services should have a service code' do
+ @session.services.each do |service|
+ service.service_code.should_not be.nil?
end
-
-
- # @resource.xml.each do |request|
- # request.response.should_not == ''
- # end
- #
- # if @resource.options.json
- # @resource.json.each do |request|
- # request.response.should_not == ''
- # end
- # end
end
end

0 comments on commit 2e37839

Please sign in to comment.