Class: SODA::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/soda/client.rb

Instance Method Summary collapse

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)

  • :ignore_ssl - Ignore ssl errors (defaults to false)

Returns a SODA::Client instance.

Example

client = SODA::Client.new({ :domain => "data.agency.gov", :app_token => "CGxarwoQlgQSev4zyUh5aR5J3" })


36
37
38
# File 'lib/soda/client.rb', line 36

def initialize(config = {})
  @config = config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
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")


113
114
115
# File 'lib/soda/client.rb', line 113

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 })


53
54
55
# File 'lib/soda/client.rb', line 53

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" }])


73
74
75
# File 'lib/soda/client.rb', line 73

def post(resource, body = nil, params = {})
  connection(:post, resource, body, params)
end

#post_form(path, fields = {}, params = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/soda/client.rb', line 117

def post_form(path, fields = {}, params = {})
  query = query_string(params)
  resource = resoure_path(path)

  # Create our request
  uri = URI.parse("https://#{@config[:domain]}#{resource}.json?#{query}")
  http = build_http_client(uri.host, uri.port)

  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field("X-App-Token", @config[:app_token])
  request.set_form_data(fields)

  # Authenticate if we're supposed to
  if @config[:username]
    request.basic_auth @config[:username], @config[:password]
  end

  # BAM!
  return 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" }])


93
94
95
# File 'lib/soda/client.rb', line 93

def put(resource, body = nil, params = {})
  connection(:put, resource, body, params)
end