Class: Adparlor::Facebook::GraphApi::AdImage

Inherits:
GraphObject
  • Object
show all
Includes:
Fields::AdImage, Traits::Methods
Defined in:
lib/adparlor/facebook/graph_api/ad_image.rb

Constant Summary

Constants included from Fields::AdImage

Fields::AdImage::CREATE_FIELDS, Fields::AdImage::FIELDS

Constants included from Fields::FieldDecorator

Fields::FieldDecorator::GLOBAL_FIELDS

Instance Attribute Summary

Attributes inherited from GraphObject

#access_token

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Traits::Methods

#create, #destroy, included, #update

Methods inherited from GraphObject

#batch, create, #create, data_pass_through, destroy, #destroy, get, #initialize, parse_data_for_collection, read, respond_for_data, #to_hash, #to_json, #update, update

Methods included from Api

#base_uri, #conn, #conn_multi, #delete, #get, #proxy_api_key

Methods included from Fields::FieldDecorator

included

Constructor Details

This class inherits a constructor from Adparlor::Facebook::GraphApi::GraphObject

Class Method Details

.get_content_type(header_content_type, file_name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 68

def get_content_type(header_content_type, file_name)
  content_type = MIME::Types.type_for(file_name)

  return content_type.first.content_type if !file_name.include?('safe_image.php') && !content_type.nil?

  header_content_type.empty? ? nil : header_content_type
end

.get_preferred_extension(content_type) ⇒ Object



64
65
66
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 64

def get_preferred_extension(content_type)
  MIME::Types[content_type].first.preferred_extension
end

.upload_file_with_content_type(normalized_file_name, file, content_type, extension) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 54

def upload_file_with_content_type(normalized_file_name, file, content_type, extension)
  tmp_file = Tempfile.new([File.basename(normalized_file_name), ".#{extension}"])

  tmp_file.binmode
  tmp_file.write(file.read)
  tmp_file.rewind

  Faraday::UploadIO.new(tmp_file.path, content_type)
end

Instance Method Details

#formatted_imageObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 14

def formatted_image
  return unless @source
  if %r{https?}.match(@source)
    file = open(@source)

    # return nil if we can't open the file from the url
    return nil if file.nil?

    content_type = AdImage.get_content_type(file.content_type, normalized_file_name)
    extension = AdImage.get_preferred_extension(content_type)

    # if content type or extension doesn't exist, the upload will fail on FB side
    return nil if content_type.nil? || extension.nil?

    @source = AdImage.upload_file_with_content_type(normalized_file_name, file, content_type, extension)
  else
    @source = Faraday::UploadIO.new(normalized_file_name, MIME::Types.type_for(normalized_file_name).first.content_type)
  end
end

#pathObject

Raises:



34
35
36
37
38
39
40
41
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 34

def path
  raise FbError.new('required parameter account_id missing', 500) unless 
  if .to_s.include?('act_')
    "/#{}/adimages"
  else
    "/act_#{}/adimages"
  end
end

#post(path, options, method = nil) ⇒ Object

Raises:



43
44
45
46
47
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 43

def post(path, options, method = nil)
  raise FbError.new('update not available', 500) if method == 'UPDATE'
  formatted_image
  super path, options
end

#update_pathObject



49
50
51
# File 'lib/adparlor/facebook/graph_api/ad_image.rb', line 49

def update_path
  path
end