Skip to content

Commit

Permalink
Switch to ActiveResource.
Browse files Browse the repository at this point in the history
  • Loading branch information
kommen committed Jan 10, 2009
1 parent 64092c6 commit 38d40d9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 53 deletions.
27 changes: 6 additions & 21 deletions lib/freckle-gem/entry.rb
@@ -1,33 +1,18 @@
require 'json'
require 'activeresource'

module FreckleGem

class Entry
@@attributes = [:minutes, :project, :description, :user, :date].freeze
@@attributes.each do |a|
attr_accessor a
end

def self.new
e = super
e.date = Date.today
e
end
class Entry < ActiveResource::Base
self.site = "http://#{Freckle::AppConfig.subdomain}.#{Freckle::AppConfig.host}:#{Freckle::AppConfig.port}/api/"

def self.from_options(options)
@@attributes.inject(Entry.new) do |e, a|
e.send("#{a}=", options[a]) if options[a]; e
end
def self.headers
@headers ||= { 'X_FRECKLETOKEN' => Freckle::AppConfig.authtoken }
end

def attributes
@@attributes.inject({}) do |h, a|
h[a] = self.send(a); h
end
{ :date => Date.today }.merge(@attributes || {})
end

def to_json
{ :entry => self.attributes }.to_json
end
end
end
23 changes: 4 additions & 19 deletions lib/freckle-gem/project.rb
Expand Up @@ -2,26 +2,11 @@

module FreckleGem

class Project
@@attributes = [:id, :name].freeze
@@attributes.each do |a|
attr_accessor a
end

def self.from_options(options)
@@attributes.inject(Project.new) do |e, a|
e.send("#{a}=", options[a]) if options[a]; e
end
end

def attributes
@@attributes.inject({}) do |h, a|
h[a] = self.send(a); h
end
end
class Project < ActiveResource::Base
self.site = "http://#{Freckle::AppConfig.subdomain}.#{Freckle::AppConfig.host}:#{Freckle::AppConfig.port}/api/"

def to_json
{ :project => self.attributes }.to_json
def self.headers
@headers ||= { 'X_FRECKLETOKEN' => Freckle::AppConfig.authtoken }
end
end
end
15 changes: 2 additions & 13 deletions lib/freckle/cli.rb
Expand Up @@ -38,19 +38,8 @@ def self.execute(stdout, arguments=[])
end
end

entry = FreckleGem::Entry.from_options(options.merge(:user => Freckle::AppConfig.user))
response = send_request(entry.to_json)
case response
when Net::HTTPSuccess
puts response.body
else
puts "Failed to create entry."
end
end

def self.send_request(data)
http = Net::HTTP.new("#{Freckle::AppConfig.subdomain}.#{Freckle::AppConfig.host}", Freckle::AppConfig.port)
http.post("/api/entries.json?token=#{Freckle::AppConfig.authtoken}", data, { 'Content-Type' => 'application/json' })
entry = FreckleGem::Entry.new(options.merge(:user => Freckle::AppConfig.user))
entry.save
end
end
end

0 comments on commit 38d40d9

Please sign in to comment.