rfid_api 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +6 -0
- data/lib/rfid_api/client.rb +41 -29
- data/lib/rfid_api/version.rb +1 -1
- data/spec/models/device_spec.rb +96 -0
- data/spec/rfid_api_spec.rb +25 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/helpers/models_helper.rb +23 -0
- metadata +9 -7
data/Gemfile
CHANGED
data/lib/rfid_api/client.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
+
require "active_support/core_ext"
|
1
2
|
require "active_model"
|
2
3
|
require "httparty"
|
3
4
|
require "ostruct"
|
4
|
-
require
|
5
|
+
require "yajl/json_gem"
|
5
6
|
|
6
7
|
module RfidApi
|
7
|
-
class Client < OpenStruct
|
8
|
+
class Client < ::OpenStruct
|
8
9
|
include ::HTTParty
|
9
10
|
|
10
|
-
extend ActiveModel::Naming
|
11
|
+
extend ::ActiveModel::Naming
|
11
12
|
|
12
13
|
# https://rfidapi.aimbulance.com/api/v1/devices.xml
|
13
14
|
base_uri File.join("https://rfidapi.aimbulance.com", "api", RfidApi.api_version)
|
@@ -17,6 +18,8 @@ module RfidApi
|
|
17
18
|
|
18
19
|
delegate :create, :update, :get, :plural_name, :format, :resources, :to => "self.class"
|
19
20
|
|
21
|
+
attr_writer :destroyed
|
22
|
+
|
20
23
|
class << self
|
21
24
|
|
22
25
|
# Gets the resource_name
|
@@ -40,7 +43,8 @@ module RfidApi
|
|
40
43
|
end
|
41
44
|
|
42
45
|
def update(id, attributes)
|
43
|
-
|
46
|
+
response = put("/#{plural_name}/#{id}.#{format}", :body => { singular_name => attributes })
|
47
|
+
resource(response, attributes)
|
44
48
|
end
|
45
49
|
|
46
50
|
def all(options = {})
|
@@ -52,7 +56,9 @@ module RfidApi
|
|
52
56
|
end
|
53
57
|
|
54
58
|
def destroy(id)
|
55
|
-
resource delete("/#{plural_name}/#{id}.#{format}")
|
59
|
+
record = resource( delete("/#{plural_name}/#{id}.#{format}") )
|
60
|
+
record.destroyed = (record && record.valid?)
|
61
|
+
record
|
56
62
|
end
|
57
63
|
|
58
64
|
def model_name
|
@@ -69,26 +75,16 @@ module RfidApi
|
|
69
75
|
@plural_name ||= resource_name.to_s.downcase.pluralize
|
70
76
|
end
|
71
77
|
|
72
|
-
def resources(response,
|
73
|
-
if response
|
78
|
+
def resources(response, attributes = {})
|
79
|
+
if response && [200, 201, 422].include?(response.code)
|
74
80
|
body = post_parse(response.parsed_response)
|
75
|
-
|
76
|
-
if [200, 201].include?(response.code)
|
77
|
-
[body].flatten.collect { |item| new(item) }
|
78
|
-
elsif [422].include?(response.code)
|
79
|
-
[body].flatten.collect do |item|
|
80
|
-
item = new(options)
|
81
|
-
item.errors = body[singular_name]["errors"]
|
82
|
-
item
|
83
|
-
end
|
84
|
-
else
|
85
|
-
nil
|
86
|
-
end
|
81
|
+
[body].flatten.collect { |item| new(item) }
|
87
82
|
end
|
88
83
|
end
|
89
84
|
|
90
|
-
def resource(
|
91
|
-
resources(
|
85
|
+
def resource(response, attributes = {})
|
86
|
+
array = resources(response, attributes)
|
87
|
+
array.nil? ? nil : array.first
|
92
88
|
end
|
93
89
|
|
94
90
|
def post_parse(body)
|
@@ -101,6 +97,14 @@ module RfidApi
|
|
101
97
|
end
|
102
98
|
end
|
103
99
|
|
100
|
+
def initialize(hash = nil)
|
101
|
+
if hash && (hash[:errors] || hash['errors'])
|
102
|
+
self.errors = (hash.delete(:errors) || hash.delete('errors'))
|
103
|
+
end
|
104
|
+
|
105
|
+
super(hash)
|
106
|
+
end
|
107
|
+
|
104
108
|
def to_key
|
105
109
|
[_id]
|
106
110
|
end
|
@@ -114,10 +118,7 @@ module RfidApi
|
|
114
118
|
end
|
115
119
|
|
116
120
|
def destroy
|
117
|
-
unless new_record?
|
118
|
-
self.class.destroy(_id)
|
119
|
-
@destroyed = true
|
120
|
-
end
|
121
|
+
self.class.destroy(_id) unless new_record?
|
121
122
|
end
|
122
123
|
|
123
124
|
def new_record?
|
@@ -125,11 +126,15 @@ module RfidApi
|
|
125
126
|
end
|
126
127
|
|
127
128
|
def persisted?
|
128
|
-
!(new_record? || destroyed?)
|
129
|
+
!(new_record? || destroyed?) && valid?
|
129
130
|
end
|
130
131
|
|
131
132
|
def destroyed?
|
132
|
-
@destroyed
|
133
|
+
@destroyed === true
|
134
|
+
end
|
135
|
+
|
136
|
+
def valid?
|
137
|
+
errors.empty?
|
133
138
|
end
|
134
139
|
|
135
140
|
def errors
|
@@ -142,13 +147,20 @@ module RfidApi
|
|
142
147
|
end
|
143
148
|
end
|
144
149
|
|
150
|
+
def read_attribute(name)
|
151
|
+
@table[name.to_sym]
|
152
|
+
end
|
153
|
+
|
154
|
+
def write_attribute(name, value)
|
155
|
+
@table[name.to_sym] = value
|
156
|
+
end
|
157
|
+
|
145
158
|
def datetime(colum_name)
|
146
159
|
begin
|
147
160
|
DateTime.parse(send(colum_name))
|
148
161
|
rescue Exception => e
|
149
162
|
nil
|
150
163
|
end
|
151
|
-
end
|
152
|
-
|
164
|
+
end
|
153
165
|
end
|
154
166
|
end
|
data/lib/rfid_api/version.rb
CHANGED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe RfidApi::Device do
|
4
|
+
include ModelsHelper
|
5
|
+
|
6
|
+
it "should be valid" do
|
7
|
+
RfidApi::Device.should be_a(Class)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should get devices" do
|
11
|
+
FakeWeb.register_uri(:get, File.join(rfid_url, "devices.json"),
|
12
|
+
:body => [device_attrs, device_attrs("_id" => 2) ].to_json)
|
13
|
+
|
14
|
+
devices = RfidApi::Device.all
|
15
|
+
devices.should_not be_empty
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should get one device" do
|
19
|
+
attrs = device_attrs("_id" => "4e312bafc546615929000001")
|
20
|
+
|
21
|
+
FakeWeb.register_uri(:get, File.join(rfid_url, "devices", "#{attrs['_id']}.json"),
|
22
|
+
:body => attrs.to_json)
|
23
|
+
|
24
|
+
device = RfidApi::Device.find(attrs["_id"])
|
25
|
+
|
26
|
+
attrs.each do |key, value|
|
27
|
+
device.send(key).should == JSON.parse(value.to_json)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return nil if RFID service not available" do
|
32
|
+
id = "4e312bafc546615929000001"
|
33
|
+
FakeWeb.register_uri(:get, File.join(rfid_url, "devices", "#{id}.json"),
|
34
|
+
:body => '{"error":{"message":"Some error","info":"Some error"}}', :status => ["500", "Service not available"])
|
35
|
+
|
36
|
+
device = RfidApi::Device.find(id)
|
37
|
+
device.should be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
context "create/update/destroy" do
|
41
|
+
before(:each) do
|
42
|
+
@attrs = device_attrs("title" => "Test device")
|
43
|
+
@attrs.delete_if {|key, value| ["_id", "created_at", "updated_at", "relays"].include?(key) }
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should create new device" do
|
47
|
+
FakeWeb.register_uri(:post, File.join(rfid_url, "devices.json"),
|
48
|
+
:body => '{"created_at":"2011-12-06T13:24:13+02:00","latitude":0.0,"secret_token":"sfoYxCgnYkCuNvZCpgxH","title":"Test device","updated_at":"2011-12-06T13:24:13+02:00","_id":"4eddfb5dc546612e14000063","longitude":0.0}')
|
49
|
+
|
50
|
+
device = RfidApi::Device.create(@attrs)
|
51
|
+
device.secret_token.should_not == @attrs['secret_token']
|
52
|
+
device.title.should == @attrs['title']
|
53
|
+
device.to_param.should_not be_blank
|
54
|
+
device.should_not be_new_record
|
55
|
+
device.should be_persisted
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should not create new device with invalid params" do
|
59
|
+
FakeWeb.register_uri(:post, File.join(rfid_url, "devices.json"),
|
60
|
+
:body => '{"created_at":null,"latitude":"wrong","secret_token":"2ny2RWAsSn7cRVlxmsVI","title":"","updated_at":null,"_id":"4eddfc65c546612e14000066","errors":{"title":["can\'t be blank"],"latitude":["is not a number"]},"longitude":0.0}')
|
61
|
+
|
62
|
+
@attrs["title"] = ''
|
63
|
+
@attrs["latitude"] = 'wrong'
|
64
|
+
|
65
|
+
device = RfidApi::Device.create(@attrs)
|
66
|
+
device.secret_token.should_not == @attrs['secret_token']
|
67
|
+
device.title.should == @attrs['title']
|
68
|
+
device.latitude.should == @attrs['latitude']
|
69
|
+
device.errors.should_not be_empty
|
70
|
+
device.should_not be_valid
|
71
|
+
device.should_not be_persisted
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should update device by id" do
|
75
|
+
id = "4eddfb5dc546612e14000063"
|
76
|
+
|
77
|
+
FakeWeb.register_uri(:put, File.join(rfid_url, "devices", "#{id}.json"),
|
78
|
+
:body => '{"created_at":"2011-12-06T13:24:13+02:00","latitude":0.0,"secret_token":"sfoYxCgnYkCuNvZCpgxH","title":"Update","updated_at":"2011-12-06T13:24:13+02:00","_id":"4eddfb5dc546612e14000063","longitude":0.0}')
|
79
|
+
|
80
|
+
device = RfidApi::Device.update(id, :title => "Update")
|
81
|
+
device.title.should == "Update"
|
82
|
+
device.should be_valid
|
83
|
+
device.should be_persisted
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should destroy device by id" do
|
87
|
+
id = "4eddfb5dc546612e14000063"
|
88
|
+
|
89
|
+
FakeWeb.register_uri(:delete, File.join(rfid_url, "devices", "#{id}.json"),
|
90
|
+
:body => '{"created_at":"2011-12-06T13:24:13+02:00","latitude":0.0,"secret_token":"sfoYxCgnYkCuNvZCpgxH","title":"Noname","updated_at":"2011-12-06T13:24:13+02:00","_id":"4eddfb5dc546612e14000063","longitude":0.0}')
|
91
|
+
|
92
|
+
device = RfidApi::Device.destroy(id)
|
93
|
+
device.should be_destroyed
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RfidApi do
|
4
|
+
it "should be valid" do
|
5
|
+
RfidApi.should be_a(Module)
|
6
|
+
end
|
7
|
+
|
8
|
+
context "setup" do
|
9
|
+
before(:each) do
|
10
|
+
RfidApi.setup do |config|
|
11
|
+
config.username = "user"
|
12
|
+
config.password = "pass"
|
13
|
+
config.format = :json
|
14
|
+
config.api_version = "v2"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should set config options" do
|
19
|
+
RfidApi.username.should == "user"
|
20
|
+
RfidApi.password.should == "pass"
|
21
|
+
RfidApi.format.should == :json
|
22
|
+
RfidApi.api_version.should == "v2"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
$:.push File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
require "rfid_api"
|
6
|
+
require "fakeweb"
|
7
|
+
|
8
|
+
# Load support files
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
10
|
+
|
11
|
+
RfidApi.setup do |config|
|
12
|
+
config.username = 'user'
|
13
|
+
config.password = 'password'
|
14
|
+
config.logger = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
# Remove this line if you don't want RSpec's should and should_not
|
19
|
+
# methods or matchers
|
20
|
+
require 'rspec/expectations'
|
21
|
+
config.include RSpec::Matchers
|
22
|
+
|
23
|
+
# == Mock Framework
|
24
|
+
config.mock_with :rspec
|
25
|
+
|
26
|
+
config.before(:suite) do
|
27
|
+
FakeWeb.allow_net_connect = false
|
28
|
+
end
|
29
|
+
|
30
|
+
config.after(:suite) do
|
31
|
+
FakeWeb.allow_net_connect = true
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ModelsHelper
|
2
|
+
def rfid_auth
|
3
|
+
[RfidApi.username, RfidApi.password].join(':')
|
4
|
+
end
|
5
|
+
|
6
|
+
def rfid_url
|
7
|
+
"https://#{rfid_auth}@rfidapi.aimbulance.com/api/v1"
|
8
|
+
end
|
9
|
+
|
10
|
+
def device_attrs(attrs = {})
|
11
|
+
attrs = {
|
12
|
+
"relays" => [],
|
13
|
+
"created_at" => Time.now,
|
14
|
+
"latitude" => 0,
|
15
|
+
"secret_token" => "Tpj1gs26b5XJPikc36ST",
|
16
|
+
"title" => "Noname",
|
17
|
+
"updated_at" => Time.now,
|
18
|
+
"_id" => "4e312bafc546615929000001",
|
19
|
+
"longitude" => 0,
|
20
|
+
"action_klass_name" => "DefaultAction"
|
21
|
+
}.merge(attrs)
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rfid_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Igor Galeta
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
18
|
+
date: 2011-12-08 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: yajl-ruby
|
@@ -101,7 +100,10 @@ files:
|
|
101
100
|
- lib/rfid_api/social_account.rb
|
102
101
|
- lib/rfid_api/version.rb
|
103
102
|
- rfid_api.gemspec
|
104
|
-
|
103
|
+
- spec/models/device_spec.rb
|
104
|
+
- spec/rfid_api_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/support/helpers/models_helper.rb
|
105
107
|
homepage: https://github.com/galetahub/rfid_api
|
106
108
|
licenses: []
|
107
109
|
|
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
133
|
requirements: []
|
132
134
|
|
133
135
|
rubyforge_project: rfid_api
|
134
|
-
rubygems_version: 1.
|
136
|
+
rubygems_version: 1.8.10
|
135
137
|
signing_key:
|
136
138
|
specification_version: 3
|
137
139
|
summary: Simple client for RFID API
|