Class: SODA::Client
- Inherits:
-
Object
- Object
- SODA::Client
- Defined in:
- lib/soda/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #blank?(object) ⇒ Boolean
-
#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" })
55 56 57 58 |
# File 'lib/soda/client.rb', line 55 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 |
# File 'lib/soda/client.rb', line 21 def generate_user_agent if Gem.win_platform? return "soda-ruby/#{SODA::VERSION} (Windows; Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})" end "soda-ruby/#{SODA::VERSION} (#{Uname.uname.sysname}/#{Uname.uname.release}; Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})" end |
Instance Method Details
#blank?(object) ⇒ Boolean
29 30 31 |
# File 'lib/soda/client.rb', line 29 def blank?(object) object.nil? || object.empty? end |
#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")
133 134 135 |
# File 'lib/soda/client.rb', line 133 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 })
73 74 75 |
# File 'lib/soda/client.rb', line 73 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" }])
93 94 95 |
# File 'lib/soda/client.rb', line 93 def post(resource, body = nil, params = {}) connection(:post, resource, body, params) end |
#post_form(resource, body = {}, params = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/soda/client.rb', line 137 def post_form(resource, body = {}, params = {}) # We'll combine any params we got from our base resource with # those passed in base = URI.parse(parse_resource(resource)) query = [ base.query, query_string(params) ].reject { |s| blank?(s) }.join '&' uri = URI.parse("https://#{base.host}#{base.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" }])
113 114 115 |
# File 'lib/soda/client.rb', line 113 def put(resource, body = nil, params = {}) connection(:put, resource, body, params) end |