Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from reinteractive-open/develop

Develop
  • Loading branch information...
commit 7b59fe19763ef0558dcfcc9688d2ca16ccf7e695 2 parents 2b0db29 + 82d29d1
@mikel mikel authored
View
1  .gitignore
@@ -1 +1,2 @@
pkg
+.rvmrc
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm use ruby-1.9.3
View
9 Gemfile
@@ -0,0 +1,9 @@
+source :rubygems
+
+group :test do
+ gem 'rake', '~> 10.0'
+ gem 'rspec', '~> 2.12'
+ gem 'ffaker', '~> 1.15'
+ gem 'webmock', '~> 1.9'
+ gem 'hoe', '>= 3.3'
+end
View
31 Gemfile.lock
@@ -0,0 +1,31 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.3.2)
+ crack (0.3.1)
+ diff-lcs (1.1.3)
+ ffaker (1.15.0)
+ hoe (3.3.1)
+ rake (>= 0.8, < 11.0)
+ rake (10.0.1)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.0)
+ rspec-expectations (2.12.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.0)
+ webmock (1.9.0)
+ addressable (>= 2.2.7)
+ crack (>= 0.1.7)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ ffaker (~> 1.15)
+ hoe (>= 3.3)
+ rake (~> 10.0)
+ rspec (~> 2.12)
+ webmock (~> 1.9)
View
6 History.txt
@@ -1,3 +1,9 @@
+=== 0.1.0 2012-12-03
+
+* Convert tests to RSpec
+* Remove Hash, Object and Array class over rides.
+* Fix array index error.
+
=== 0.0.1 2010-04-22
* 1 major enhancement:
View
12 Manifest.txt
@@ -1,16 +1,16 @@
+.rspec
+.rvmrc
+Gemfile
+Gemfile.lock
History.txt
Manifest.txt
README.rdoc
Rakefile
lib/switchvox.rb
-lib/switchvox/array.rb
lib/switchvox/base.rb
-lib/switchvox/hash.rb
lib/switchvox/net_http_digest_auth.rb
-lib/switchvox/object.rb
script/console
script/destroy
script/generate
-test/test_helper.rb
-test/test_json_to_obj.rb
-test/test_switchvox.rb
+spec/spec_helper.rb
+spec/switchvox/base_spec.rb
View
5 Rakefile
@@ -1,5 +1,4 @@
require 'rubygems'
-gem 'hoe', '>= 2.1.0'
require 'hoe'
require 'fileutils'
require './lib/switchvox'
@@ -18,8 +17,8 @@ $hoe = Hoe.spec 'switchvox' do
end
-require 'newgem/tasks'
-Dir['tasks/**/*.rake'].each { |t| load t }
+# require 'newgem/tasks'
+# Dir['tasks/**/*.rake'].each { |t| load t }
# TODO - want other tests/tasks run by default? Add them to the list
# remove_task :default
View
3  lib/switchvox.rb
@@ -1,9 +1,6 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-require 'switchvox/hash'
-require 'switchvox/array'
-require 'switchvox/object'
require 'switchvox/net_http_digest_auth'
require 'switchvox/base'
View
14 lib/switchvox/array.rb
@@ -1,14 +0,0 @@
-# A fancy way of iterating over an array and converting hashes to objects
-class Array
- def to_obj
- # Make a deep copy of the array
- a = Marshal.load(Marshal.dump(self))
- a.each do |i|
- case i.class.to_s
- when "Hash" then i = i.to_obj
- when "Array" then i = i.to_obj
- end
- end
- a
- end
-end
View
51 lib/switchvox/base.rb
@@ -8,6 +8,7 @@ module Switchvox
require 'digest/md5'
require 'rubygems'
require 'json'
+require 'ostruct'
# Raised when credentials are incorrect
class LoginError < RuntimeError
@@ -22,7 +23,7 @@ class UnhandledResponse < RuntimeError
end
# The primary class used to interact with Switchvox.
-class Base
+class Base
URL = "/json"
attr :host, true
@@ -44,7 +45,7 @@ def initialize(host, user, pass, options={})
@url = URI.parse("https://" + @host + URL)
@ssl = false
@ssl = true if @url.scheme == "https"
-
+
@connection = false
@auth_header = false
login!
@@ -55,40 +56,45 @@ def initialize(host, user, pass, options={})
def request(method, parameters={})
login! unless logged_in?
json = wrap_json(method, parameters)
-
+
# Send the request
header = {'Content-Type' => "text/json"}
+
request = Net::HTTP::Post.new(@url.path, header)
request.digest_auth(@user, @pass, @auth_header)
request.body = json
response = @connection.request(request)
- if @debug
- puts "#{method}: Request"
- puts json
- puts "\n"
- end
-
case response
when Net::HTTPOK
raise EmptyResponse unless response.body
- response_json = JSON.parse response.body
- if @debug
- puts "#{method}: Response:"
- pp response_json
- puts "\n\n"
- end
- response_obj = response_json["response"]["result"].to_obj
- return response_obj
+ return json_parse(response.body)
when Net::HTTPUnauthorized
login!
request(method, parameters)
when Net::HTTPForbidden
- raise LoginError, "Invalid Username or Password"
+ raise LoginError, "Invalid Username or Password"
else raise UnhandledResponse, "Can't handle response #{response}"
end
end
+ # TODO - cover this with specs and return json not call other method
+ def json_parse(body)
+ json = JSON.parse body
+ convert_to_obj(json["response"]["result"])
+ end
+
+ def convert_to_obj(arg)
+ if arg.is_a? Hash
+ arg.each { |k, v| arg[k] = convert_to_obj(v) }
+ OpenStruct.new arg
+ elsif arg.is_a? Array
+ arg.map! { |v| convert_to_obj(v) }
+ else
+ arg
+ end
+ end
+
protected
# Check to see if we are logged in
@@ -96,7 +102,7 @@ def logged_in?
return false unless @auth_header
true
end
-
+
# Attempt HTTP Digest Authentication with Switchvox
def login!
connect! unless connected?
@@ -117,9 +123,6 @@ def connect!
@connection.use_ssl = true
@connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
- if @debug
- #@connection.set_debug_output $stderr
- end
@connection.start
end
@@ -137,9 +140,7 @@ def wrap_json(method, parameters={})
end
end
-
-end
-
+end
View
17 lib/switchvox/hash.rb
@@ -1,17 +0,0 @@
-# A fancy way of iterating over a hash and converting hashes to objects
-class Hash
- def to_obj
- o = Object.new
- self.each do |k,v|
- # If we're looking at a hash or array, we need to look through them and convert any hashes to objects as well
- case v.class.to_s
- when "Hash" then v = v.to_obj
- when "Array" then v = v.to_obj
- end
- o.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
- o.class.send(:define_method, k, proc{o.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
- o.class.send(:define_method, "#{k}=", proc{|v| o.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
- end
- o
- end
-end
View
5 lib/switchvox/object.rb
@@ -1,5 +0,0 @@
-class Object
- def to_obj
- self
- end
-end
View
11 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+require 'ffaker'
+require 'webmock/rspec'
+require 'switchvox'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+
+ config.order = 'random'
+end
View
71 spec/switchvox/base_spec.rb
@@ -0,0 +1,71 @@
+require 'spec_helper'
+
+describe Switchvox::Base do
+ let(:host) { 'example.com' }
+ let(:switchvox) do
+ stub_request(:head, 'https://' + host + '/json').
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
+ to_return(:status => 200, :body => "", :headers => {})
+ req = Net::HTTP.new(host, '8080')
+ Switchvox::Base.new(host ,Faker::Internet.user_name, 'password')
+ end
+
+ def response_json(body)
+ Hash['response', Hash['result', body]].to_json
+ end
+
+ describe 'parse json and return object' do
+ it 'should test_object' do
+ input = {"string" => "value"}
+ obj = switchvox.json_parse(response_json(input))
+ obj.string.should == 'value'
+ end
+
+ it 'should test test_nested_object' do
+ input = {"dogs" => {"retriever" => "sparky", "basset" => "jennie", "pinscher" => "carver"}}
+ obj = switchvox.json_parse(response_json(input))
+ obj.dogs.retriever.should == 'sparky'
+ end
+
+ it 'should test test_array_of_objects' do
+ input = [{"retriever" => "sparky"}, {"basset" => "jennie"}, {"pinscher" => "carver"}]
+ obj = switchvox.json_parse(response_json(input))
+ obj[0].retriever.should == "sparky"
+ end
+
+ it 'should test test_deep_nest_mixed' do
+ input = {
+ "kennels" => [
+ {
+ "dallas" => [
+ {"name" => "north"},
+ {"name" => "east"},
+ ]
+ },
+ {
+ "frisco" => [
+ {"name" => "south"},
+ {"name" => "west"}
+ ],
+ "company" => "Doggie Daze"
+ }
+ ]
+ }
+ obj = switchvox.json_parse(response_json(input))
+ obj.kennels[1].frisco[1].name.should == 'west'
+ end
+
+ it 'should test test_deep_nest_hash' do
+ input = {
+ "kennels" => {
+ "kennel" => {
+ "dallas" => ["north", "south"],
+ "frisco" => ["east", "west"]
+ }
+ }
+ }
+ obj = switchvox.json_parse(response_json(input))
+ obj.kennels.kennel.dallas[0].should == 'north'
+ end
+ end
+end
View
4 test/test_helper.rb
@@ -1,4 +0,0 @@
-require 'stringio'
-require 'test/unit'
-require 'pp'
-require File.dirname(__FILE__) + '/../lib/switchvox'
View
49 test/test_json_to_obj.rb
@@ -1,49 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestJson2Object < Test::Unit::TestCase
- def test_object
- json = {"string" => "value"}.to_json
- obj = JSON.parse(json).to_obj
- assert_equal("value", obj.string)
- end
-
- def test_nested_object
- json = {"dogs" => {"retriever" => "sparky", "basset" => "jennie", "pinscher" => "carver"}}.to_json
- obj = JSON.parse(json).to_obj
- assert_equal("sparky", obj.dogs.retriever)
- end
-
- def test_array_of_objects
- json = [{"retriever" => "sparky"}, {"basset" => "jennie"}, {"pinscher" => "carver"}].to_json
- obj = JSON.parse(json).to_obj
- assert_equal("sparky", obj[0].retriever)
- end
-
- def test_deep_nest_mixed
- json = {"kennels" => [
- {"dallas" => [
- {"name" => "north"},
- {"name" => "east"},
- ]},
- {"frisco" => [
- {"name" => "south"},
- {"name" => "west"}
- ],
- "company" => "Doggie Daze"
- }
- ]}.to_json
- obj = JSON.parse(json).to_obj
- assert_equal("west", obj.kennels[1].frisco[0].name)
- end
-
- def test_deep_nest_hash
- json = {"kennels" => {
- "kennel" => {
- "dallas" => ["north", "south"],
- "frisco" => ["east", "west"]}}
- }.to_json
- obj = JSON.parse(json).to_obj
- pp obj
- assert_equal("north", obj.kennels.kennel.dallas[0])
- end
-end
View
11 test/test_switchvox.rb
@@ -1,11 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestSwitchvox < Test::Unit::TestCase
-
- def setup
- end
-
- def test_truth
- assert true
- end
-end

0 comments on commit 7b59fe1

Please sign in to comment.
Something went wrong with that request. Please try again.