forked from HHS/pillbox_docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpillbox_resource.rb
195 lines (162 loc) · 5.3 KB
/
pillbox_resource.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
=begin
USAGE
require 'pillbox_resource'
name = 'aspirin'
PillboxResource.api_key = "YOUR SECRET KEY"
pills = PillboxResource.find(:all, :params=>{"ingredient"=>name})
if pills.empty?
puts "could not find #{name}"
else
...
end
NOTE: shape/color lookup doesn't always work.
>> PillboxResource.find(:all, :params=>{'shape'=>'C48337'}) # GOOD!
>> PillboxResource.find(:first, :params=>{'color'=>"C48324;C48323"}) # WORKS!
>> PillboxResource.find(:all, :params=>{'shape'=>'capsule'}) # BROKERZED
=> NoMethodError: undefined method `name' for nil:NilClass
=end
begin
require 'active_resource'
rescue LoadError
begin
require 'rubygems'
require 'active_resource'
rescue LoadError
abort <<-ERROR
The 'activeresource' library could not be loaded. If you have RubyGems
installed you can install ActiveResource by doing "gem install activeresource".
ERROR
end
end
# Version check
module Pillbox
ARES_VERSIONS = ['2.3.4', '2.3.5']
end
require 'active_resource/version'
unless Pillbox::ARES_VERSIONS.include?(ActiveResource::VERSION::STRING)
abort <<-ERROR
ActiveResource version #{Pillbox::ARES_VERSIONS.join(' or ')} is required.
ERROR
end
# Patch ActiveResource
# handle a weird disclaimer message that is in XML
module ActiveResource
class Base
def self.instantiate_collection(collection, prefix_options = {})
if collection.is_a?(Hash) && collection.size == 1
value = collection.values.first
if value.is_a?(Array)
value.collect! { |record| instantiate_record(record, prefix_options) }
else
[ instantiate_record(value, prefix_options) ]
end
else
# strip extra layer off the front end (a disclaimer)
(d,disclaimer), (p,collection) = collection.sort
# ensure type Array
collection = collection.is_a?(Array) ? collection : Array[collection]
collection.collect! { |record| instantiate_record(record, prefix_options) }
end
end
end
end
class PillboxResource < ActiveResource::Base
self.site = "http://pillbox.nlm.nih.gov/PHP/pillboxAPIService.php"
SHAPES = {
'BULLET'=> 'C48335',
'CAPSULE'=> 'C48336',
'CLOVER'=> 'C48337',
'DIAMOND'=> 'C48338',
'DOUBLE_CIRCLE'=> 'C48339',
'FREEFORM'=> 'C48340',
'GEAR'=> 'C48341',
'HEPTAGON'=> 'C48342',
'HEXAGON'=> 'C48343',
'OCTAGON'=> 'C48344',
'OVAL'=> 'C48345',
'PENTAGON'=> 'C48346',
'RECTANGLE'=> 'C48347',
'ROUND'=> 'C48348',
'SEMI_CIRCLE'=> 'C48349',
'SQUARE'=> 'C48350',
'TEAR'=> 'C48351',
'TRAPEZOID'=> 'C48352',
'TRIANGLE'=> 'C48353'
}
SHAPE_CODES = SHAPES.invert
COLORS = {
'BLACK'=> 'C48323',
'BLUE'=> 'C48333',
'BROWN'=> 'C48332',
'GRAY'=> 'C48324',
'GREEN'=> 'C48329',
'ORANGE'=> 'C48331',
'PINK'=> 'C48328',
'PURPLE'=> 'C48327',
'RED'=> 'C48326',
'TURQUOISE'=> 'C48334',
'WHITE'=> 'C48325',
'YELLOW'=> 'C48330'
}
COLOR_CODES = COLORS.invert
cattr_accessor :api_key
def find(first, options={})
super first, interpret_params(options)
end
def interpret_params(options = {})
params = options['params'] || {}
params['key'] ||= self.api_key
begin
params['color'] = case params['color']
when NilClass;
when Array; params['color'].join(";")
when /^(\d|[a-f]|[A-F])+/; params['color'] # valid hex
else; COLORS[params['color'].upcase]
end
rescue
# "color not found"
end
begin
params['shape'] = case params['shape']
when NilClass;
when Array; params['color'].join(";")
when /^(\d|[a-f]|[A-F])+/; params['shape'] # valid hex
else; SHAPES[params['shape'].upcase]
end
rescue # NoMethodError => e
# raise X if e.match "shape not found"
end
# todo: prodcode
params.delete_if {|k,v| v.nil? }
options.merge!(params)
end
def shape # handle multi-color (OUTPUT ONLY)
return nil unless attributes['SPLSHAPE']
attributes['SPLSHAPE'].split(";").map do |shape_code|
SHAPE_CODES[shape_code] || shape_code
end
end
def color
return nil unless attributes['SPLCOLOR']
attributes['SPLCOLOR'].split(";").map do |color_code|
COLOR_CODES[color_code] || shape_code
end
end
def description; attributes['RXSTRING'] end
def prodcode; attributes['PRODUCT_CODE'] end
def product_code; attributes['PRODUCT_CODE'] end
def has_image?; attributes['HAS_IMAGE'] == '1' end
def ingredients; attributes['INGREDIENTS'].split(";") end
def size; attributes['SPLSIZE'].to_f end
def image_id; attributes['image_id'] end
def image_url(image_size = 'super_small')
unless image_id
return nil
end
case image_size
when "super_small"; "http://pillbox.nlm.nih.gov/assets/super_small/#{image_id}ss.png"
when "small"; "http://pillbox.nlm.nih.gov/assets/small/#{image_id}sm.jpg"
end
end
def imprint; attributes['splimprint'] end
end