soda-ruby 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.mkd CHANGED
@@ -1,2 +1,4 @@
1
1
  0.2.0 - Initial Public Release
2
2
  0.2.1 - Point release to update gemspec dependencies
3
+ ...
4
+ 0.2.4 - Point release including tests that are now mocked properly
data/README.mkd CHANGED
@@ -1 +1,18 @@
1
1
  The humble beginnings of a [SODA 2.0](http://dev.socrata.com) wrapper for Ruby.
2
+
3
+ Installation
4
+ ------------
5
+
6
+ SODA is distributed as a gem, which is how it should be used in your app.
7
+
8
+ Include the gem and hashie in your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'soda-ruby', :require => 'soda'
12
+ ```
13
+
14
+ Quick Start
15
+ -----------
16
+ ```ruby
17
+ @client = SODA::Client.new({:domain => "sandbox.demo.socrata.com", :app_token => "K6rLY8NBK0Hgm8QQybFmwIUQw" })
18
+ ```
data/lib/soda/client.rb CHANGED
@@ -6,6 +6,7 @@ require 'uri'
6
6
  require 'json'
7
7
  require 'cgi'
8
8
  require 'hashie'
9
+ require 'curb'
9
10
 
10
11
  module SODA
11
12
  class Client
@@ -15,21 +16,10 @@ module SODA
15
16
 
16
17
  def get(resource, params = {})
17
18
  query = query_string(params)
18
-
19
- # If we didn't get a full path, assume "/resource/"
20
- if !resource.start_with?("/")
21
- resource = "/resource/" + resource
22
- end
23
-
24
- # Check to see if we were given an output type
25
- extension = ".json"
26
- if matches = resource.match(/^(.+)(\.\w+)$/)
27
- resource = matches.captures[0]
28
- extension = matches.captures[1]
29
- end
19
+ path = resource_path(resource)
30
20
 
31
21
  # Create our request
32
- uri = URI.parse("https://#{@config[:domain]}#{resource}#{extension}?#{query}")
22
+ uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")
33
23
  http = Net::HTTP.new(uri.host, uri.port)
34
24
  http.use_ssl = true
35
25
 
@@ -42,37 +32,15 @@ module SODA
42
32
  end
43
33
 
44
34
  # BAM!
45
- response = http.request(request)
46
-
47
- # Check our response code
48
- if response.code != "200"
49
- raise "Error querying \"#{uri.to_s}\": #{response.body}"
50
- else
51
- if extension == ".json"
52
- # Return a bunch of mashes if we're JSON
53
- response = JSON::parse(response.body)
54
- if response.is_a? Array
55
- return response.collect { |r| Hashie::Mash.new(r) }
56
- else
57
- return Hashie::Mash.new(response)
58
- end
59
- else
60
- # We don't partically care, just return the raw body
61
- return response.body
62
- end
63
- end
35
+ return handle_response(http.request(request))
64
36
  end
65
37
 
66
38
  def post(resource, body = "", params = {})
67
39
  query = query_string(params)
68
-
69
- # If we didn't get a full path, assume "/resource/"
70
- if !resource.start_with?("/")
71
- resource = "/resource/" + resource
72
- end
40
+ path = resource_path(resource)
73
41
 
74
42
  # Create our request
75
- uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
43
+ uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")
76
44
  http = Net::HTTP.new(uri.host, uri.port)
77
45
  http.use_ssl = true
78
46
 
@@ -87,23 +55,44 @@ module SODA
87
55
  end
88
56
 
89
57
  # BAM!
90
- response = http.request(request)
58
+ return handle_response(http.request(request))
59
+ end
91
60
 
92
- # Check our response code
93
- if response.code != "200"
94
- raise "Error querying \"#{uri.to_s}\": #{response.body}"
95
- else
96
- # Return a bunch of mashes
97
- response = JSON::parse(response.body)
98
- if response.is_a? Array
99
- return response.collect { |r| Hashie::Mash.new(r) }
100
- else
101
- return Hashie::Mash.new(response)
102
- end
61
+ def put(resource, body = "", params = {})
62
+ query = query_string(params)
63
+ path = resource_path(resource)
64
+
65
+ # Create our request
66
+ uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")
67
+ http = Net::HTTP.new(uri.host, uri.port)
68
+ http.use_ssl = true
69
+
70
+ request = Net::HTTP::Put.new(uri.request_uri)
71
+ request.add_field("X-App-Token", @config[:app_token])
72
+ request.content_type = "application/json"
73
+ request.body = body.to_json
74
+
75
+ # Authenticate if we're supposed to
76
+ if @config[:username]
77
+ request.basic_auth @config[:username], @config[:password]
103
78
  end
79
+
80
+ # BAM!
81
+ return handle_response(http.request(request))
104
82
  end
105
83
 
106
- def put(resource, body = "", params = {})
84
+ def upload_file(path, filename, params = {}, field = 'file', remote_filename = filename)
85
+ # c = Curl::Easy.new("https://#{@config[:domain]}#{path}?#{query_string(params)}")
86
+ # c.multipart_form_post = true
87
+ # c.http_auth_types = :basic
88
+ # c.username = @config[:username]
89
+ # c.password = @config[:password]
90
+ # c.headers['X-App-Token'] = @config[:app_token]
91
+ # c.http_post(Curl::PostField.file(field, filename, remote_filename))
92
+
93
+ # puts c.body_str.inspect
94
+ # return Hashie::Mash.new(JSON.parse(c.body_str))
95
+
107
96
  query = query_string(params)
108
97
 
109
98
  # If we didn't get a full path, assume "/resource/"
@@ -116,7 +105,7 @@ module SODA
116
105
  http = Net::HTTP.new(uri.host, uri.port)
117
106
  http.use_ssl = true
118
107
 
119
- request = Net::HTTP::Put.new(uri.request_uri)
108
+ request = Net::HTTP::Post.new(uri.request_uri)
120
109
  request.add_field("X-App-Token", @config[:app_token])
121
110
  request.content_type = "application/json"
122
111
  request.body = body.to_json
@@ -127,20 +116,32 @@ module SODA
127
116
  end
128
117
 
129
118
  # BAM!
130
- response = http.request(request)
119
+ return handle_response(http.request(request))
120
+ end
131
121
 
132
- # Check our response code
133
- if response.code != "200"
134
- raise "Error querying \"#{uri.to_s}\": #{response.body}"
135
- else
136
- # Return a bunch of mashes
137
- response = JSON::parse(response.body)
138
- if response.is_a? Array
139
- return response.collect { |r| Hashie::Mash.new(r) }
140
- else
141
- return Hashie::Mash.new(response)
142
- end
122
+ def post_form(path, fields = {}, params = {})
123
+ query = query_string(params)
124
+ resource = resoure_path(path)
125
+
126
+ # Create our request
127
+ uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
128
+ http = Net::HTTP.new(uri.host, uri.port)
129
+ http.use_ssl = true
130
+ if @config[:ignore_ssl]
131
+ http.auth.ssl.verify_mode = openssl::ssl::verify_none
132
+ end
133
+
134
+ request = Net::HTTP::Post.new(uri.request_uri)
135
+ request.add_field("X-App-Token", @config[:app_token])
136
+ request.set_form_data(fields)
137
+
138
+ # Authenticate if we're supposed to
139
+ if @config[:username]
140
+ request.basic_auth @config[:username], @config[:password]
143
141
  end
142
+
143
+ # BAM!
144
+ return handle_response(http.request(request))
144
145
  end
145
146
 
146
147
  def delete(resource, body = "", params = {})
@@ -179,9 +180,45 @@ module SODA
179
180
  end
180
181
 
181
182
  private
182
- def query_string(params)
183
+ def query_string(params)
183
184
  # Create query string of escaped key, value pairs
184
185
  return params.collect{ |key, val| "#{key}=#{CGI::escape(val.to_s)}" }.join("&")
185
186
  end
187
+
188
+ def resource_path(resource)
189
+ # If we didn't get a full path, assume "/resource/"
190
+ if !resource.start_with?("/")
191
+ resource = "/resource/" + resource
192
+ end
193
+
194
+ # Check to see if we were given an output type
195
+ extension = ".json"
196
+ if matches = resource.match(/^(.+)(\.\w+)$/)
197
+ resource = matches.captures[0]
198
+ extension = matches.captures[1]
199
+ end
200
+
201
+ return resource + extension
202
+ end
203
+
204
+ def handle_response(response)
205
+ # Check our response code
206
+ if response.code != "200"
207
+ raise "Error in request: #{response.body}"
208
+ else
209
+ if response["Content-Type"].include?("application/json")
210
+ # Return a bunch of mashes if we're JSON
211
+ response = JSON::parse(response.body)
212
+ if response.is_a? Array
213
+ return response.collect { |r| Hashie::Mash.new(r) }
214
+ else
215
+ return Hashie::Mash.new(response)
216
+ end
217
+ else
218
+ # We don't partically care, just return the raw body
219
+ return response.body
220
+ end
221
+ end
222
+ end
186
223
  end
187
224
  end
data/lib/soda/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SODA
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soda-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-06 00:00:00.000000000 Z
12
+ date: 2013-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie