From c1a28a9e068b6b04219005424e9b143fd94d35bb Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 20 May 2011 17:03:38 +0200 Subject: [PATCH] First real commit with basic functions --- .gitignore | 1 + .rspec | 1 + .rvmrc | 1 + Gemfile | 1 - README | 13 +++++++++++++ lib/rjiffy.rb | 11 ++++++++++- lib/rjiffy/box.rb | 13 +++++++++++++ lib/rjiffy/connection.rb | 22 ++++++++++++++++++++++ rjiffy.gemspec | 8 ++++++-- spec/rjiffy_spec.rb | 26 ++++++++++++++++++++++++++ spec/spec_helper.rb | 4 ++++ 11 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 .rspec create mode 100644 .rvmrc create mode 100644 README create mode 100644 lib/rjiffy/box.rb create mode 100644 lib/rjiffy/connection.rb create mode 100644 spec/rjiffy_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 4040c6c..2f0a9ac 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .bundle Gemfile.lock pkg/* +.DS_Store diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..53607ea --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--colour diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 0000000..84ebced --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm use 1.9.2@rjiffy --create diff --git a/Gemfile b/Gemfile index 8409f35..c80ee36 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,3 @@ source "http://rubygems.org" -# Specify your gem's dependencies in rjiffy.gemspec gemspec diff --git a/README b/README new file mode 100644 index 0000000..821003f --- /dev/null +++ b/README @@ -0,0 +1,13 @@ +### Description +# +# +# Find all boxes +api = Rjiffy::Connection.new() +boxes = api.boxes + +# Find one box +box = api.find(ID) +box == Object with attributes for the box infos + +# Delete a box +box.delete diff --git a/lib/rjiffy.rb b/lib/rjiffy.rb index 4d70c05..663e764 100644 --- a/lib/rjiffy.rb +++ b/lib/rjiffy.rb @@ -1,3 +1,12 @@ module Rjiffy - # Your code goes here... + require 'rjiffy/connection' + require 'rjiffy/box' +end + +class ::Hash + def method_missing(name) + return self[name] if key? name + self.each { |k,v| return v if k.to_s.to_sym == name } + super.method_missing name + end end diff --git a/lib/rjiffy/box.rb b/lib/rjiffy/box.rb new file mode 100644 index 0000000..0e64760 --- /dev/null +++ b/lib/rjiffy/box.rb @@ -0,0 +1,13 @@ +class Box < Rjiffy::Connection + def initialize(hash) + hash.each do |k,v| + self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair + self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable + self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable + end + end + + def delete + # TODO + end +end diff --git a/lib/rjiffy/connection.rb b/lib/rjiffy/connection.rb new file mode 100644 index 0000000..33dfb0f --- /dev/null +++ b/lib/rjiffy/connection.rb @@ -0,0 +1,22 @@ +module Rjiffy + class Connection + include HTTParty + + API_VERSION = "v1.0" + + def initialize(token) + self.class.base_uri "https://api.jiffybox.de/#{token}/#{API_VERSION}" + end + + def list_boxes + response = self.class.get("/jiffyBoxes") + response["result"] + end + + def find(id) + response = self.class.get("/jiffyBoxes/#{id}") + Box.new(response["result"]) + end + + end +end diff --git a/rjiffy.gemspec b/rjiffy.gemspec index d3c2471..f456e9d 100644 --- a/rjiffy.gemspec +++ b/rjiffy.gemspec @@ -6,8 +6,8 @@ Gem::Specification.new do |s| s.name = "rjiffy" s.version = Rjiffy::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["TODO: Write your name"] - s.email = ["TODO: Write your email address"] + s.authors = ["Frank Mueller"] + s.email = ["frank@heidjer.info"] s.homepage = "" s.summary = %q{TODO: Write a gem summary} s.description = %q{TODO: Write a gem description} @@ -18,4 +18,8 @@ Gem::Specification.new do |s| s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] + + s.add_dependency('httparty', '>= 0.7.7') + + s.add_development_dependency('rspec', '>= 2.6.0') end diff --git a/spec/rjiffy_spec.rb b/spec/rjiffy_spec.rb new file mode 100644 index 0000000..730d375 --- /dev/null +++ b/spec/rjiffy_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper')) + +describe Rjiffy do + + before(:all) do + #TODO: stub request + @connection = Rjiffy::Connection.new("sjdhakjsdhkajsdhak") + end + + it "list all jiffyboxes" do + @connection.list_boxes.class.should == Hash + # test if the data are like the jiffybox api specification + end + + it "find jiffybox by id" do + id = 11243 + box = @connection.find(id) + box.name.should == "DeathStar" + box.class.should == Box + end + + it "should delete specific jiffybox" do + # test delete + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e015f9b --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,4 @@ +$:.push File.expand_path("../lib", __FILE__) +require "httparty" + +require "rjiffy"