aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iev_api/client.rb
blob: 9fb9c94c328a52a8392531ec1ee06ecb1a45914e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
102
103
104
105
106
107
108
109
module IevApi
  class Client

    PER_PAGE = 12
    PARALLEL_WORKERS = 10
    ACTIONS = %w{ importer exporter validator }
    IMPORT_FORMAT = %w{ neptune netex gtfs }
    EXPORT_FORMAT = %w{ neptune netex gtfs hub kml }

    attr_accessor *IevApi::Configuration::VALID_OPTIONS_KEYS
    
    def initialize(options={})
      attrs = IevApi.options.merge(options)
      IevApi::Configuration::VALID_OPTIONS_KEYS.each do |key|
        send("#{key}=", attrs[key])
      end
    end

    def url_for(endpoint, *args)
      path = case endpoint.to_s
             when 'jobs' then jobs_path(args)
             when 'job' then job_path(args)               
             when 'report' then report_path(args)
             else raise ArgumentError.new("Unrecognized path: #{path}")
             end

      [account_path, path.split('.').first].join('')
    end

    def jobs(referential_id, options = {})
      results = request(:get, jobs_path(referential_id), options)
      results.respond_to?(:jobs) ? results.jobs : []
    end
    
    def job(referential_id, job_id, options = {})
      results = request(:get, job_path(referential_id, job_id), options)
      results.respond_to?(:job) ? results.job : []
    end
    
    def jobs_path(referential_id)
      "/referentials/#{referential_id}/jobs"
    end

    def job_path(referential_id, job_id)
      "/referentials/#{referential_id}/jobs/#{job_id}"
    end
    
    def report(referential_id, report_id, options = {})
      results = request(:get, report_path(referential_id, report_id), options)
      results.respond_to?(:report) ? results.report : []
    end
    
    def report_path(referential_id, report_id)
      "/referential/#{referential_id}/job/#{report_id}"
    end

    def account_path
      "#{protocol}://#{Rails.application.config.iev_url}"      
    end

    def protocol
      @secure ? "https" : "http"
    end
    
    # Perform an HTTP request
    def request(method, path, params = {}, options = {})
      
      action_and_format = (params[:action].present? && params[:format].present?) ? "/#{params[:action]}/#{params[:format]}" : ""      
      build_path = connection.path_prefix + path + action_and_format
      puts build_path.inspect
      response = connection(options).run_request(method, nil, nil, nil) do |request|
        case method
        when :delete, :get
          request.url(build_path, params)
        when :post, :put
          request.url(build_path)
          request.body = params unless params.empty?
        end
      end

      response.body
    end

    def connection(options={})
      default_options = {
        :headers => {
          :accept => 'application/json',
          :user_agent => user_agent,
        },
        :ssl => {:verify => false},
        :url => account_path,
      }

      @connection ||= Faraday.new(default_options.deep_merge(connection_options)) do |builder|
        middleware.each { |mw| builder.use *mw }

        builder.adapter adapter
      end

      # cache_dir = File.join(ENV['TMPDIR'] || '/tmp', 'cache')

      # @connection.response :caching do  
      #   ActiveSupport::Cache::FileStore.new cache_dir, :namespace => 'iev',
      #   :expires_in => 3600  # one hour
      # end
    end
    
  end  
end