Skip to content

Commit

Permalink
Fix Gem Gemfile to bundle with soda.gemspec
Browse files Browse the repository at this point in the history
Rubocop first pass
  -  5 offenses left
  • Loading branch information
michaelachrisco authored and housepage committed Jun 11, 2015
1 parent 015471f commit 6b087ba
Show file tree
Hide file tree
Showing 9 changed files with 1,269 additions and 215 deletions.
1,064 changes: 1,064 additions & 0 deletions .rubocop.yml

Large diffs are not rendered by default.

16 changes: 3 additions & 13 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem 'hashie'
gem 'multipart-post'

group :development do
gem 'rake'
end

group :test do
gem 'test-unit'
gem 'shoulda'
gem 'webmock'
end
# Specify your gem's dependencies in soda.gemspec
gemspec
12 changes: 10 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
PATH
remote: .
specs:
soda-ruby (0.2.13)
hashie
multipart-post

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -37,9 +44,10 @@ PLATFORMS
ruby

DEPENDENCIES
hashie
multipart-post
bundler (~> 1.7)
minitest
rake
shoulda
soda-ruby!
test-unit
webmock
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Rake::TestTask.new do |t|
t.libs << 'test'
end

desc "Run tests"
desc 'Run tests'
task :default => :test
2 changes: 1 addition & 1 deletion lib/soda.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require "soda/client"
require 'soda/client'
142 changes: 68 additions & 74 deletions lib/soda/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Client
# client = SODA::Client.new({ :domain => "data.agency.gov", :app_token => "CGxarwoQlgQSev4zyUh5aR5J3" })
#
def initialize(config = {})
@config = config.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
@config = config.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
end

##
Expand Down Expand Up @@ -120,7 +120,7 @@ def post_form(resource, body = {}, params = {})
uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")

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

# Authenticate if we're supposed to
Expand All @@ -130,99 +130,93 @@ def post_form(resource, body = {}, params = {})

# BAM!
http = build_http_client(uri.host, uri.port)
return handle_response(http.request(request))
handle_response(http.request(request))
end

private
def query_string(params)
# Create query string of escaped key, value pairs
return params.collect{ |key, val| "#{key}=#{CGI::escape(val.to_s)}" }.join("&")
end

def resource_path(resource)
# If we didn't get a full path, assume "/resource/"
if !resource.start_with?("/")
resource = "/resource/" + resource
end
def query_string(params)
# Create query string of escaped key, value pairs
params.map { |key, val| "#{key}=#{CGI.escape(val.to_s)}" }.join('&')
end

# Check to see if we were given an output type
extension = ".json"
if matches = resource.match(/^(.+)(\.\w+)$/)
resource = matches.captures[0]
extension = matches.captures[1]
end
def resource_path(resource)
# If we didn't get a full path, assume "/resource/"
resource = '/resource/' + resource unless resource.start_with?('/')

return resource + extension
# Check to see if we were given an output type
extension = '.json'
if matches = resource.match(/^(.+)(\.\w+)$/)
resource = matches.captures[0]
extension = matches.captures[1]
end

def handle_response(response)
# Check our response code
if !["200", "202"].include? response.code
raise "Error in request: #{response.body}"
else
if response.body.nil? || response.body.empty?
return nil
elsif response["Content-Type"].include?("application/json")
# Return a bunch of mashes if we're JSON
response = JSON::parse(response.body, :max_nesting => false)
if response.is_a? Array
return response.collect { |r| Hashie::Mash.new(r) }
else
return Hashie::Mash.new(response)
end
resource + extension
end

def handle_response(response)
# Check our response code
if !%w(200 202).include? response.code
fail "Error in request: #{response.body}"
else
if response.body.nil? || response.body.empty?
return nil
elsif response['Content-Type'].include?('application/json')
# Return a bunch of mashes if we're JSON
response = JSON.parse(response.body, :max_nesting => false)
if response.is_a? Array
return response.map { |r| Hashie::Mash.new(r) }
else
# We don't partically care, just return the raw body
return response.body
return Hashie::Mash.new(response)
end
else
# We don't partically care, just return the raw body
return response.body
end
end
end

