aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iev_api/client.rb
blob: 1994b1027c7a315718d42f4508bab758be320b93 (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
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 'scheduled_job' then scheduled_job_path(args)               
             when 'terminated_job' then terminated_job_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)
    end

    def jobs_path(referential_id)
      "referentials/#{referential_id}/jobs"
    end
    
    def scheduled_job(referential_id, job_id, options = {})
      results = request(:get, scheduled_job_path(referential_id, job_id), options)    
    end

    def scheduled_job_path(referential_id, job_id)
      "referentials/#{referential_id}/scheduled_jobs/#{job_id}"
    end

    def terminated_job(referential_id, job_id, options = {})
      results = request(:get, terminated_job_path(referential_id, job_id), options)    
    end   

    def terminated_job_path(referential_id, job_id)
      "referentials/#{referential_id}/terminated_jobs/#{job_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 = {})

      response = connection(options).run_request(method, nil, nil, nil) do |request|
        case method
        when :delete, :get
          request.url(path, params)
        when :post, :put
          request.url(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(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
      
      @connection      
    end
    
  end  
end