Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi! I cleaned up your code for you! #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc

# Logs and databases #
######################
*.log

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
2 changes: 1 addition & 1 deletion PostInstall.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

For more information on sds-rest, see http://sdsrest.rubyforge.org

NOTE: Change this information in PostInstall.txt
NOTE: Change this information in PostInstall.txt
You can also delete it if you don't want it.


84 changes: 42 additions & 42 deletions lib/sds-activeresource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'sds-rest'

module SDSActiveResource

class Base < ActiveResource::Base

def self.connection(refresh = false)
Expand All @@ -15,7 +15,7 @@ def self.connection(refresh = false)
@connection.password = password
@connection
end

#we don't want to create XML, we want to pass the attributes directly to the SDS service where the XML can be created
def to_xml(options={})
newattributes = {}
Expand All @@ -28,52 +28,52 @@ def self.query(query)
instantiate_collection(@connection.query(collection_path(), query))
end
end

class SDSConnection < ActiveResource::Connection

def service
@service = SDSRest::Service.new :username => user, :password => password, :authority => get_authority(site), :url => get_url(site) if @service.nil?
@service
end

def query(path, query)
container = get_container(path)
query.gsub!(/AND/i, "%26%26 ")
response = service.query(container, query)
entity = REXML::Document.new(response.body)
entities = []
entity.root().elements.each { |element|
options = {}
element.elements.each { |element2|

entity.root().elements.each { |element|
options = {}
element.elements.each { |element2|
options[element2.name] = parse_value(element.attributes["xsi:type"], element2.text)
}
entities.push(options)
}

entities
end

def post(path, body = '', headers = {})
container = get_container(path)
response = service.create_entity(container, body["entityname"], body["Id"], body)
response['location'] = "test/" + body["Id"].to_s
response
end
end

def delete(path, body = '', headers = {})
container = get_container(path)
id = get_id(path)
service.delete_entity(container, id)
end
end

def put(path, body = '', headers = {})
container = get_container(path)
container = get_container(path)
response = service.update_entity(container, body["name"], body["Id"], nil, body)
response['location'] = "test/" + body["Id"].to_s
response
end

def get(path, headers = {})
container = get_container(path)
entityname = get_entity(path)
Expand All @@ -83,75 +83,75 @@ def get(path, headers = {})
response = service.query(container, query)
entity = REXML::Document.new(response.body)
entities = []
entity.root().elements.each { |element|
options = {}
element.elements.each { |element2|

entity.root().elements.each { |element|
options = {}
element.elements.each { |element2|
options[element2.name] = parse_value(element2.attributes["xsi:type"], element2.text)
}
entities.push(options)
}

entities
entities
else

response = service.get_entity(container, id)

entity = REXML::Document.new(response.body)
options = {}
entity.root().elements.each { |element|

options = {}
entity.root().elements.each { |element|
options[element.name] = parse_value(element.attributes["xsi:type"], element.text)
}
options['id'] = id
options

end
end

def get_authority(path)
splitpath = URI::split(path.to_s)
splitpath[2].split('.')[0]
end

def get_url(path)
splitpath = URI::split(path.to_s)
host = splitpath[2]
host[get_authority(path) + "."] = ""
host
end

def get_params(path)
puts path
if(path.include? "?")
params = path.split('?')[1]
params = path.split('?')[1]
params || ""
end
end

def get_container(path)
container = path.to_s.split('/')[1]

if(container.nil?)
raise "no container found"
end
container
end

def get_entity(path)
path.delete! '.xml'
entity = path.split('/')[2]

if(entity.include? "?")
entity = entity.split('?')[0]
entity = entity.split('?')[0]
end

if(entity.nil?)
raise "no entity found"
end
entity
end

def parse_value(type, value)
if(type == "x:decimal")
if(value.include?("."))
Expand All @@ -165,19 +165,19 @@ def parse_value(type, value)
DateTime.parse(value)
else
value
end
end
end

def get_id(path)
path.delete! '.xml'
path.to_s.split('/')[3]
end

def Boolean(string)
return true if string == true || string =~ /^true$/i
return false if string == false || string.nil? || string =~ /^false$/i
raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
end

end
end
Loading