Class: OpengraphTransporter::Common

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

Constant Summary collapse

FB_GRAPH_HOST =
"https://graph.facebook.com"

Class Method Summary collapse

Class Method Details

.get_app_token(app_id, app_secret) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/opengraph_transporter/common.rb', line 89

def get_app_token(app_id, app_secret)
  params = {
    :client_id => app_id,
    :client_secret => app_secret,
    :grant_type => 'client_credentials'
  }
  path = "/oauth/access_token"
  response = fb_graph_call(path, params, {:format => :text})
  
  if response.nil?
    say("<%= color('Invalid Data:', RED, BOLD) %> please recheck source and destination app_id and app_secret.")
    exit
  end

  dummy = Addressable::URI.new
  dummy.query = response
  access_token = dummy.query_values["access_token"]

  return access_token
end

.get_application_details(app_token, app_id) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/opengraph_transporter/common.rb', line 52

def get_application_details(app_token, app_id)
  fql = %Q{select display_name, description, link, namespace from application where app_id = #{app_id}}
  params = {:access_token => app_token, :q => fql}
  request = 'fql'
  response = fb_graph_call(request, params)
  return response['data'][0]
end

.get_application_name(app_token, app_id) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/opengraph_transporter/common.rb', line 77

def get_application_name(app_token, app_id)
  params = {:access_token => app_token}
  request = "#{app_id}"
  response = fb_graph_call(request, params)
  if response.nil?
    show_arguments_info(translation)
    say("<%= color('Invalid application data, please recheck application and locale settings.', RED, BOLD) %>")
    exit
  end
  return response['name']
end

.get_application_translations(app_token, app_locale, include_hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/opengraph_transporter/common.rb', line 28

def get_application_translations(app_token, app_locale, include_hash)
  if include_hash
    fql = %Q{select native_hash, native_string, best_string FROM translation WHERE locale = "#{app_locale}"}
    mappings = {"native_hash" => "native_hash", "native_string" => "native", "best_string" => "translation"}
  else
    fql = %Q{select native_string, best_string FROM translation WHERE locale = "#{app_locale}"}
    mappings = {"native_string" => "native", "best_string" => "translation"}
  end
  
  params = {:access_token => app_token, :q => fql}
  request = 'fql'
  app_translations = fb_graph_call(request, params)['data']
  @app_translations = Array.new
  
  app_translations.each do |translation| 
    @app_translations.push(Hash[translation.map {|k, v| [mappings[k], v] }])
  end
   
  if !include_hash
    @app_translations = @app_translations.uniq!
  end
  return @app_translations
end

.remove_empty_translations(translation) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/opengraph_transporter/common.rb', line 19

def remove_empty_translations(translation)
  say(".....remove empty translations")
   translation[:dst_translation_arr].each_with_index do |dst_translation, index| 
     if dst_translation[:translation].empty?
       translation[:dst_translation_arr].delete_at(index)
     end 
  end
end

.show_arguments_info(translation) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/opengraph_transporter/common.rb', line 68

def show_arguments_info(translation)
  say("<%= color('\n***********************************************************************************************************', YELLOW, BOLD) %>")
  say("Source Application ID:      <%= color('#{Base.translation[:source_application_id]}', YELLOW, BOLD) %>     Application Name: <%= color('#{Base.translation[:src_app_name]}', YELLOW, BOLD) %>")
  say("Destination Application ID: <%= color('#{Base.translation[:destination_application_id]}', YELLOW, BOLD) %>     Application Name: <%= color('#{Base.translation[:dst_app_name]}', YELLOW, BOLD) %>")
  say("Primary Locale:      <%= color('#{Base.translation[:primary_locale]}', YELLOW, BOLD) %>")
  say("Selected Locale:      <%= color('#{Base.translation[:app_locale]}', YELLOW, BOLD) %>")
  say("<%= color('***********************************************************************************************************\n', YELLOW, BOLD) %>")
end

.show_translations_info(translation) ⇒ Object



60
61
62
63
64
65
# File 'lib/opengraph_transporter/common.rb', line 60

def show_translations_info(translation)
  say("<%= color('\n***********************************************************************************************************', YELLOW, BOLD) %>")
  say("Source Application ID:      <%= color('#{translation[:source_application_id]}', BOLD) %>    Existing translations: <%= color('#{translation[:src_translation_arr].length}', YELLOW, BOLD) %>    Application Name: <%= color('#{translation[:src_app_name]}', BOLD) %>")
  say("Destination Application ID: <%= color('#{translation[:destination_application_id]}', BOLD) %>    Existing translations: <%= color('#{translation[:dst_translation_arr].length}', YELLOW, BOLD) %>    Application Name: <%= color('#{translation[:dst_app_name]}', BOLD) %>")
  say("<%= color('***********************************************************************************************************\n', YELLOW, BOLD) %>")
end

.update_destination_translations(translation) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/opengraph_transporter/common.rb', line 8

def update_destination_translations(translation)
  say(".....updating translation text")
  translation[:dst_translation_arr].each do |dst_translation| 
    translation[:src_translation_arr].each do |src_translation| 
      if dst_translation[:native].eql?(src_translation['native'])
        dst_translation[:translation] = src_translation['translation']
      end
    end
  end
end