Class: Clinical::Trial

Inherits:
Object
  • Object
show all
Includes:
HTTParty, HappyMapper
Defined in:
lib/clinical/trial.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



54
55
56
# File 'lib/clinical/trial.rb', line 54

def categories
  @categories
end

#keywordsObject (readonly)

Returns the value of attribute keywords.



53
54
55
# File 'lib/clinical/trial.rb', line 53

def keywords
  @keywords
end

#termsObject (readonly)

Returns the value of attribute terms.



55
56
57
# File 'lib/clinical/trial.rb', line 55

def terms
  @terms
end

Class Method Details

.extract_options!Object



185
186
187
# File 'lib/clinical/trial.rb', line 185

def extract_options!
  last.is_a?(Hash) ? pop : { }
end

.find(*args) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/clinical/trial.rb', line 131

def find(*args)
  options = args.extract_options!

  options[:page] ||= 1
  options[:per_page] ||= 20

  query = query_hash_for(*[args, options])
  response = get("/search", :query => query)
  trials = Collection.create_from_results(options[:page], 
    options[:per_page], 
    response.body)

  if options[:extended]
    fetch_more_details(trials)
  else
    trials
  end
end

.find_by_id(id) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/clinical/trial.rb', line 118

def find_by_id(id)
  response = get("/show/#{id}")
  if response.code == 400
    nil
  else
    begin
      parse(response.body)
    rescue LibXML::XML::Error
      return nil
    end 
  end
end

.query_hash_for(*args) ⇒ Object



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
# File 'lib/clinical/trial.rb', line 150

def query_hash_for(*args)
  query = {}
  options = args.extract_options! || {}
  
  conditions = options[:conditions] || {}
  query["start"] = (options[:per_page] * options[:page]) - (options[:per_page] - 1)
  unless conditions[:recruiting].nil?
    query["recr"] = conditions[:recruiting] ? "open" : "closed" 
  end
  query["term"] = args.first if args.first.is_a?(String)
  
  {
    :condition => "cond", 
    :sponsor => "spons",
    :intervention => "intr",
    :outcome => "outc",
    :sponsor => "spons"
  }.each do |key,value|
    query[value] = conditions[key] unless conditions[key].nil?
  end

  unless conditions[:updated_at].nil?
    unless conditions[:updated_at].is_a?(Array)
      conditions[:updated_at] = [conditions[:updated_at]] 
    end

    query["lup_s"] = conditions[:updated_at][0].strftime("%m/%d/%Y")
    if conditions[:updated_at].size == 2
      query["lup_e"] = conditions[:updated_at][1].strftime("%m/%d/%Y") 
    end
  end

  query
end

Instance Method Details

#conditionsObject



77
78
79
80
81
82
83
# File 'lib/clinical/trial.rb', line 77

def conditions
  if condition_items.nil? || condition_items.empty?
    condition_summary.nil? ? nil : condition_summary.split(";")
  else
    condition_items
  end
end

#get_metadataObject

this metadata is not accessible in the feed so crawl the html page to get keywords, categories, and terms



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
# File 'lib/clinical/trial.rb', line 91

def 
  response = self.class.get("/show/#{id}", :query => {:displayxml => false})
  html = Nokogiri::HTML(response.body)

   = {}

  {
    :terms => 0, 
    :categories => 1,
    :keywords => 2
  }.each do |key, value|

    [key] = []
    html.search("div.indent3:nth-last-child(#{value}) td").each do |td|
      word = td.inner_html.split(/\<br\>/).collect{|i| i.gsub(/\<div.*/, "").strip.chomp}
      if word != ""
        [key] += word
      end
    end

  end

  @terms, @categories, @keywords = [:terms], [:categories], [:keywords]
  
end

#idObject



57
58
59
# File 'lib/clinical/trial.rb', line 57

def id
  self.nct_id
end

#open?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/clinical/trial.rb', line 61

def open?
  self.status && self.status.open?
end

#outcomesObject



69
70
71
# File 'lib/clinical/trial.rb', line 69

def outcomes
  @outcomes ||= [primary_outcomes, secondary_outcomes].flatten
end

#phaseObject



85
86
87
# File 'lib/clinical/trial.rb', line 85

def phase
  self.text_phase.gsub(/phase /i, "").to_i
end

#sponsorsObject



65
66
67
# File 'lib/clinical/trial.rb', line 65

def sponsors
  @sponsors ||= [lead_sponsor, (collaborators || []), (agencies || [])].flatten
end

#statusObject



73
74
75
# File 'lib/clinical/trial.rb', line 73

def status
  self.read_status || self.overall_status
end