soda-ruby 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/soda/client.rb +53 -6
  2. data/lib/soda/version.rb +1 -1
  3. metadata +3 -3
data/lib/soda/client.rb CHANGED
@@ -21,8 +21,15 @@ module SODA
21
21
  resource = "/resource/" + resource
22
22
  end
23
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
30
+
24
31
  # Create our request
25
- uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
32
+ uri = URI.parse("https://#{@config[:domain]}#{resource}#{extension}?#{query}")
26
33
  http = Net::HTTP.new(uri.host, uri.port)
27
34
  http.use_ssl = true
28
35
 
@@ -41,12 +48,17 @@ module SODA
41
48
  if response.code != "200"
42
49
  raise "Error querying \"#{uri.to_s}\": #{response.body}"
43
50
  else
44
- # Return a bunch of mashes
45
- response = JSON::parse(response.body)
46
- if response.is_a? Array
47
- return response.collect { |r| Hashie::Mash.new(r) }
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
48
59
  else
49
- return Hashie::Mash.new(response)
60
+ # We don't partically care, just return the raw body
61
+ return response.body
50
62
  end
51
63
  end
52
64
  end
@@ -131,6 +143,41 @@ module SODA
131
143
  end
132
144
  end
133
145
 
146
+ def delete(resource, body = "", params = {})
147
+ query = query_string(params)
148
+
149
+ # If we didn't get a full path, assume "/resource/"
150
+ if !resource.start_with?("/")
151
+ resource = "/resource/" + resource
152
+ end
153
+
154
+ # Create our request
155
+ uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
156
+ http = Net::HTTP.new(uri.host, uri.port)
157
+ http.use_ssl = true
158
+
159
+ request = Net::HTTP::Delete.new(uri.request_uri)
160
+ request.add_field("X-App-Token", @config[:app_token])
161
+ request.content_type = "application/json"
162
+ request.body = body.to_json
163
+
164
+ # Authenticate if we're supposed to
165
+ if @config[:username]
166
+ request.basic_auth @config[:username], @config[:password]
167
+ end
168
+
169
+ # BAM!
170
+ response = http.request(request)
171
+
172
+ # Check our response code
173
+ if response.code != "200"
174
+ raise "Error querying \"#{uri.to_s}\": #{response.body}"
175
+ else
176
+ # Return a bunch of mashes
177
+ return response
178
+ end
179
+ end
180
+
134
181
  private
135
182
  def query_string(params)
136
183
  # Create query string of escaped key, value pairs
data/lib/soda/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SODA
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.4"
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.2
4
+ version: 0.2.4
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: 2012-12-13 00:00:00.000000000 Z
12
+ date: 2013-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  version: 1.3.6
60
60
  requirements: []
61
61
  rubyforge_project: soda-ruby
62
- rubygems_version: 1.8.24
62
+ rubygems_version: 1.8.25
63
63
  signing_key:
64
64
  specification_version: 3
65
65
  summary: Ruby for SODA 2.0