Module: LinkedData::Client::HTTP

Defined in:
lib/ontologies_api_client/http.rb

Defined Under Namespace

Classes: Link

Class Method Summary collapse

Class Method Details

.connObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/ontologies_api_client/http.rb', line 42

def self.conn
  unless LinkedData::Client.connection_configured?
    if Kernel.const_defined?("Rails")
      rails = Kernel.const_get("Rails")
      store = rails.cache if rails.cache
    end
    LinkedData::Client.config_connection(cache_store: store)
  end
  LinkedData::Client.settings.conn
end

.delete(id) ⇒ Object

Raises:

  • (Exception)


132
133
134
135
136
# File 'lib/ontologies_api_client/http.rb', line 132

def self.delete(id)
  puts "Deleting #{id}" if $DEBUG
  response = conn.delete id
  raise Exception, response.body if response.status >= 500
end

.get(path, params = {}, options = {}) ⇒ Object



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
# File 'lib/ontologies_api_client/http.rb', line 53

def self.get(path, params = {}, options = {})
  headers = options[:headers] || {}
  raw = options[:raw] || false # return the unparsed body of the request
  params = params.delete_if {|k,v| v == nil || v.to_s.empty?}
  params[:ncbo_cache_buster] = Time.now.to_f if raw # raw requests don't get cached to ensure body is available

  begin
    puts "Getting: #{path} with #{params}" if $DEBUG
    begin
      response = conn.get do |req|
        req.url path
        req.params = params
        req.options[:timeout] = 60
        req.headers.merge(headers)
      end
    rescue Exception => e
      params = Faraday::Utils.build_query(params)
      path << "?" unless params.empty? || path.include?("?")
      raise e, "Problem retrieving:\n#{path}#{params}\n\nError: #{e.message}\n#{e.backtrace.join("\n\t")}"
    end

    raise Exception, "Problem retrieving:\n#{path}\n#{response.body}" if response.status >= 500

    if raw
      obj = response.body
    elsif response.respond_to?(:parsed_body) && response.parsed_body
      obj = response.parsed_body
      obj = obj.dup if obj.frozen?
    else
      obj = recursive_struct(load_json(response.body))
    end
  rescue Exception => e
    puts "Problem getting #{path}" if $DEBUG
    raise e
  end
  obj
end

.get_batch(paths, params = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ontologies_api_client/http.rb', line 91

def self.get_batch(paths, params = {})
  responses = []
  if conn.in_parallel?
    conn.in_parallel do
      paths.each {|p| responses << conn.get(p, params) }
    end
  else
    responses = threaded_request(paths, params)
  end
  return responses
end

.object_from_json(json) ⇒ Object



138
139
140
# File 'lib/ontologies_api_client/http.rb', line 138

def self.object_from_json(json)
  recursive_struct(load_json(json))
end

.patch(path, obj) ⇒ Object

Raises:

  • (Exception)


123
124
125
126
127
128
129
130
# File 'lib/ontologies_api_client/http.rb', line 123

def self.patch(path, obj)
  file, file_attribute = params_file_handler(obj)
  response = conn.patch do |req|
    req.url path
    custom_req(obj, file, file_attribute, req)
  end
  raise Exception, response.body if response.status >= 500
end

.post(path, obj) ⇒ Object

Raises:

  • (Exception)


103
104
105
106
107
108
109
110
111
# File 'lib/ontologies_api_client/http.rb', line 103

def self.post(path, obj)
  file, file_attribute = params_file_handler(obj)
  response = conn.post do |req|
    req.url path
    custom_req(obj, file, file_attribute, req)
  end
  raise Exception, response.body if response.status >= 500
  recursive_struct(load_json(response.body))
end

.put(path, obj) ⇒ Object

Raises:

  • (Exception)


113
114
115
116
117
118
119
120
121
# File 'lib/ontologies_api_client/http.rb', line 113

def self.put(path, obj)
  file, file_attribute = params_file_handler(obj)
  response = conn.put do |req|
    req.url path
    custom_req(obj, file, file_attribute, req)
  end
  raise Exception, response.body if response.status >= 500
  recursive_struct(load_json(response.body))
end