Class: Gallery3

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-gallery3.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Gallery3

Returns a new instance of Gallery3.



9
10
11
12
13
14
15
16
# File 'lib/ruby-gallery3.rb', line 9

def initialize(options = nil)
  if options != nil
    @options = options      
  elsif
    file = File.read(Pathname.new(ENV["HOME"]).join(".gallery3.json"))
    @options = JSON.parse(file, :symbolize_names => true)
  end
end

Instance Method Details

#create_item(item_info) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby-gallery3.rb', line 54

def create_item(item_info)
  entity = JSON.generate(item_info[:entity])

  item_id = @options[:rootItemId]
  item_id = item_info[:target_item][:id] if !item_info[:target_item].nil?
  
  url = "#{@options[:host]}#{@options[:base]}/rest/item/#{item_id}"
  
  response = RestClient::Request.execute(
    :method => :post, 
    :url => url, 
    :headers => {"X-Gallery-Request-Method" => "post"," X-Gallery-Request-Key" => @options[:requestKey]},
    :payload => {'entity' => entity})

  return JSON.parse(response, :symbolize_names => true)
end

#find_item(item) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby-gallery3.rb', line 36

def find_item(item)
  # url = "#{@options['host']}#{@options['base']}/rest/item/#{item[:id]}"
  # response = RestClient.get url, :headers => {"X-Gallery-Request-Key" => @options["requestKey"], "X-Gallery-Request-Method" => "get"}
  # item = JSON.parse(response)
  # return item
  url = "#{@options[:host]}#{@options[:base]}/rest/item/#{item[:id]}"
  
  response = RestClient::Request.execute(
    :method => :get, 
    :url => url, 
    :headers => {
      "X-Gallery-Request-Method" => "get", 
      "X-Gallery-Request-Key" => @options[:requestKey]
      })
  return JSON.parse(response, :symbolize_names => true)
  
end

#image_url_public?(item) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/ruby-gallery3.rb', line 104

def image_url_public?(item)
  return find_item(item)[:entity][:file_url_public]
end

#login?(user_info = nil) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby-gallery3.rb', line 18

def login? ( = nil)
  url = "#{@options[:host]}#{@options[:base]}/rest"
  # response = RestClient.post(url, 
  #       {'user' => userInfo[:user], 'password' => userInfo[:password]},
  #       {"Content-Type" => "application/x-www-form-urlencoded", "X-Gallery-Request-Method" => "post"} )
   = {
    :user => @options[:user],
    :password => @options[:password]
  }
  
  response = RestClient::Request.execute(
    :method => :post, 
    :url => url, 
    :headers => {"Content-Type" => "application/x-www-form-urlencoded", "X-Gallery-Request-Method" => "post"},
    :payload => )
  return response
end

#upload_file(upload_info) ⇒ Object



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
# File 'lib/ruby-gallery3.rb', line 71

def upload_file(upload_info)
  # entity = JSON.generate(uploadInfo[:entity])
  item_id = @options[:rootItemId]
  item_id = upload_info[:target_item][:id] if !upload_info[:target_item].nil?
        
  entity = {
    :title => File.basename(upload_info[:file], File.extname(upload_info[:file])),
    :name => File.basename(upload_info[:file]).downcase.gsub(" ", "_"),
    :type => 'photo'
  }    

  upload_info_entity = upload_info[:entity]
  
  if !upload_info_entity.nil? then
    entity[:title] = upload_info_entity[:title] if !upload_info_entity[:title].nil?
    entity[:name] = upload_info_entity[:name] if !upload_info_entity[:name].nil?    
  end
      
  url = "#{@options[:host]}#{@options[:base]}/rest/item/#{item_id}"
  
  response = RestClient::Request.execute(
    :method => :post, 
    :url => url, 
    :headers => {"X-Gallery-Request-Method" => "post"," X-Gallery-Request-Key" => @options[:requestKey], 'Content-Type' => 'multipart/form-data'},
    :payload => {
      'entity' => JSON.generate(entity),
      :multipart => true, 
      :file  => File.new(upload_info[:file], 'rb')}
    ) 
        
  return JSON.parse(response, :symbolize_names => true)    
end