Skip to content

Commit

Permalink
Introduced some metaprograming techniques to make objects better to i…
Browse files Browse the repository at this point in the history
…nteract with. Should be able to use setter methods now but haven't tested yet.
  • Loading branch information
Tom Skarbek Wazynski committed Mar 30, 2015
1 parent 0209214 commit a572abc
Show file tree
Hide file tree
Showing 26 changed files with 919 additions and 319 deletions.
Binary file added badgeapi-0.0.8.gem
Binary file not shown.
1 change: 1 addition & 0 deletions badgeapi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Gem::Specification.new do |spec|
spec.authors = ["Tom Skarbek Wazynski"]
spec.email = ["[email protected]"]
spec.summary = %q{Allows you to connect to Lancaster University Badge API to manipulate badges, issue badges and display badges.}
spec.description = %q{Allows you to connect to Lancaster University Badge API to manipulate badges, issue badges and display badges.}
spec.homepage = "http://innovationhub.lancaster.ac.uk"
spec.license = "MIT"

Expand Down
3 changes: 3 additions & 0 deletions lib/badgeapi.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'active_support/all'
require 'faraday'
require 'json'

require_relative "badgeapi/badgeapi_object"
require_relative "badgeapi/version"
require_relative "badgeapi/badge"
require_relative "badgeapi/collection"
Expand Down
48 changes: 24 additions & 24 deletions lib/badgeapi/badge.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
#lib/badgeapi/badge.rb
require 'faraday'
require 'json'

module Badgeapi
class Badge

attr_reader :id, :name, :description, :requirements, :hint, :image, :recipient_id, :issuer_id, :collection_id, :issued_at, :created_at, :updated_at

def initialize(attributes)
@id = attributes["id"]
@name = attributes["name"]
@description = attributes["description"]
@requirements = attributes["requirements"]
@hint = attributes["hint"]
@image = attributes["image"]
@recipient_id = attributes["recipient_id"]
@collection_id = attributes["collection_id"]
@issued_at = attributes["issued_at"]
@created_at = attributes["created_at"]
@updated_at = attributes["updated_at"]
end
class Badge < BadgeapiObject

define_attribute_methods %w(
id
name
description
requirements
hint
image
recipient_id
collection_id
issuer_id
issued_at
created_at
updated_at
)

def self.find(id)
connection = Faraday.new()
Expand All @@ -29,7 +25,7 @@ def self.find(id)
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
new(attributes)
from_response(attributes)
end
end

Expand All @@ -43,7 +39,7 @@ def self.all params = {}
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
attributes.map { |attributes| new(attributes) }
attributes.map { |attributes| from_response(attributes) }
end
end

Expand All @@ -58,10 +54,14 @@ def self.create params={}
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
new(attributes)
from_response(attributes)
end
end

def self.save params={}

end

def self.destroy(id)
connection = Faraday.new()
connection.token_auth(Badgeapi.api_key)
Expand All @@ -73,7 +73,7 @@ def self.destroy(id)
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
new(attributes)
from_response(attributes)
end
end

Expand Down
52 changes: 52 additions & 0 deletions lib/badgeapi/badgeapi_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#lib/badgeapi/badgeapi_object.rb

module Badgeapi
class BadgeapiObject
class << self

def define_attribute_methods attribute_names
attribute_names.each do |name|
define_method name.to_s do
return instance_variable_get("@#{name}".to_sym)
end

define_method "#{name}=" do |val|
instance_variable_set("@#{name}".to_sym, val)
end
end
end

attr_reader :attribute_names

def initialize attributes = {}
if instance_of? BadgeapiObject
raise Error,
"#{self.class} is an abstract class and cannot be instantiated"
end

self.attributes = attributes
yield self if block_given?
end

def from_response attributes
record = new

attributes.each do |name, value|
record.instance_variable_set "@#{name}", value
#"@#{name}" = value.to_s
end

# record.persist! if record.respond_to? :persist!
record
end


def from_json attributes = {}
attributes.map { |attributes| new(attributes) }
end

attr_reader :attributes

end
end
end
29 changes: 13 additions & 16 deletions lib/badgeapi/collection.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#lib/badgeapi/collection.rb
require 'faraday'
require 'json'

module Badgeapi
class Collection
class Collection < BadgeapiObject

attr_reader :id, :name, :description, :created_at, :updated_at

def initialize(attributes)
@id = attributes["id"]
@name = attributes["name"]
@description = attributes["description"]
@created_at = attributes["created_at"]
@updated_at = attributes["updated_at"]
end
define_attribute_methods %w(
id
name
description
created_at
updated_at
)

def self.find(id)
connection = Faraday.new()
Expand All @@ -23,8 +19,9 @@ def self.find(id)
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
new(attributes)
from_response(attributes)
end

end

def self.all params = {}
Expand All @@ -35,7 +32,7 @@ def self.all params = {}
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
attributes.map { |attributes| new(attributes) }
attributes.map { |attributes| from_response(attributes) }
end
end

Expand All @@ -50,7 +47,7 @@ def self.create params={}
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
new(attributes)
from_response(attributes)
end
end

