Class: GlobalRegistry::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
# File 'lib/global_registry/base.rb', line 8

def initialize(args = {})
  @base_url = args[:base_url]
  @access_token = args[:access_token]
  @xff = args[:xff]
end

Class Method Details

.delete(id, headers = {}) ⇒ Object



47
48
49
# File 'lib/global_registry/base.rb', line 47

def self.delete(id, headers = {})
  new.delete(id, headers)
end

.delete_or_ignore(id, headers = {}) ⇒ Object



54
55
56
57
58
# File 'lib/global_registry/base.rb', line 54

def self.delete_or_ignore(id, headers = {})
  delete(id, headers)
rescue RestClient::Exception => e
  raise unless e.response.code.to_i == 404
end

.find(id, params = {}, headers = {}) ⇒ Object



14
15
16
# File 'lib/global_registry/base.rb', line 14

def self.find(id, params = {}, headers = {})
  new.find(id, params, headers)
end

.get(params = {}, headers = {}) ⇒ Object



21
22
23
# File 'lib/global_registry/base.rb', line 21

def self.get(params = {}, headers = {})
  new.get(params, headers)
end

.post(params = {}, headers = {}) ⇒ Object



33
34
35
# File 'lib/global_registry/base.rb', line 33

def self.post(params = {}, headers = {})
  new.post(params, headers)
end

.put(id, params = {}, headers = {}) ⇒ Object



40
41
42
# File 'lib/global_registry/base.rb', line 40

def self.put(id, params = {}, headers = {})
  new.put(id, params, headers)
end

.request(method, params, path = nil, headers = {}) ⇒ Object



66
67
68
# File 'lib/global_registry/base.rb', line 66

def self.request(method, params, path = nil, headers = {})
  new.request(method, params, path, headers)
end

Instance Method Details

#default_pathObject



103
104
105
# File 'lib/global_registry/base.rb', line 103

def default_path
  self.class.to_s.split('::').last.underscore.pluralize
end

#delete(id, headers = {}) ⇒ Object



50
51
52
# File 'lib/global_registry/base.rb', line 50

def delete(id, headers = {})
  request(:delete, {}, path_with_id(id), headers)
end

#delete_or_ignore(id, headers = {}) ⇒ Object



59
60
61
62
63
# File 'lib/global_registry/base.rb', line 59

def delete_or_ignore(id, headers = {})
  delete(id, headers)
rescue RestClient::Exception => e
  raise unless e.response.code.to_i == 404
end

#find(id, params = {}, headers = {}) ⇒ Object



17
18
19
# File 'lib/global_registry/base.rb', line 17

def find(id, params = {}, headers = {})
  request(:get, params, path_with_id(id), headers)
end

#get(params = {}, headers = {}) ⇒ Object



24
25
26
# File 'lib/global_registry/base.rb', line 24

def get(params = {}, headers = {})
  request(:get, params, nil, headers)
end

#get_all_pages(params = {}, headers = {}) ⇒ Object



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

def get_all_pages(params = {}, headers = {})
  results = results_from_all_pages(params, headers)
  return results unless block_given?
  results.each { |result| yield result }
end

#path_with_id(id) ⇒ Object



107
108
109
# File 'lib/global_registry/base.rb', line 107

def path_with_id(id)
  "#{default_path}/#{id}"
end

#post(params = {}, headers = {}) ⇒ Object



36
37
38
# File 'lib/global_registry/base.rb', line 36

def post(params = {}, headers = {})
  request(:post, params, nil, headers)
end

#put(id, params = {}, headers = {}) ⇒ Object



43
44
45
# File 'lib/global_registry/base.rb', line 43

def put(id, params = {}, headers = {})
  request(:put, params, path_with_id(id), headers)
end

#request(method, params, path = nil, headers = {}) ⇒ Object



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

def request(method, params, path = nil, headers = {})
  raise 'You need to configure GlobalRegistry with your access_token.' unless access_token

  url = if base_url.starts_with? 'http'
          Addressable::URI.parse(base_url)
        else
          Addressable::URI.parse("http://#{base_url}")
        end
  url.path = path || default_path
  url.query_values = headers.delete(:params) if headers[:params]

  case method
  when :post
    post_headers = default_headers.merge(content_type: :json, timeout: -1).merge(headers)
    RestClient.post(url.to_s, params.to_json, post_headers) { |response, request, result, &block|
      handle_response(response, request, result)
    }
  when :put
    put_headers = default_headers.merge(content_type: :json, timeout: -1).merge(headers)
    RestClient.put(url.to_s, params.to_json, put_headers) { |response, request, result, &block|
      handle_response(response, request, result)
    }
  else
    url.query_values = (url.query_values || {}).merge(params) if params.any?
    get_args = { method: method, url: url.to_s, timeout: -1,
                 headers: default_headers.merge(headers)
               }
    RestClient::Request.execute(get_args) { |response, request, result, &block|
      handle_response(response, request, result)
    }
  end
end