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



52
53
54
# File 'lib/global_registry/base.rb', line 52

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

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



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

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



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

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

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



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

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

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



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

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

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



72
73
74
# File 'lib/global_registry/base.rb', line 72

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

Instance Method Details

#default_pathObject



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

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

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



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

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

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



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

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



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

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

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



26
27
28
# File 'lib/global_registry/base.rb', line 26

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

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



30
31
32
33
34
# File 'lib/global_registry/base.rb', line 30

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



112
113
114
# File 'lib/global_registry/base.rb', line 112

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

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



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

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

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



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

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

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



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
102
103
104
105
106
# File 'lib/global_registry/base.rb', line 76

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: nil).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: nil).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: nil,
                headers: default_headers.merge(headers)}
    RestClient::Request.execute(get_args) { |response, request, result, &block|
      handle_response(response, request, result)
    }
  end
end