soda-ruby 0.2.11 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,3 +5,6 @@
5
5
  0.2.6 - Code cleanup from @raykao
6
6
  0.2.7 - Removing dead code and an un-needed dependency
7
7
  0.2.8 - Removing the nesting limit on the JSON parser
8
+ 0.2.10 - Removing a nesting limit that we missed
9
+ 0.2.11 - We weren't properly handling an empty payload response
10
+ 0.2.12 - Added a global option to disable SSL checking from https://github.com/socrata/soda-ruby/pull/7
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- # Just a simple wrapper for SODA 2.0
2
+ #
3
+ # A Ruby client to the RESTful Socrata Open Data API
4
+ #
5
+ # For more details, check out http://socrata.github.io/soda-ruby
6
+ #
3
7
 
4
8
  require 'net/https'
5
9
  require 'uri'
@@ -9,22 +13,103 @@ require 'hashie'
9
13
 
10
14
  module SODA
11
15
  class Client
16
+ ##
17
+ #
18
+ # Creates a new SODA client.
19
+ #
20
+ # * +config+ - A hash of the options to initialize the client with
21
+ #
22
+ # == Config Options
23
+ #
24
+ # * +:domain+ - The domain you want to access
25
+ # * +:username+ - Your Socrata username (optional, only necessary for modifying data)
26
+ # * +:password+ - Your Socrata password (optional, only necessary for modifying data)
27
+ # * +:app_token+ - Your Socrata application token (register at http://dev.socrata.com/register)
28
+ # * +:ignore_ssl+ - Ignore ssl errors (defaults to false)
29
+ #
30
+ # Returns a SODA::Client instance.
31
+ #
32
+ # == Example
33
+ #
34
+ # client = SODA::Client.new({ :domain => "data.agency.gov", :app_token => "CGxarwoQlgQSev4zyUh5aR5J3" })
35
+ #
12
36
  def initialize(config = {})
13
37
  @config = config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
14
38
  end
15
39
 
40
+ ##
41
+ #
42
+ # Retrieve a resource via an HTTP GET request.
43
+ #
44
+ # * +resource+ - The resource identifier or path.
45
+ # * +params+ - A hash of the URL parameters you want to pass
46
+ #
47
+ # Returns a Hashie::Mash that you can interact with as a Hash if you want.
48
+ #
49
+ # == Example
50
+ #
51
+ # response = client.get("644b-gaut", { :firstname => "OPRAH", :lastname => "WINFREY", "$limit" => 5 })
52
+ #
16
53
  def get(resource, params = {})
17
54
  connection(:get, resource, nil, params)
18
55
  end
19
56
 
57
+ ##
58
+ #
59
+ # Update a resource via an HTTP POST request.
60
+ #
61
+ # Requires an authenticated client (both +:username+ and +:password+ passed into the +options+ hash)
62
+ #
63
+ # * +resource+ - The resource identifier or path.
64
+ # * +body+ - The payload to POST. Will be converted to JSON (optional).
65
+ # * +params+ - A hash of the URL parameters you want to pass (optional).
66
+ #
67
+ # Returns a Hashie::Mash that you can interact with as a Hash if you want.
68
+ #
69
+ # == Example
70
+ #
71
+ # response = client.post("644b-gaut", [{ :firstname => "OPRAH", :lastname => "WINFREY" }])
72
+ #
20
73
  def post(resource, body = nil, params = {})
21
74
  connection(:post, resource, body, params)
22
75
  end
23
76
 
77
+ ##
78
+ #
79
+ # Replaces a resource via an HTTP PUT request.
80
+ #
81
+ # Requires an authenticated client (both +:username+ and +:password+ passed into the +options+ hash)
82
+ #
83
+ # * +resource+ - The resource identifier or path.
84
+ # * +body+ - The payload to POST. Will be converted to JSON (optional).
85
+ # * +params+ - A hash of the URL parameters you want to pass (optional).
86
+ #
87
+ # Returns a Hashie::Mash that you can interact with as a Hash if you want.
88
+ #
89
+ # == Example
90
+ #
91
+ # response = client.put("644b-gaut", [{ :firstname => "OPRAH", :lastname => "WINFREY" }])
92
+ #
24
93
  def put(resource, body = nil, params = {})
25
94
  connection(:put, resource, body, params)
26
95
  end
27
96
 
97
+ ##
98
+ #
99
+ # Deletes a resource via an HTTP DELETE request.
100
+ #
101
+ # Requires an authenticated client (both +:username+ and +:password+ passed into the +options+ hash)
102
+ #
103
+ # * +resource+ - The resource identifier or path.
104
+ # * +body+ - The payload to send with the DELETE. Will be converted to JSON (optional).
105
+ # * +params+ - A hash of the URL parameters you want to pass (optional).
106
+ #
107
+ # Returns a Hashie::Mash that you can interact with as a Hash if you want.
108
+ #
109
+ # == Example
110
+ #
111
+ # response = client.delete("644b-gaut")
112
+ #
28
113
  def delete(resource, body = nil, params = {})
29
114
  connection(:delete, resource, body, params)
30
115
  end
@@ -35,11 +120,7 @@ module SODA
35
120
 
36
121
  # Create our request
37
122
  uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
38
- http = Net::HTTP.new(uri.host, uri.port)
39
- http.use_ssl = true
40
- if @config[:ignore_ssl]
41
- http.auth.ssl.verify_mode = openssl::ssl::verify_none
42
- end
123
+ http = build_http_client(uri.host, uri.port)
43
124
 
44
125
  request = Net::HTTP::Post.new(uri.request_uri)
45
126
  request.add_field("X-App-Token", @config[:app_token])
@@ -107,8 +188,7 @@ module SODA
107
188
 
108
189
  uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")
109
190
 
110
- http = Net::HTTP.new(uri.host, uri.port)
111
- http.use_ssl = true
191
+ http = build_http_client(uri.host, uri.port)
112
192
  request = eval("Net::HTTP::#{method.capitalize}").new(uri.request_uri)
113
193
  request.add_field("X-App-Token", @config[:app_token])
114
194
 
@@ -137,5 +217,14 @@ module SODA
137
217
  end
138
218
  end
139
219
 
220
+ def build_http_client(host, port)
221
+ http = Net::HTTP.new(host, port)
222
+ http.use_ssl = true
223
+ if @config[:ignore_ssl]
224
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
225
+ end
226
+ http
227
+ end
228
+
140
229
  end
141
230
  end
@@ -1,3 +1,3 @@
1
1
  module SODA
2
- VERSION = "0.2.11"
2
+ VERSION = "0.2.12"
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.11
4
+ version: 0.2.12
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: 2014-03-18 00:00:00.000000000 Z
12
+ date: 2014-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie