Class: Radian6::API

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

Defined Under Namespace

Classes: HarvestError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, app_key, opts = {}) ⇒ API

Returns a new instance of API.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/radian6/api.rb', line 13

def initialize(username, password, app_key, opts={})
  @auth_appkey = app_key
  opts = { :sandbox => false, :debug => false, :async => false }.merge(opts)
  @debug = opts[:debug]
  @async = opts[:async]
  @proxy = opts[:proxy]
  if opts[:sandbox]
    @endpoint = "https://demo-api.radian6.com/socialcloud/v1/"
  else
    @endpoint = "https://api.radian6.com/socialcloud/v1/"
  end

  authenticate(username, password)
  return self
end

Instance Attribute Details

#auth_appkeyObject (readonly)

Returns the value of attribute auth_appkey.



12
13
14
# File 'lib/radian6/api.rb', line 12

def auth_appkey
  @auth_appkey
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



12
13
14
# File 'lib/radian6/api.rb', line 12

def auth_token
  @auth_token
end

Instance Method Details

#eachRangeTopicPostsXML(range_start, range_end, topics = [62727], media = [1,2,4,5,8,9,10,11,12,13,16], page_size = 1000) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/radian6/api.rb', line 116

def eachRangeTopicPostsXML(range_start, range_end, topics=[62727], media=[1,2,4,5,8,9,10,11,12,13,16], page_size=1000)
  page = 1
  fetched_article_count = 0
  botched = 0
  total_count = 1
  begin
    xml         = sanitize_xml( fetchRangeTopicPostsXML(range_start, range_end, topics, media, page, page_size) )
    counter     = get_posts_counter_for(xml)
    total_count = counter.total
    fetched_article_count = (page -1) * page_size + counter.count

    yield page, xml, counter

    page += 1
  end while total_count > fetched_article_count
end

#fetchInfluencerData(topics = [], show_sentiment = 0, show_engagement = 0) ⇒ Object



138
139
140
141
# File 'lib/radian6/api.rb', line 138

def fetchInfluencerData(topics=[], show_sentiment = 0, show_engagement = 0)
  path = "data/influencerdata/#{topics.join(",")}/#{show_sentiment}/#{show_engagement}/"
  api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
end

#fetchMediaTypesObject



110
111
112
113
114
# File 'lib/radian6/api.rb', line 110

def fetchMediaTypes
  path = "lookup/mediaproviders"
  xml = api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
  return xml
end

#fetchRangeConversationCloudData(range_start, range_end, topics = [62727], media = [1,2,4,5,8,9,10,11,12,13,16], segmentation = 0) ⇒ Object



133
134
135
136
# File 'lib/radian6/api.rb', line 133

def fetchRangeConversationCloudData(range_start, range_end, topics=[62727], media=[1,2,4,5,8,9,10,11,12,13,16], segmentation=0)
  path = "data/tagclouddata/#{range_start}/#{range_end}/#{topics.join(',')}/#{media.join(',')}/#{segmentation}"
  api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
end

#fetchRangeTopicPosts(range_start, range_end, topics = [62727], media = [1,2,4,5,8,9,10,11,12,13,16], start_page = 0, page_size = 1000, dump_file = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/radian6/api.rb', line 52

def fetchRangeTopicPosts(range_start,range_end,topics=[62727],media=[1,2,4,5,8,9,10,11,12,13,16],start_page=0,page_size=1000,dump_file=false)
  # BEWARE: range_start and range_end should be UNIX epochs in milliseconds, not seconds
  xml = fetchRangeTopicPostsXML(range_start, range_end, topics, media, start_page, page_size)

  if dump_file
    log "\tDumping to file #{dump_file} at #{Time.now}"
    f = File.new(dump_file, "w")
    f.write(xml)
    f.close

    counter = Radian6::SAX::PostCounter.new 
    parser = Nokogiri::XML::SAX::Parser.new(counter)
    parser.parse(xml)
    log "\tFinished parsing the file at #{Time.now}"
    raise counter.error if counter.error
    return counter
  else
    return Radian6::Post.from_xml(xml)
  end
end

