diff options
| author | Robert | 2017-07-21 15:59:34 +0200 | 
|---|---|---|
| committer | Robert | 2017-07-21 16:32:47 +0200 | 
| commit | ded817a7d0802cc2aafcdbe4bb256698f6868afd (patch) | |
| tree | 30ac61f0f8483dc36d470da28a786eb091e5f0ff /lib | |
| parent | 4fffff70d5dc57929653ed1d1a1ce68e9769cee4 (diff) | |
| download | chouette-core-ded817a7d0802cc2aafcdbe4bb256698f6868afd.tar.bz2 | |
Refs: #3507@4.5h spec setup and refactoring, using Faraday in worker
- Setting up correct headers for the Webmock request (Oh Boy)
- Refactoring all Faraday requests into `lib/af83/http_fetcher.rb`
- Implementing the Download
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/af83/http_fetcher.rb | 21 | 
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/af83/http_fetcher.rb b/lib/af83/http_fetcher.rb new file mode 100644 index 000000000..514dd1dc9 --- /dev/null +++ b/lib/af83/http_fetcher.rb @@ -0,0 +1,21 @@ +module AF83 +  class HTTPFetcher +    def self.get_resource(*args) +      new.get_resource(*args) +    end + +    def get_resource(host:, path:, token: nil, params: {}, parse_json: false)  +      Faraday.new(url: host) do |c| +        c.headers['Authorization'] = "Token token=#{token.inspect}" if token +        c.adapter  Faraday.default_adapter + +        resp = c.get path, params +        if resp.status == 200 +          return parse_json ? JSON.parse(resp.body) : resp.body +        else +          raise "Error on api request status : #{resp.status} => #{resp.body}" +        end +      end +    end +  end +end  | 
