Class: Refine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Refine

Returns a new instance of Refine.



64
65
66
# File 'lib/google_refine.rb', line 64

def initialize(url)
  self.url = url
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



62
63
64
# File 'lib/google_refine.rb', line 62

def url
  @url
end

Instance Method Details

#create_importing_jobObject



75
76
77
78
79
# File 'lib/google_refine.rb', line 75

def create_importing_job
  response = RestClient.post("#{self.url}/command/core/create-importing-job", nil)
  job_id = JSON[response]["jobID"]
  Job.new(self, job_id)
end

#create_project(filename, param = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/google_refine.rb', line 87

def create_project(filename, param = {})

  param[:project_name] ||= File.basename(filename)

  if self.version >= "2.5"
    begin
      options = {}
      options[:projectName]            = param[:project_name]
      options[:encoding]               = param[:encoding]                  || "UTF-8"
      options[:separator]              = param[:separator]                 || "\t"
      options[:headerLines]            = param[:header_lines]              || 1
      options[:limit]                  = param[:limit]
      options[:guessCellValueTypes]    = param[:guess_value_type]          || false
      options[:processQuotes]          = true

      job = create_importing_job
      job.load_raw_data(filename)
      project = job.create_project(options)
      project
    ensure
      job.cancel if job
    end
  elsif [ "2.0", "2.1" ].include?(self.version)
    begin
      RestClient.post("#{self.url}/command/core/create-project-from-upload",
        Hash[
         {  project_name:      param[:project_name],
            header_lines:      param[:header_lines],
            limit:             param[:limit],
            guess_value_type:  param[:guess_value_type],
            ignore_quotes:     ! param[:process_quotes],
            project_file:  File.new(filename, "rb")
          }.map { |key, value| [ key.to_s.gsub('_', '-'), value ] }
        ]
      )
    rescue RestClient::Found
      project_id = $!.response.headers[:location].match(/project=(\d+)/)[1]
      Project.new(self, project_id)
    end
  end

end

#versionObject



81
82
83
84
# File 'lib/google_refine.rb', line 81

def version
  response = RestClient.get("#{self.url}/command/core/get-version")
  JSON[response]['version']
end