Module: GiveyRuby::Shared::InstanceMethods

Defined in:
lib/givey_ruby/shared.rb

Instance Method Summary collapse

Instance Method Details

#api_clientObject



15
16
17
# File 'lib/givey_ruby/shared.rb', line 15

def api_client
  @api_client ||= GiveyRuby.configuration.client
end

#api_versionObject



19
20
21
# File 'lib/givey_ruby/shared.rb', line 19

def api_version
  @api_version ||= GiveyRuby.configuration.api_version
end

#convert_files_for_upload(value) ⇒ Object

replace avatar object (ActionDispatch::Http::UploadedFile) with UploadIO object



69
70
71
72
73
74
75
76
# File 'lib/givey_ruby/shared.rb', line 69

def convert_files_for_upload(value)
  if value.kind_of?(ActionDispatch::Http::UploadedFile)
    upload_io = UploadIO.new(value.tempfile.path, value.content_type, value.original_filename)
    return upload_io
  else
    false
  end
end

#find_files_for_conversion(body, parents = []) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/givey_ruby/shared.rb', line 54

def find_files_for_conversion(body, parents = [])
  @body ||= body
  body.each do |param, value|
    if value.kind_of?(Hash)
      parents.push(param)
      find_files_for_conversion(value, parents)
    else
      if upload_io = convert_files_for_upload(value)
        eval("@body[:#{parents.map(&:to_sym).join("][:")}][:#{param}] = upload_io")
      end
    end
  end
end

#get_token_response(url) ⇒ Object

CLIENT CREDENTIALS ACCESS



38
39
40
# File 'lib/givey_ruby/shared.rb', line 38

def get_token_response(url)
  JSON.parse(access_token.get("/#{api_version}#{url}").body)
end

#post_token_response(url, body, headers = {}) ⇒ Object



42
43
44
45
46
# File 'lib/givey_ruby/shared.rb', line 42

def post_token_response(url, body, headers = {})
  body      = find_files_for_conversion(body)
  response  = access_token.post("/#{api_version}#{url}", {body: body, headers: headers})
  JSON.parse(response.body)
end

#put_token_response(url, body, headers = {}) ⇒ Object



48
49
50
51
52
# File 'lib/givey_ruby/shared.rb', line 48

def put_token_response(url, body, headers = {})
  body      = find_files_for_conversion(body)
  response  = access_token.put("/#{api_version}#{url}", {body: body, headers: headers})
  JSON.parse(response.body)
end

#token_from_fileObject



23
24
25
26
27
# File 'lib/givey_ruby/shared.rb', line 23

def token_from_file
  if File.exists?(GiveyRuby.configuration.token_file)
    File.read(GiveyRuby.configuration.token_file).gsub(/\n/, '')
  end
end

#token_to_file(token_string) ⇒ Object



29
30
31
32
# File 'lib/givey_ruby/shared.rb', line 29

def token_to_file(token_string)
  File.open(GiveyRuby.configuration.token_file, 'w'){|f| f.write(token_string) }
  token_string
end