Expand All @@ -65,7 +62,7 @@ def self.destroy(id)
if attributes.include?("error")
raise Exception.new(attributes['error'])
else
new(attributes)
from_response(attributes)
end
end

Expand Down
55 changes: 38 additions & 17 deletions test/badge/badge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_exists
end

def test_it_returns_back_a_single_badge
VCR.use_cassette('one_badge') do
Badgeapi.api_key = "c1616687d0fc420d85c8590357d1ab29"
VCR.use_cassette('one_badge', :record => :all) do
Badgeapi.api_key = "86340fbfc17b4032b07592037dcc5e0b"

badge = Badgeapi::Badge.find(1)
assert_equal Badgeapi::Badge, badge.class
Expand All @@ -29,8 +29,8 @@ def test_it_returns_back_a_single_badge
end

def test_it_returns_back_all_badges
VCR.use_cassette('all_badges') do
Badgeapi.api_key = "c1616687d0fc420d85c8590357d1ab29"
VCR.use_cassette('all_badges', :record => :all) do
Badgeapi.api_key = "86340fbfc17b4032b07592037dcc5e0b"
result = Badgeapi::Badge.all

# Make sure we got all the badges
Expand All @@ -44,7 +44,7 @@ def test_it_returns_back_all_badges

def test_it_returns_back_all_badges_from_collection
VCR.use_cassette('all_badges_from_collection') do
Badgeapi.api_key = "c1616687d0fc420d85c8590357d1ab29"
Badgeapi.api_key = "86340fbfc17b4032b07592037dcc5e0b"
result = Badgeapi::Badge.all(collection_id: 2)

# Make sure we got all the badges
Expand All @@ -57,8 +57,8 @@ def test_it_returns_back_all_badges_from_collection
end

def test_all_limit
VCR.use_cassette('all_badges_limited') do
Badgeapi.api_key = "c1616687d0fc420d85c8590357d1ab29"
VCR.use_cassette('all_badges_limited', :record => :all) do
Badgeapi.api_key = "86340fbfc17b4032b07592037dcc5e0b"
result = Badgeapi::Badge.all(limit: 1)

# Make sure we got all the badges
Expand All @@ -71,16 +71,16 @@ def test_all_limit
end

def test_badges_raise_errors
VCR.use_cassette('badge_error') do
Badgeapi.api_key= 'c1616687d0fc420d85c8590357d1ab29'
VCR.use_cassette('badge_error', :record => :all) do
Badgeapi.api_key= '86340fbfc17b4032b07592037dcc5e0b'
assert_raises(Exception) { Badgeapi::Badge.find(27) }
end
end

def test_create_a_new_badge
VCR.use_cassette('create_badge') do
VCR.use_cassette('create_badge', :record => :all) do

Badgeapi.api_key = 'c1616687d0fc420d85c8590357d1ab29'
Badgeapi.api_key = '86340fbfc17b4032b07592037dcc5e0b'

badge = Badgeapi::Badge.create(
name: "Create Badge Test",
Expand All @@ -104,9 +104,9 @@ def test_create_a_new_badge
end

def test_create_new_badge_failure
VCR.use_cassette('create_new_badge_failure') do
VCR.use_cassette('create_new_badge_failure', :record => :all) do

Badgeapi.api_key = 'c1616687d0fc420d85c8590357d1ab29'
Badgeapi.api_key = '86340fbfc17b4032b07592037dcc5e0b'

badge = Badgeapi::Badge.create(
name: "Create Badge Test Destroy",
Expand All @@ -133,9 +133,9 @@ def test_create_new_badge_failure
end

def test_badge_destroy
VCR.use_cassette('destroy_badge') do
VCR.use_cassette('destroy_badge', :record => :all) do

Badgeapi.api_key= 'c1616687d0fc420d85c8590357d1ab29'
Badgeapi.api_key= '86340fbfc17b4032b07592037dcc5e0b'

badge = Badgeapi::Badge.create(
name: "Create Badge for Destroy",
Expand All @@ -155,9 +155,9 @@ def test_badge_destroy
end

def test_badge_destroy_error
VCR.use_cassette('destroy_badge_error') do
VCR.use_cassette('destroy_badge_error', :record => :all) do

Badgeapi.api_key= 'c1616687d0fc420d85c8590357d1ab29'
Badgeapi.api_key= '86340fbfc17b4032b07592037dcc5e0b'

badge = Badgeapi::Badge.create(
name: "Create Badge for Destroy",
Expand All @@ -174,6 +174,27 @@ def test_badge_destroy_error
end
end

# def test_update_badge
# VCR.use_cassette('destroy_badge_error', :record => :all) do
#
# Badgeapi.api_key= '86340fbfc17b4032b07592037dcc5e0b'
#
# badge = Badgeapi::Badge.create(
# name: "Create Badge for update",
# description: "This is a new badge",
# requirements: "You need to love the Badge API",
# hint: "Love us..",
# image: "http://example.org/badge.png",
# collection_id: 1
# )
#
# badge = Badgeapi::Badge.update(badge.id,
# name: "Updated Badge",
# )
#
# end
# end



end
Loading

0 comments on commit a572abc

Please sign in to comment.