rfid_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rfid_api.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = RFID API client
2
+
3
+ == Install
4
+
5
+ gem 'rfid_api'
6
+
7
+ == Configure
8
+
9
+ RfidApi.setup do |config|
10
+ config.username = 'user'
11
+ config.password = 'password'
12
+ end
13
+
14
+ == Usage
15
+
16
+ @cards = RfidApi::Card.all
17
+
18
+ @device = RfidApi::Device.create(params[:rfid_api_device])
19
+
20
+ @device = RfidApi::Device.update(params[:id], params[:rfid_api_device])
21
+
22
+ @device = RfidApi::Device.find(params[:id])
23
+ @device.destroy
24
+
25
+ Copyright (c) 2011 Aimbulance, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,24 @@
1
+ module RfidApi
2
+ class Action < Client
3
+ self.resource_name = "action"
4
+
5
+ class << self
6
+ def find(device_id, id, options = {})
7
+ resource get("/devices/#{device_id}/#{plural_name}/#{id}.#{format}", :query => options)
8
+ end
9
+
10
+ def create(device_id, attributes)
11
+ response = post("/devices/#{device_id}/#{plural_name}.#{format}", :body => { singular_name => attributes })
12
+ resource(response, attributes)
13
+ end
14
+
15
+ def update(device_id, id, attributes)
16
+ resource put("/devices/#{device_id}/#{plural_name}/#{id}.#{format}", :body => { singular_name => attributes })
17
+ end
18
+
19
+ def destroy(device_id, id)
20
+ resource delete("/devices/#{device_id}/#{plural_name}/#{id}.#{format}")
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module RfidApi
2
+ class Card < Client
3
+ self.resource_name = "card"
4
+ end
5
+ end
@@ -0,0 +1,154 @@
1
+ require "active_model"
2
+ require "httparty"
3
+ require "ostruct"
4
+ require 'yajl/json_gem'
5
+
6
+ module RfidApi
7
+ class Client < OpenStruct
8
+ include ::HTTParty
9
+
10
+ extend ActiveModel::Naming
11
+
12
+ # https://rfidapi.aimbulance.com/api/v1/devices.xml
13
+ base_uri File.join("https://rfidapi.aimbulance.com", "api", RfidApi.api_version)
14
+ basic_auth RfidApi.username, RfidApi.password
15
+ format RfidApi.format
16
+ debug_output File.open(Rails.root.join('log', 'rfid_api.log'), "a")
17
+
18
+ delegate :create, :update, :get, :plural_name, :format, :resources, :to => "self.class"
19
+
20
+ class << self
21
+
22
+ # Gets the resource_name
23
+ def resource_name
24
+ # Not using superclass_delegating_reader. See +site+ for explanation
25
+ if defined?(@resource_name)
26
+ @resource_name
27
+ elsif superclass != Object && superclass.proxy
28
+ superclass.proxy.dup.freeze
29
+ end
30
+ end
31
+
32
+ # Set resource_name
33
+ def resource_name=(name)
34
+ @resource_name = name
35
+ end
36
+
37
+ def create(attributes)
38
+ response = post("/#{plural_name}.#{format}", :body => { singular_name => attributes })
39
+ resource(response, attributes)
40
+ end
41
+
42
+ def update(id, attributes)
43
+ resource put("/#{plural_name}/#{id}.#{format}", :body => { singular_name => attributes })
44
+ end
45
+
46
+ def all(options = {})
47
+ resources get("/#{plural_name}.#{format}", :query => options)
48
+ end
49
+
50
+ def find(id, options = {})
51
+ resource get("/#{plural_name}/#{id}.#{format}", :query => options)
52
+ end
53
+
54
+ def destroy(id)
55
+ resource delete("/#{plural_name}/#{id}.#{format}")
56
+ end
57
+
58
+ def model_name
59
+ @_model_name ||= ActiveModel::Name.new(self)
60
+ end
61
+
62
+ protected
63
+
64
+ def singular_name
65
+ @singular_name ||= resource_name.to_s.downcase.singularize
66
+ end
67
+
68
+ def plural_name
69
+ @plural_name ||= resource_name.to_s.downcase.pluralize
70
+ end
71
+
72
+ def resources(response, options = {})
73
+ if response
74
+ 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
87
+ end
88
+ end
89
+
90
+ def resource(resource, options = {})
91
+ resources(resource, options).first
92
+ end
93
+
94
+ def post_parse(body)
95
+ return body unless body.is_a?(String)
96
+
97
+ case format
98
+ when :json then Yajl::Parser.new.parse(body)
99
+ when :xml then Nokogiri::XML.parse(body)
100
+ end
101
+ end
102
+ end
103
+
104
+ def to_key
105
+ [_id]
106
+ end
107
+
108
+ def to_param
109
+ _id
110
+ end
111
+
112
+ def save
113
+ new_record? ? create(@table) : update(_id, @table)
114
+ end
115
+
116
+ def destroy
117
+ unless new_record?
118
+ self.class.destroy(_id)
119
+ @destroyed = true
120
+ end
121
+ end
122
+
123
+ def new_record?
124
+ _id.blank?
125
+ end
126
+
127
+ def persisted?
128
+ !(new_record? || destroyed?)
129
+ end
130
+
131
+ def destroyed?
132
+ @destroyed == true
133
+ end
134
+
135
+ def errors
136
+ @errors ||= ActiveModel::Errors.new(self)
137
+ end
138
+
139
+ def errors=(attributes)
140
+ attributes.each do |key, value|
141
+ [value].flatten.each { |message| errors.add(key, message) }
142
+ end
143
+ end
144
+
145
+ def datetime(colum_name)
146
+ begin
147
+ DateTime.parse(send(colum_name))
148
+ rescue Exception => e
149
+ nil
150
+ end
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,13 @@
1
+ module RfidApi
2
+ class Device < Client
3
+ self.resource_name = "device"
4
+
5
+ def events(options = {})
6
+ resources get("/#{plural_name}/#{_id}/events.#{format}", :query => options)
7
+ end
8
+
9
+ def actions(options = {})
10
+ resources get("/#{plural_name}/#{_id}/actions.#{format}", :query => options)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module RfidApi
2
+ class Event < Client
3
+ self.resource_name = "event"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module RfidApi
2
+ class SocialAccount < Client
3
+ self.resource_name = "social_account"
4
+
5
+ def cards(options = {})
6
+ resources get("/#{plural_name}/#{_id}/cards.#{format}", :query => options)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module RfidApi
2
+ VERSION = "0.0.1".freeze
3
+ end
data/lib/rfid_api.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'active_support/core_ext/module/attribute_accessors'
2
+
3
+ module RfidApi
4
+ autoload :Client, 'rfid_api/client'
5
+ autoload :Device, 'rfid_api/device'
6
+ autoload :Card, 'rfid_api/card'
7
+ autoload :Event, 'rfid_api/event'
8
+ autoload :SocialAccount, 'rfid_api/social_account'
9
+ autoload :Action, 'rfid_api/action'
10
+
11
+ mattr_accessor :username
12
+ @@username = 'user'
13
+
14
+ mattr_accessor :password
15
+ @@password = 'password'
16
+
17
+ mattr_accessor :format
18
+ @@format = :json
19
+
20
+ mattr_accessor :api_version
21
+ @@api_version = 'v1'
22
+
23
+ def self.setup
24
+ yield self
25
+ end
26
+ end
data/rfid_api.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rfid_api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rfid_api"
7
+ s.version = RfidApi::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Igor Galeta"]
10
+ s.email = ["galeta.igor@gmail.com"]
11
+ s.homepage = "https://github.com/galetahub/rfid_api"
12
+ s.summary = %q{Simple client for RFID API}
13
+ s.description = %q{Simple client to work with RFID API}
14
+
15
+ s.rubyforge_project = "rfid_api"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ s.extra_rdoc_files = ["README.rdoc"]
22
+
23
+ s.add_dependency("yajl-ruby", "~> 1.0.0")
24
+ s.add_dependency("httparty", "~> 0.8.1")
25
+ s.add_dependency("activesupport", ">= 0")
26
+ s.add_dependency("activemodel", ">= 0")
27
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rfid_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Igor Galeta
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-28 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yajl-ruby
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: httparty
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 61
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 1
50
+ version: 0.8.1
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: activesupport
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: activemodel
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :runtime
80
+ version_requirements: *id004
81
+ description: Simple client to work with RFID API
82
+ email:
83
+ - galeta.igor@gmail.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files:
89
+ - README.rdoc
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - README.rdoc
94
+ - Rakefile
95
+ - lib/rfid_api.rb
96
+ - lib/rfid_api/action.rb
97
+ - lib/rfid_api/card.rb
98
+ - lib/rfid_api/client.rb
99
+ - lib/rfid_api/device.rb
100
+ - lib/rfid_api/event.rb
101
+ - lib/rfid_api/social_account.rb
102
+ - lib/rfid_api/version.rb
103
+ - rfid_api.gemspec
104
+ has_rdoc: true
105
+ homepage: https://github.com/galetahub/rfid_api
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project: rfid_api
134
+ rubygems_version: 1.6.2
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Simple client for RFID API
138
+ test_files: []
139
+