passive_resource 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in passive_resource.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,46 @@
1
+ Use passive resource to create on instances from hashes especially from third party apis.
2
+ Allows for the ability place hash manipulation logic in one neat place
3
+
4
+ Examples:
5
+
6
+ used with a hash or hash like object
7
+ > require 'passive_resource'
8
+ => true
9
+ > class NewClass < PassiveResource::Base
10
+ > def whole_name; "#{first_name} #{last_name}" end
11
+ > end
12
+ => nil
13
+ > instance = NewClass.new(:first_name => 'grady', :last_name => 'griffin')
14
+ => {"first_name"=>"grady", "last_name"=>"griffin"}
15
+ > instance.first_name
16
+ => "grady"
17
+ > instance.whole_name
18
+ => "grady griffin"
19
+
20
+
21
+ Used as with an api returning json
22
+
23
+ > class FacebookProfile < PassiveResource::Base
24
+ > end
25
+ => nil
26
+ > facebook = FacebookProfile.new('https://graph.facebook.com/19292868552')
27
+ => {"id"=>"19292868552", "name"=>"Facebook Platform", "picture"=>"http://profile.ak.fbcdn.net/hprofile-ak-ash2/276791_19292868552_1958181823_s.jpg", "link"=>"http://www.facebook.com/platform", "likes"=>3923792, "category"=>"Product/service", "website"=>"http://developers.facebook.com", "username"=>"platform", "founded"=>"2007", "company_overview"=>"Facebook Platform enables anyone to build social apps on Facebook and the web.", "mission"=>"To make the web more open and social.", "about"=>"We're building the social web. Get the latest here: developers.facebook.com ", "talking_about_count"=>74536}
28
+ > facebook.talking_about_count
29
+ => 74536
30
+ > facebook.founded
31
+ => "2007"
32
+
33
+ Works with code that was previously using hashes
34
+
35
+ > facebook['talking_about_count']
36
+ => 74536
37
+ > facebook[:founded]
38
+ => "2007"
39
+
40
+ Objects are nested automatically
41
+ > instance = NewClass.new(:locations => [{:city => 'Orlando', :state => "FL"}, {:city => 'Greenwood', :state => "SC"}])
42
+ => {"locations"=>[{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]}
43
+ > instance.locations
44
+ => [{"city"=>"Orlando", "state"=>"FL"}, {"city"=>"Greenwood", "state"=>"SC"}]
45
+ > instance.locations.first.city
46
+ => "Orlando"
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ # If you want to make this the default task
8
+ task :default => :spec
@@ -0,0 +1,72 @@
1
+ module PassiveResource
2
+ class Base
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'active_support/json'
5
+ require 'rest_client'
6
+
7
+ HASH_METHODS = (ActiveSupport::HashWithIndifferentAccess.new.methods - Object.new.methods)
8
+
9
+ def initialize(p = {})
10
+ p = load_from_url(p) if p.is_a?(String)
11
+ if self.class.nestable?(p)
12
+ @seedling = ActiveSupport::HashWithIndifferentAccess.new(p)
13
+ elsif self.class.collection?(p)
14
+ @seedling = self.class.many(p)
15
+ else
16
+ raise PassiveResource::InvalidSeedlingException, "A hash like object or a collection of hash like objects is required"
17
+ end
18
+ end
19
+
20
+ def seedling
21
+ @seedling
22
+ end
23
+
24
+ def respond_to?(method)
25
+ return true if super
26
+ accessor = method.to_s.gsub(/\=$/,'')
27
+ @seedling.has_key?(accessor) or HASH_METHODS.include?(method)
28
+ end
29
+
30
+ def [](key)
31
+ if self.class.nestable?(@seedling[key])
32
+ @seedling[key] = self.class.new(@seedling[key])
33
+ elsif self.class.collection?(@seedling[key])
34
+ @seedling[key] = self.class.many(@seedling[key])
35
+ end
36
+ @seedling[key]
37
+ end
38
+
39
+ def self.many(ary)
40
+ ary = [ary].compact.flatten
41
+ ary.collect {|hash| nestable?(hash) ? new(hash) : hash }
42
+ end
43
+
44
+ def method_missing(method_sym, *arguments, &block)
45
+ accessor = method_sym.to_s.gsub(/\=$/,'')
46
+ if @seedling.has_key?(accessor)
47
+ accessor == method_sym.to_s ? self[accessor] : @seedling[accessor] = arguments.first
48
+ elsif HASH_METHODS.include?(method_sym)
49
+ @seedling.send(method_sym, *arguments, &block)
50
+ else
51
+ super
52
+ end
53
+ end
54
+
55
+ def inspect
56
+ @seedling.inspect
57
+ end
58
+
59
+ def load_from_url(url)
60
+ rtn = RestClient.get(url)
61
+ JSON.parse(rtn)
62
+ end
63
+
64
+ def self.nestable?(obj)
65
+ ['ActiveSupport::HashWithIndifferentAccess', 'HashWithIndifferentAccess', 'Hash'].include?(obj.class.to_s)
66
+ end
67
+
68
+ def self.collection?(obj)
69
+ ['Array', 'WillPaginate::Collection'].include?(obj.class.to_s)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module PassiveResource
2
+ class InvalidSeedlingException < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module PassiveResource
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "passive_resource/version"
2
+ require "passive_resource/base"
3
+ require "passive_resource/error"
4
+
5
+ module PassiveResource
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "passive_resource/version"
4
+ require "passive_resource/base"
5
+ require "passive_resource/error"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "passive_resource"
9
+ s.version = PassiveResource::VERSION
10
+ s.authors = ["Grady Griffin"]
11
+ s.email = ["gradyg@izea.com"]
12
+ s.homepage = ""
13
+ s.summary = %q{Simple objects from json}
14
+ s.description = %q{Creates Ruby objects from JSON requested from apis or hashes}
15
+
16
+ s.rubyforge_project = "passive_resource"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ # specify any dependencies here; for example:
24
+ # s.add_development_dependency "rspec"
25
+ # s.add_runtime_dependency "rest-client"
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_dependency("activesupport", '> 2.3')
28
+ s.add_dependency "rest-client"
29
+ s.add_dependency "rspec"
30
+ else
31
+ s.add_runtime_dependency("activesupport", '> 2.3')
32
+ s.add_runtime_dependency "rest-client"
33
+ s.add_development_dependency "rspec"
34
+ end
35
+ end
data/spec/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ rspec --configure autotest
4
+
data/spec/base_spec.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ describe PassiveResource::Base, "#new" do
3
+ it "should return a PassiveResource::Base instance" do
4
+ instance = PassiveResource::Base.new
5
+ instance.class.to_s.should eq('PassiveResource::Base')
6
+ end
7
+
8
+ it "should define methods as specified in the hash" do
9
+ instance = PassiveResource::Base.new(:name => 'grady')
10
+ instance.name.should eq('grady')
11
+ end
12
+
13
+ it "should nest PassiveResource::Base objects" do
14
+ instance = PassiveResource::Base.new(:location => {:city => 'Orlando'})
15
+ instance.location.class.to_s.should eq('PassiveResource::Base')
16
+ end
17
+
18
+ it "should nest PassiveResource::Base objects within collections" do
19
+ instance = PassiveResource::Base.new(:cities => [{:city => 'Orlando'}, {:city => 'Greenwood'}])
20
+ instance.cities.all? {|item| item.is_a?(PassiveResource::Base)}.should eq(true)
21
+ end
22
+
23
+ it "should respond to Hash methods" do
24
+ instance = PassiveResource::Base.new(:name => 'grady')
25
+ PassiveResource::Base::HASH_METHODS.all? {|m| instance.respond_to?(m)}.should eq(true)
26
+ end
27
+
28
+ it "should respond to created methods" do
29
+ instance = PassiveResource::Base.new(:name => 'grady')
30
+ instance.respond_to?('name').should eq(true)
31
+ end
32
+ end
33
+
34
+ describe PassiveResource::Base, "#seedling" do
35
+ it "should have a seedling equal to the hash" do
36
+ instance = PassiveResource::Base.new(:name => 'grady')
37
+ instance.seedling['name'].should eq('grady')
38
+ end
39
+ end
40
+
41
+ describe PassiveResource::Base, "#many" do
42
+ it "should return a collection of PassiveResource::Base objects" do
43
+ instances = PassiveResource::Base.many(:cities => [{:city => 'Orlando'}, {:city => 'Greenwood'}])
44
+ instances.all? {|item| item.is_a?(PassiveResource::Base)}.should eq(true)
45
+ end
46
+ end
47
+
48
+ describe PassiveResource::Base, "#inspect" do
49
+ it "should return the seedling to_s" do
50
+ instance = PassiveResource::Base.new(:name => 'grady')
51
+ instance.inspect.should eq("{\"name\"=>\"grady\"}")
52
+ end
53
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'passive_resource'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passive_resource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Grady Griffin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70256019607440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>'
20
+ - !ruby/object:Gem::Version
21
+ version: '2.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70256019607440
25
+ - !ruby/object:Gem::Dependency
26
+ name: rest-client
27
+ requirement: &70256019606840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70256019606840
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70256020759600 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70256020759600
47
+ description: Creates Ruby objects from JSON requested from apis or hashes
48
+ email:
49
+ - gradyg@izea.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - README
57
+ - Rakefile
58
+ - lib/passive_resource.rb
59
+ - lib/passive_resource/base.rb
60
+ - lib/passive_resource/error.rb
61
+ - lib/passive_resource/version.rb
62
+ - passive_resource.gemspec
63
+ - spec/.rspec
64
+ - spec/base_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: ''
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project: passive_resource
86
+ rubygems_version: 1.8.15
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Simple objects from json
90
+ test_files:
91
+ - spec/base_spec.rb
92
+ - spec/spec_helper.rb