def connection(method = "Get", resource = nil, body = nil, params = {})
method = method.to_sym.capitalize

query = query_string(params)
path = resource_path(resource)
uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")
def connection(method = 'Get', resource = nil, body = nil, params = {})
method = method.to_sym.capitalize

request = eval("Net::HTTP::#{method.capitalize}").new(uri.request_uri)
request.add_field("X-App-Token", @config[:app_token])
query = query_string(params)
path = resource_path(resource)
uri = URI.parse("https://#{@config[:domain]}#{path}?#{query}")

if method === :Post || :Put || :Delete
request.content_type = "application/json"
request.body = body.to_json(:max_nesting => false)
end
request = eval("Net::HTTP::#{method.capitalize}").new(uri.request_uri)
request.add_field('X-App-Token', @config[:app_token])

# Authenticate if we're supposed to
if @config[:username]
request.basic_auth @config[:username], @config[:password]
end
if method === :Post || :Put || :Delete
request.content_type = 'application/json'
request.body = body.to_json(:max_nesting => false)
end

http = build_http_client(uri.host, uri.port)
if method === :Delete
response = http.request(request)
# Check our response code
if response.code != "200"
raise "Error querying \"#{uri.to_s}\": #{response.body}"
else
# Return a bunch of mashes
return response
end
else
return handle_response(http.request(request))
end
# Authenticate if we're supposed to
if @config[:username]
request.basic_auth @config[:username], @config[:password]
end

def build_http_client(host, port)
http = Net::HTTP.new(host, port)
http.use_ssl = true
if @config[:ignore_ssl]
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
if @config[:timeout]
http.read_timeout = @config[:timeout]
http = build_http_client(uri.host, uri.port)
if method === :Delete
response = http.request(request)
# Check our response code
if response.code != '200'
fail "Error querying \"#{uri}\": #{response.body}"
else
# Return a bunch of mashes
return response
end
http
else
return handle_response(http.request(request))
end
end

def build_http_client(host, port)
http = Net::HTTP.new(host, port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @config[:ignore_ssl]
http.read_timeout = @config[:timeout] if @config[:timeout]
http
end
end
end
2 changes: 1 addition & 1 deletion lib/soda/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SODA
VERSION = "0.2.13"
VERSION = '0.2.13'
end
30 changes: 18 additions & 12 deletions soda.gemspec
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
require File.expand_path("../lib/soda/version", __FILE__)
require File.expand_path('../lib/soda/version', __FILE__)

Gem::Specification.new do |s|
s.name = 'soda-ruby'
s.version = SODA::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Chris Metcalf"]
s.email = 'chris.metcalf@socrata.com'
s.homepage =
s.name = 'soda-ruby'
s.version = SODA::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Chris Metcalf']
s.email = 'chris.metcalf@socrata.com'
s.homepage =
'http://github.com/socrata/soda-ruby'
s.summary = "Ruby for SODA 2.0"
s.description = "A simple wrapper for SODA 2.0"
s.summary = 'Ruby for SODA 2.0'
s.description = 'A simple wrapper for SODA 2.0'

s.required_rubygems_version = ">= 1.3.6"
s.required_rubygems_version = '>= 1.3.6'

# required for validation
s.rubyforge_project = "soda-ruby"
s.rubyforge_project = 'soda-ruby'

# If you need to check in files that aren't .rb files, add them here
s.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.mkd"]
s.files = Dir['{lib}/**/*.rb', 'bin/*', 'LICENSE', '*.mkd']
s.require_path = 'lib'

# we depend on:
s.add_dependency 'hashie'
s.add_dependency 'multipart-post'
s.add_development_dependency 'bundler', '~> 1.7'
s.add_development_dependency 'rake'
s.add_development_dependency 'shoulda'
s.add_development_dependency 'webmock'
s.add_development_dependency 'minitest'
s.add_development_dependency 'test-unit'
end
Loading

0 comments on commit 6b087ba

Please sign in to comment.