#fetchRangeTopicPostsXML(range_start, range_end, topics = [], media = [1,2,4,5,8,9,10,11,12,13,16], start_page = 1, page_size = 1000) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/radian6/api.rb', line 73

def fetchRangeTopicPostsXML(range_start, range_end, topics=[], media=[1,2,4,5,8,9,10,11,12,13,16], start_page=1, page_size=1000)
  # BEWARE: range_start and range_end should be UNIX epochs in milliseconds, not seconds
  path = "data/topicdata/realtime/#{range_start}/#{range_end}/#{topics.join(',')}/#{media.join(',')}/#{start_page}/#{page_size}?includeFullContent=1"
  log "\tGetting page #{start_page} for range #{range_start} to #{range_end} in topic #{topics.join(', ')} at #{Time.now}"
  xml = api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
  return xml
end

#fetchRealtimeRecentTopicPosts(hours = 1, topics = [62727], media = [1,2,4,5,8,9,10,11,12,13,16], start_page = 0, page_size = 1000, dump_file = false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/radian6/api.rb', line 81

def fetchRealtimeRecentTopicPosts(hours=1,topics=[62727],media=[1,2,4,5,8,9,10,11,12,13,16],start_page=0,page_size=1000,dump_file=false)
   xml = fetchRealtimeRecentTopicPostsXML(hours, topics, media, start_page, page_size)

  if dump_file
    log "\tDumping to file #{dump_file} at #{Time.now}"
    f = File.new(dump_file, "w")
    f.write(xml)
    f.close

    counter = Radian6::SAX::PostCounter.new 
    parser = Nokogiri::XML::SAX::Parser.new(counter)
    parser.parse(xml)
    log "\tFinished parsing the file at #{Time.now}"
    raise counter.error if counter.error
    return counter
  else
    return Radian6::Post.from_xml(xml)
  end
end

#fetchRealtimeRecentTopicPostsXML(hours = 1, topics = [], media = [1,2,4,5,8,9,10,11,12,13,16], start_page = 1, page_size = 1000) ⇒ Object



101
102
103
104
105
106
# File 'lib/radian6/api.rb', line 101

def fetchRealtimeRecentTopicPostsXML(hours=1, topics=[], media=[1,2,4,5,8,9,10,11,12,13,16], start_page=1, page_size=1000)
  path = "data/topicdata/realtime/#{hours}/#{topics.join(',')}/#{media.join(',')}/#{start_page}/#{page_size}?includeFullContent=1"
  log "\tGetting page #{start_page} for recent #{hours} hours in topic #{topics.join(', ')} at #{Time.now}"
  xml = api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
  return xml
end

#fetchRecentTopicPosts(hours = 1, topics = [62727], media = [1,2,4,5,8,9,10,11,12,13,16], start_page = 0, page_size = 1000, dump_file = false) ⇒ Object



41
42
43
44
45
# File 'lib/radian6/api.rb', line 41

def fetchRecentTopicPosts(hours=1,topics=[62727],media=[1,2,4,5,8,9,10,11,12,13,16],start_page=0,page_size=1000,dump_file=false)
  path = "data/topicdata/recent/#{hours}/#{topics.join(',')}/#{media.join(',')}/#{start_page}/#{page_size}"
  xml = api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })     
  return Radian6::Post.from_xml(xml)
end

#fetchWidgetData(widgetId) ⇒ Object



143
144
145
146
# File 'lib/radian6/api.rb', line 143

def fetchWidgetData(widgetId)
  path = "data/widget/#{widgetId}"
  api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
end

#get_by_path(path) ⇒ Object



47
48
49
50
# File 'lib/radian6/api.rb', line 47

def get_by_path(path)
  xml = api_get(path, { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })     
  return xml
end

#topics(cache = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/radian6/api.rb', line 29

def topics(cache=false)
  if (cache)
    xml = File.read('./fixtures/topics.xml')
  else
    log("App Key: #{@auth_appkey.inspect}")
    log("Auth Token: #{@auth_token.inspect}")
    xml = api_get( "topics", { 'auth_appkey' => @auth_appkey, 'auth_token' => @auth_token })
  end
  log("Received XML\n#{xml.inspect}")
  return Radian6::Topic.from_xml(xml)
end