Class: WolframDatabin::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wolfram_databin/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



10
11
12
# File 'lib/wolfram_databin/base.rb', line 10

def initialize
  self.debug_flag=false
end

Instance Attribute Details

#clntObject

Returns the value of attribute clnt.



5
6
7
# File 'lib/wolfram_databin/base.rb', line 5

def clnt
  @clnt
end

#debug_flagObject

Returns the value of attribute debug_flag.



5
6
7
# File 'lib/wolfram_databin/base.rb', line 5

def debug_flag
  @debug_flag
end

#shortidObject

Returns the value of attribute shortid.



5
6
7
# File 'lib/wolfram_databin/base.rb', line 5

def shortid
  @shortid
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/wolfram_databin/base.rb', line 5

def uri
  @uri
end

Class Method Details

.submit_data(tshortid, aquery) ⇒ Object



26
27
28
29
30
31
# File 'lib/wolfram_databin/base.rb', line 26

def self.submit_data(tshortid,aquery)
  dbin=WolframDatabin::Base.new
  dbin.set_shortid(tshortid)
  puts "WolframDatabin Gem: posting #{dbin.datadrop_url} with #{aquery.inspect}  at #{Time.now}"
  dbin.submit_data(tshortid,aquery)
end

Instance Method Details

#build_clientObject



47
48
49
50
51
52
53
# File 'lib/wolfram_databin/base.rb', line 47

def build_client
  if @clnt==nil then
  # @clnt=HTTPClient.new 
   @clnt=Hurley::Client.new  self.get_url2
  end
  @clnt
end

#datadrop_urlObject



16
17
18
# File 'lib/wolfram_databin/base.rb', line 16

def datadrop_url
  "http://wolfr.am/#{self.shortid}"
end

#debugObject



13
14
15
# File 'lib/wolfram_databin/base.rb', line 13

def debug
  self.debug_flag=true
end

#fix_query(query) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/wolfram_databin/base.rb', line 32

def fix_query(query)
  #need to remove underscores
  fixed={}
  query.each {|k,v|   fixed[k.to_s.gsub('_',"U")]=v}
 # puts "fixed is #{fixed}  query is #{query}"
  fixed
  
end

#get_urlObject



19
20
21
22
# File 'lib/wolfram_databin/base.rb', line 19

def get_url
  url="https://datadrop.wolframcloud.com/api/v1.0/Add?"
  url
end

#get_url2Object



23
24
25
# File 'lib/wolfram_databin/base.rb', line 23

def get_url2
  "https://datadrop.wolframcloud.com/api/v1.0"
end

#post_data(query) ⇒ Object



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
# File 'lib/wolfram_databin/base.rb', line 54

def post_data(query)
raise "short id not set" if self.shortid==nil
  res=''
  qdata={bin: self.shortid}.merge(self.fix_query(query))
  begin
    timeout=40
     # @uri=URI.parse(self.get_url)
    # c = Hurley::Client.new "https://datadrop.wolframcloud.com/api/v1.0"
    # c.get "Add", :time => Time.now.iso8601, :fred => "hello"
     # q=self.get_url + Hurley::Query::Flat.new(qdata).to_s
    #  puts "url #{q} query #{qdata}" if self.debug_flag
     # res=Hurley.get(q)
     @clnt=self.build_client
     @clnt.request_options.timeout = timeout   # set to 60
     
     res=@clnt.get "Add", qdata
   rescue  Exception => e
     puts "Exception: #{e.inspect}"
   ensure
      puts "result: #{res.inspect}   query #{qdata} bin: #{self.shortid}  url: #{@uri.inspect}" if self.debug_flag
   end
   #if res.is_a?(HTTP::Message)
   if res.is_a?(Hurley::Response) 
      return {success: res.body.include?('successfully added'), result: res} 
    else
      return {success: false, result: res}   
    end
end

#set_shortid(atid) ⇒ Object



7
8
9
# File 'lib/wolfram_databin/base.rb', line 7

def set_shortid(atid)
  self.shortid=atid
end

#submit_data(tshortid, aquery) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/wolfram_databin/base.rb', line 40

def submit_data(tshortid,aquery)
  self.set_shortid(tshortid)
 #   puts "INSPECT  POST #{self.shortid} query: #{aquery}"
 # self.debug
  res=self.post_data(aquery)
  res
end