soda-ruby 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 0.2.0 - Initial Public Release
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2012 Socrata, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ The humble beginnings of a [SODA 2.0](http://dev.socrata.com) wrapper for Ruby.
@@ -0,0 +1 @@
1
+ require "soda/client"
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env ruby
2
+ # Just a simple wrapper for SODA 2.0
3
+
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'json'
7
+ require 'cgi'
8
+ require 'hashie'
9
+
10
+ module SODA
11
+ class Client
12
+ def initialize(config = {})
13
+ @config = config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
14
+ end
15
+
16
+ def get(resource, params = {})
17
+ # Create query string of escaped key, value pairs
18
+ query = params.collect{ |key, val| "#{key}=#{CGI::escape(val.to_s)}" }.join("&")
19
+
20
+ # If we didn't get a full path, assume "/resource/"
21
+ if !resource.start_with?("/")
22
+ resource = "/resource/" + resource
23
+ end
24
+
25
+ # Create our request
26
+ uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
27
+ http = Net::HTTP.new(uri.host, uri.port)
28
+ http.use_ssl = true
29
+
30
+ request = Net::HTTP::Get.new(uri.request_uri)
31
+ request.add_field("X-App-Token", @config[:app_token])
32
+
33
+ # Authenticate if we're supposed to
34
+ if @config[:username]
35
+ request.basic_auth @config[:username], @config[:password]
36
+ end
37
+
38
+ # BAM!
39
+ response = http.request(request)
40
+
41
+ # Check our response code
42
+ if response.code != "200"
43
+ raise "Error querying \"#{uri.to_s}\": #{response.body}"
44
+ else
45
+ # Return a bunch of mashes
46
+ response = JSON::parse(response.body)
47
+ if response.is_a? Array
48
+ return response.collect { |r| Hashie::Mash.new(r) }
49
+ else
50
+ return Hashie::Mash.new(response)
51
+ end
52
+ end
53
+ end
54
+
55
+ def post(resource, body = "", params = {})
56
+ # Create query string of escaped key, value pairs
57
+ query = params.collect{ |key, val| "#{key}=#{CGI::escape(val)}" }.join("&")
58
+
59
+ # If we didn't get a full path, assume "/resource/"
60
+ if !resource.start_with?("/")
61
+ resource = "/resource/" + resource
62
+ end
63
+
64
+ # Create our request
65
+ uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ http.use_ssl = true
68
+
69
+ request = Net::HTTP::Post.new(uri.request_uri)
70
+ request.add_field("X-App-Token", @config[:app_token])
71
+ request.content_type = "application/json"
72
+ request.body = body.to_json
73
+
74
+ # Authenticate if we're supposed to
75
+ if @config[:username]
76
+ request.basic_auth @config[:username], @config[:password]
77
+ end
78
+
79
+ # BAM!
80
+ response = http.request(request)
81
+
82
+ # Check our response code
83
+ if response.code != "200"
84
+ raise "Error querying \"#{uri.to_s}\": #{response.body}"
85
+ else
86
+ # Return a bunch of mashes
87
+ response = JSON::parse(response.body)
88
+ if response.is_a? Array
89
+ return response.collect { |r| Hashie::Mash.new(r) }
90
+ else
91
+ return Hashie::Mash.new(response)
92
+ end
93
+ end
94
+ end
95
+
96
+ def put(resource, body = "", params = {})
97
+ # Create query string of escaped key, value pairs
98
+ query = params.collect{ |key, val| "#{key}=#{CGI::escape(val)}" }.join("&")
99
+
100
+ # If we didn't get a full path, assume "/resource/"
101
+ if !resource.start_with?("/")
102
+ resource = "/resource/" + resource
103
+ end
104
+
105
+ # Create our request
106
+ uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
107
+ http = Net::HTTP.new(uri.host, uri.port)
108
+ http.use_ssl = true
109
+
110
+ request = Net::HTTP::Put.new(uri.request_uri)
111
+ request.add_field("X-App-Token", @config[:app_token])
112
+ request.content_type = "application/json"
113
+ request.body = body.to_json
114
+
115
+ # Authenticate if we're supposed to
116
+ if @config[:username]
117
+ request.basic_auth @config[:username], @config[:password]
118
+ end
119
+
120
+ # BAM!
121
+ response = http.request(request)
122
+
123
+ # Check our response code
124
+ if response.code != "200"
125
+ raise "Error querying \"#{uri.to_s}\": #{response.body}"
126
+ else
127
+ # Return a bunch of mashes
128
+ response = JSON::parse(response.body)
129
+ if response.is_a? Array
130
+ return response.collect { |r| Hashie::Mash.new(r) }
131
+ else
132
+ return Hashie::Mash.new(response)
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,3 @@
1
+ module SODA
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soda-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Metcalf
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple wrapper for SODA 2.0
15
+ email: chris.metcalf@socrata.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/soda/client.rb
21
+ - lib/soda/version.rb
22
+ - lib/soda.rb
23
+ - LICENSE
24
+ - CHANGELOG.mkd
25
+ - README.mkd
26
+ homepage: http://github.com/socrata/soda-ruby
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.6
44
+ requirements: []
45
+ rubyforge_project: soda-ruby
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Ruby for SODA 2.0
50
+ test_files: []