Class: Intrinio::WebAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/intrinio/web_api.rb

Overview

A general purpose HTTP wrapper. This is poor man’s HTTParty with dynamic methods.

Direct Known Subclasses

API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ WebAPI

Returns a new instance of WebAPI.



13
14
15
# File 'lib/intrinio/web_api.rb', line 13

def initialize(base_url)
  @base_url = base_url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &_block) ⇒ Object

Allow using any method as the first segment of the path object.user ‘details’ becomes object.get ‘user/details’



19
20
21
# File 'lib/intrinio/web_api.rb', line 19

def method_missing(method_sym, *arguments, &_block)
  get "/#{method_sym}", *arguments
end

Instance Attribute Details

#after_request_blockObject (readonly)

Returns the value of attribute after_request_block.



10
11
12
# File 'lib/intrinio/web_api.rb', line 10

def after_request_block
  @after_request_block
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/intrinio/web_api.rb', line 10

def base_url
  @base_url
end

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/intrinio/web_api.rb', line 11

def debug
  @debug
end

#formatObject

Returns the value of attribute format.



11
12
13
# File 'lib/intrinio/web_api.rb', line 11

def format
  @format
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



10
11
12
# File 'lib/intrinio/web_api.rb', line 10

def last_error
  @last_error
end

Instance Method Details

#after_request(&block) ⇒ Object

Set a block to be executed after the request. This is called only when using get and not when using get!. Good for JSON decoding, for example.



39
40
41
# File 'lib/intrinio/web_api.rb', line 39

def after_request(&block)
  @after_request_block = block
end

#cacheObject



32
33
34
# File 'lib/intrinio/web_api.rb', line 32

def cache
  @cache ||= WebCache.new
end

#construct_url(path, params = {}) ⇒ Object

Build a URL from all its explicit and implicit pieces.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/intrinio/web_api.rb', line 76

def construct_url(path, params={})
  path = "/#{path}" unless path[0] == '/'
  all_params = default_params.merge params
  result = "#{base_url}#{path}"
  result = "#{result}.#{format}" if format && File.extname(result) == ''
  unless all_params.empty?
    all_params = URI.encode_www_form all_params
    result = "#{result}?#{all_params}"
  end
  result
end

#default_paramsObject



88
89
90
# File 'lib/intrinio/web_api.rb', line 88

def default_params
  @default_params ||= {}
end

#get(path, extra = nil, params = {}) ⇒ Object

Return the response from the API.



44
45
46
47
48
# File 'lib/intrinio/web_api.rb', line 44

def get(path, extra=nil, params={})
  response = get! path, extra, params
  response = after_request_block.call(response) if after_request_block
  response
end

#get!(path, extra = nil, params = {}) ⇒ Object

Return the response from the API, without executing the after_request block.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/intrinio/web_api.rb', line 52

def get!(path, extra=nil, params={})
  if extra.is_a?(Hash) and params.empty?
    params = extra
    extra = nil
  end
  
  path = "#{path}/#{extra}" if extra
  url = construct_url path, params

  return url if debug 

  response = cache.get(url)
  @last_error = response.error
  response.content
end

#param(params) ⇒ Object

Add one or more parameter to the default query string. Good for adding keys that are always needed, like API keys.



25
26
27
28
29
30
# File 'lib/intrinio/web_api.rb', line 25

def param(params)
  params.each do |key, value|
    default_params[key] = value
    default_params.delete key if value.nil?
  end
end

#save(filename, path, params = {}) ⇒ Object

Save the response from the API to a file.



69
70
71
72
73
# File 'lib/intrinio/web_api.rb', line 69

def save(filename, path, params={})
  response = get! path, nil, params
  return response if debug
  File.write filename, response.to_s
end