Class: SODA::Client
- Inherits:
-
Object
- Object
- SODA::Client
- Defined in:
- lib/soda/client.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#delete(resource, body = nil, params = {}) ⇒ Object
Deletes a resource via an HTTP DELETE request.
-
#get(resource, params = {}) ⇒ Object
Retrieve a resource via an HTTP GET request.
-
#initialize(config = {}) ⇒ Client
constructor
Creates a new SODA client.
-
#post(resource, body = nil, params = {}) ⇒ Object
Update a resource via an HTTP POST request.
- #post_form(resource, body = {}, params = {}) ⇒ Object
-
#put(resource, body = nil, params = {}) ⇒ Object
Replaces a resource via an HTTP PUT request.
Constructor Details
#initialize(config = {}) ⇒ Client
Creates a new SODA client.
-
config- A hash of the options to initialize the client with
Config Options
-
:domain- The domain you want to access -
:username- Your Socrata username (optional, only necessary for modifying data) -
:password- Your Socrata password (optional, only necessary for modifying data) -
:app_token- Your Socrata application token (register at dev.socrata.com/register) -
:access_token- Your Socrata OAuth token (optional, dev.socrata.com/docs/authentication.html) -
:ignore_ssl- Ignore SSL errors, which is very unsafe and only should be done in desperate circumstances (defaults to false) -
:debug_stream- Set an output stream for debugging
Returns a SODA::Client instance.
Example
client = SODA::Client.new({ :domain => "data.agency.gov", :app_token => "CGxarwoQlgQSev4zyUh5aR5J3" })
52 53 54 55 |
# File 'lib/soda/client.rb', line 52 def initialize(config = {}) @config = Hashie.symbolize_keys! config @user_agent = SODA::Client.generate_user_agent end |
Class Method Details
.generate_user_agent ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/soda/client.rb', line 21 def generate_user_agent if Gem.win_platform? "soda-ruby/#{SODA::VERSION} (Windows; Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})" else "soda-ruby/#{SODA::VERSION} (#{Uname.uname.sysname}/#{Uname.uname.release}; Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})" end end |
Instance Method Details
#delete(resource, body = nil, params = {}) ⇒ Object
Deletes a resource via an HTTP DELETE request.
Requires an authenticated client (both :username and :password passed into the options hash)
-
resource- The resource identifier or path. -
body- The payload to send with the DELETE. Will be converted to JSON (optional). -
params- A hash of the URL parameters you want to pass (optional).
Returns a Hashie::Mash that you can interact with as a Hash if you want.
Example
response = client.delete("644b-gaut")
130 131 132 |
# File 'lib/soda/client.rb', line 130 def delete(resource, body = nil, params = {}) connection(:delete, resource, body, params) end |
#get(resource, params = {}) ⇒ Object
Retrieve a resource via an HTTP GET request.
-
resource- The resource identifier or path. -
params- A hash of the URL parameters you want to pass
Returns a Hashie::Mash that you can interact with as a Hash if you want.
Example
response = client.get("644b-gaut", { :firstname => "OPRAH", :lastname => "WINFREY", "$limit" => 5 })
70 71 72 |
# File 'lib/soda/client.rb', line 70 def get(resource, params = {}) connection(:get, resource, nil, params) end |
#post(resource, body = nil, params = {}) ⇒ Object
Update a resource via an HTTP POST request.
Requires an authenticated client (both :username and :password passed into the options hash)
-
resource- The resource identifier or path. -
body- The payload to POST. Will be converted to JSON (optional). -
params- A hash of the URL parameters you want to pass (optional).
Returns a Hashie::Mash that you can interact with as a Hash if you want.
Example
response = client.post("644b-gaut", [{ :firstname => "OPRAH", :lastname => "WINFREY" }])
90 91 92 |
# File 'lib/soda/client.rb', line 90 def post(resource, body = nil, params = {}) connection(:post, resource, body, params) end |
#post_form(resource, body = {}, params = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/soda/client.rb', line 134 def post_form(resource, body = {}, params = {}) query = query_string(params) path = resource_path(resource) uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}") request = Net::HTTP::Post.new(uri.request_uri) add_default_headers_to_request(request) request.set_form_data(body) # Authenticate if we're supposed to authenticate(request) # BAM! http = build_http_client(uri.host, uri.port) handle_response(http.request(request)) end |
#put(resource, body = nil, params = {}) ⇒ Object
Replaces a resource via an HTTP PUT request.
Requires an authenticated client (both :username and :password passed into the options hash)
-
resource- The resource identifier or path. -
body- The payload to POST. Will be converted to JSON (optional). -
params- A hash of the URL parameters you want to pass (optional).
Returns a Hashie::Mash that you can interact with as a Hash if you want.
Example
response = client.put("644b-gaut", [{ :firstname => "OPRAH", :lastname => "WINFREY" }])
110 111 112 |
# File 'lib/soda/client.rb', line 110 def put(resource, body = nil, params = {}) connection(:put, resource, body, params) end |