sodacan 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +6 -0
- data/SodaCan.gemspec +26 -0
- data/lib/sodacan/base.rb +102 -0
- data/lib/sodacan/location.rb +12 -0
- data/lib/sodacan/version.rb +3 -0
- data/lib/sodacan.rb +10 -0
- data/spec/sodacan_spec.rb +17 -0
- data/spec/spec_helper.rb +8 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1e2878a19c2e3c2e2c5c2a3a53fd3757626af587
|
4
|
+
data.tar.gz: 4335d7c5a9656b9c88a30737f8b607a62cd7f1dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f430404f72766743419210026778ef914e02df4724b04dfec16dff25cdafff4a31d9b05d8834be493635af435503745af6f6040236a919a2b77db16fa30c005
|
7
|
+
data.tar.gz: 64ea068ce0a131ee770cf5279776f938a3b6d2e2582656830d63f293d6d101d6fa3f8da2577a3a590d19a81b731aeec897932a2667cceed3cedabfcfee669d09
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 2.0.0@opendata --create
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
coderay (1.0.9)
|
5
|
+
diff-lcs (1.2.2)
|
6
|
+
json (1.7.7)
|
7
|
+
method_source (0.8.1)
|
8
|
+
mime-types (1.22)
|
9
|
+
pry (0.9.12)
|
10
|
+
coderay (~> 1.0.5)
|
11
|
+
method_source (~> 0.8)
|
12
|
+
slop (~> 3.4)
|
13
|
+
rest-client (1.6.7)
|
14
|
+
mime-types (>= 1.16)
|
15
|
+
rspec (2.13.0)
|
16
|
+
rspec-core (~> 2.13.0)
|
17
|
+
rspec-expectations (~> 2.13.0)
|
18
|
+
rspec-mocks (~> 2.13.0)
|
19
|
+
rspec-core (2.13.1)
|
20
|
+
rspec-expectations (2.13.0)
|
21
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
22
|
+
rspec-mocks (2.13.1)
|
23
|
+
slop (3.4.4)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
json
|
30
|
+
pry
|
31
|
+
rest-client
|
32
|
+
rspec (= 2.13.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Trevor John, tjohn
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#SodaCan
|
2
|
+
|
3
|
+
An ActiveRecord-like interface to databases with a JSON based Soda2 interface, and example of which is the NYC OpenData set.
|
4
|
+
|
5
|
+
##Usage
|
6
|
+
|
7
|
+
A new database should be added to an application by subclassing SodaCan::Base
|
8
|
+
|
9
|
+
class MyDB < SodaCan::Base
|
10
|
+
end
|
11
|
+
|
12
|
+
This new class comes with a host of query methods out of the box. Before querying the database, connect it to the Soda2 database
|
13
|
+
|
14
|
+
MyDB.connect [uri string here]
|
15
|
+
|
16
|
+
##Query Methods
|
17
|
+
|
18
|
+
All query methods are class methods on a DB class. Query methods return instances of the DB class, just like ActiveRecord.
|
19
|
+
|
20
|
+
###.many
|
21
|
+
|
22
|
+
Returns an array of up to the first 1000 instances in the database
|
23
|
+
|
24
|
+
###.execute _query_
|
25
|
+
|
26
|
+
Returns the instances retrieved as a result of sending the query to the database
|
27
|
+
|
28
|
+
###.find _id_
|
29
|
+
|
30
|
+
Returns the instance in the database with the given ID, nil if the id is not found
|
31
|
+
|
32
|
+
###.search _term_
|
33
|
+
|
34
|
+
Returns an array of any instances that have the term string as a value for any field. Search works only for full tokens in the database. "ban" will match "ban on apples" but not "bananas are good"
|
35
|
+
|
36
|
+
###.where _params_
|
37
|
+
|
38
|
+
Returns an array of instances where the field represented by the key in the params hash has the value represented by the value in the params hash
|
39
|
+
|
40
|
+
##Instance methods
|
41
|
+
|
42
|
+
###\#\_fields
|
43
|
+
|
44
|
+
Returns a hash of field names and types found in the instance
|
45
|
+
|
46
|
+
###\#accessors
|
47
|
+
|
48
|
+
Depending upon the record being accessed a number of accessor methods are created for the instance, allowing access to the fields.
|
49
|
+
|
50
|
+
##Example
|
51
|
+
|
52
|
+
class MyDB < SodaCan::Base
|
53
|
+
end
|
54
|
+
|
55
|
+
MyDB.connect 'http://someurl.com/'
|
56
|
+
|
57
|
+
first = MyDB.find 1
|
58
|
+
bananas = MyDB.search "banana"
|
59
|
+
bananas.first.calories # calorie count of the first banana
|
60
|
+
fruit = MyDB.where(food_group: 'fruit')
|
data/Rakefile
ADDED
data/SodaCan.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sodacan/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sodacan"
|
8
|
+
spec.version = SodaCan::VERSION
|
9
|
+
spec.authors = ["Trevor John, tjohn", "Tyler Heck, theck01"]
|
10
|
+
spec.email = ["tyler.heck@gmail.com"]
|
11
|
+
spec.description = %q{Wrapper to make ActiveRecord type queries on Soda2 interfaces.}
|
12
|
+
spec.summary = %q{SOQL Wrapper for JSON Soda 2 interfaces}
|
13
|
+
spec.homepage = "http://rubygems.org/gems/sodacan"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "json", ["= 1.7.7"]
|
25
|
+
spec.add_runtime_dependency "rest-client", ["= 1.6.7"]
|
26
|
+
end
|
data/lib/sodacan/base.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
module SodaCan
|
2
|
+
class Base
|
3
|
+
|
4
|
+
def initialize (data, fields)
|
5
|
+
|
6
|
+
used_fields = {}
|
7
|
+
data.each do |key,val|
|
8
|
+
method_name = key.to_sym
|
9
|
+
|
10
|
+
case fields[method_name]
|
11
|
+
when "number"
|
12
|
+
val = BigDecimal(val)
|
13
|
+
when "double"
|
14
|
+
val = Float(val)
|
15
|
+
when "boolean"
|
16
|
+
if val =~ /(true|1)/
|
17
|
+
val = true
|
18
|
+
else
|
19
|
+
val = false
|
20
|
+
end
|
21
|
+
when "location"
|
22
|
+
val = Location.new val
|
23
|
+
when "calendar_date"
|
24
|
+
val = Time.new val
|
25
|
+
end
|
26
|
+
|
27
|
+
used_fields[method_name] = fields[method_name]
|
28
|
+
define_singleton_method(method_name){ val }
|
29
|
+
end
|
30
|
+
|
31
|
+
define_singleton_method(:_fields){ used_fields }
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
class << self
|
36
|
+
|
37
|
+
@@url = nil
|
38
|
+
|
39
|
+
def many
|
40
|
+
send_query @@url
|
41
|
+
end
|
42
|
+
|
43
|
+
def connect (url)
|
44
|
+
@@url = url.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def execute (soql)
|
48
|
+
query = "#{ @@url }?$query=#{ soql }"
|
49
|
+
send_query query
|
50
|
+
end
|
51
|
+
|
52
|
+
def find (id)
|
53
|
+
where(id: id).first
|
54
|
+
end
|
55
|
+
|
56
|
+
def search (term)
|
57
|
+
query = "#{ @@url }?$q=#{ term }"
|
58
|
+
send_query query
|
59
|
+
end
|
60
|
+
|
61
|
+
def where (params)
|
62
|
+
query = "#{ @@url }?"
|
63
|
+
|
64
|
+
if params.has_key?(:id)
|
65
|
+
query << "$limit=1&$offset=#{ params[:id].to_i-1 }"
|
66
|
+
params.delete(:id)
|
67
|
+
end
|
68
|
+
|
69
|
+
unless params.empty?
|
70
|
+
query << "$where="
|
71
|
+
|
72
|
+
query = params.to_a.reduce(query) do |q,pair|
|
73
|
+
if pair[1].is_a? Numeric
|
74
|
+
q << "#{ pair[0] }=#{ pair[1] }&"
|
75
|
+
else
|
76
|
+
q << "#{ pair[0] }=\"#{ pair[1] }\"&"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
query = query[0..-2]
|
80
|
+
end
|
81
|
+
|
82
|
+
send_query query
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
protected
|
87
|
+
|
88
|
+
def send_query (query)
|
89
|
+
response = RestClient.get query
|
90
|
+
|
91
|
+
field_names = JSON.parse(response.headers[:x_soda2_fields])
|
92
|
+
field_types = JSON.parse(response.headers[:x_soda2_types])
|
93
|
+
fields = field_names.zip(field_types).reduce({}) do |memo, pair|
|
94
|
+
memo[pair[0].to_sym] = pair[1]
|
95
|
+
memo
|
96
|
+
end
|
97
|
+
|
98
|
+
JSON.parse(response.body).map{ |data_hash| self.new(data_hash,fields) }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module SodaCan
|
2
|
+
class Location
|
3
|
+
def initialize (params)
|
4
|
+
lat = nil
|
5
|
+
lat = Float(params['latitude']) if params['latitude']
|
6
|
+
long = nil
|
7
|
+
long = Float(params['longtitude']) if params['longitude']
|
8
|
+
define_singleton_method(:latitude){ lat }
|
9
|
+
define_singleton_method(:longitude){ long }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/sodacan.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SodaCan::Base do
|
4
|
+
before do
|
5
|
+
end
|
6
|
+
|
7
|
+
describe ".connect" do
|
8
|
+
it "sets the url" do
|
9
|
+
@url = "some_url"
|
10
|
+
expect {
|
11
|
+
puts Market.connect( @url )
|
12
|
+
}.to change { Market.instance_variables }.to eq(@url)
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sodacan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Trevor John, tjohn
|
8
|
+
- Tyler Heck, theck01
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: json
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.7.7
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.7.7
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rest-client
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.6.7
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.6.7
|
70
|
+
description: Wrapper to make ActiveRecord type queries on Soda2 interfaces.
|
71
|
+
email:
|
72
|
+
- tyler.heck@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .rspec
|
78
|
+
- .rvmrc
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- SodaCan.gemspec
|
85
|
+
- lib/sodacan.rb
|
86
|
+
- lib/sodacan/base.rb
|
87
|
+
- lib/sodacan/location.rb
|
88
|
+
- lib/sodacan/version.rb
|
89
|
+
- spec/sodacan_spec.rb
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
homepage: http://rubygems.org/gems/sodacan
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.0.2
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: SOQL Wrapper for JSON Soda 2 interfaces
|
115
|
+
test_files:
|
116
|
+
- spec/sodacan_spec.rb
|
117
|
+
- spec/spec_helper.rb
|