Develop #2

Merged
merged 29 commits into from Dec 10, 2012

Projects

None yet

3 participants

@rimian

No description provided.

@rimian rimian commented on the diff Dec 2, 2012
lib/switchvox/base.rb
+ 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) }
+ o = OpenStruct.new
+ o.marshal_load Hash[arg.map { |k, v| [k.to_sym, v] }]
+ return o
+ elsif arg.is_a? Array
+ arg.map! { |v| convert_to_obj(v) }
+ else
+ arg
+ end
+ end
@rimian
rimian Dec 2, 2012

I wanna refactor this out into its own concern.

@seangeo
seangeo Dec 2, 2012

Is it used anywhere else?

@rimian
rimian Dec 2, 2012

Nah, but it seems like something the base class should delegate to. I'm OK with leaving it.

@rimian rimian commented on the diff Dec 2, 2012
test/test_json_to_obj.rb
-
- 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)
@rimian
rimian Dec 2, 2012

Note: This spec is wrong, yes? It should be "south". Or am I missing something? This error does not persist in the new code.

@mikel
mikel Dec 3, 2012

Good catch, you are right.

@seangeo seangeo and 1 other commented on an outdated diff Dec 2, 2012
lib/switchvox/base.rb
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) }
+ o = OpenStruct.new
@seangeo
seangeo Dec 2, 2012

You should be able to pass the hash in as an argument to the constructor instead of using marshal_load.

@rimian
rimian Dec 2, 2012

Done! Much less code. No longer need the keys to symbols either.

@mikel mikel commented on an outdated diff Dec 3, 2012
-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
@mikel
mikel Dec 3, 2012

Why are you deleting this?

@mikel mikel commented on an outdated diff Dec 3, 2012
@@ -1,4 +0,0 @@
-=== 0.0.1 2010-04-22
-
-* 1 major enhancement:
- * Initial release
@mikel
mikel Dec 3, 2012

Why are you deleting this?

@mikel
reinteractive Open Source member

Looks good Rimian, please don't delete the Manifest or History though.

👍

@mikel mikel commented on the diff Dec 3, 2012
Gemfile.lock
@@ -0,0 +1,26 @@
+GEM
@mikel
mikel Dec 3, 2012

OK, remove this file from git and add it to .gitignore. See http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/ for reference

@mikel mikel commented on an outdated diff Dec 3, 2012
@@ -0,0 +1,7 @@
+source :rubygems
+
+group :test do
+ gem 'rspec'
+ gem 'ffaker'
+ gem 'webmock'
+end
@mikel
mikel Dec 3, 2012

Should add loose (minor) version numbers to these.

@mikel mikel merged commit 7b59fe1 into master Dec 10, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment