Class: G5FoundationClient::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/g5_foundation_client/fetcher.rb

Class Method Summary collapse

Class Method Details

.fetch_url(url, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/g5_foundation_client/fetcher.rb', line 2

def self.fetch_url(url, &block)
  response = HTTParty.get(
      url,
      {query:   {access_token: G5FoundationClient::access_token},
       headers: {'Content-Type' => 'application/json', 'Accept' => 'application/json'}
      }
  )

  case response.response.code.to_i
    when 200
      yield response.body
    when 404
      raise G5FoundationClient::RecordNotFoundException.new(
                "Couldn't find record at URL '#{url}'"
            )
    else
      raise G5FoundationClient::Error.new(
                "I got an unexpected response code '#{response.response.code}'"
            )
  end
end