Class: Tweetkit::Response::Tweets

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tweetkit/response.rb

Defined Under Namespace

Classes: Expansions, Fields, Meta, Tweet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, **options) ⇒ Tweets

Returns a new instance of Tweets.



12
13
14
# File 'lib/tweetkit/response.rb', line 12

def initialize(response, **options)
  parse! response, **options
end

Instance Attribute Details

#annotationsObject

Returns the value of attribute annotations.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def annotations
  @annotations
end

#connectionObject

Returns the value of attribute connection.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def connection
  @connection
end

#context_annotationsObject

Returns the value of attribute context_annotations.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def context_annotations
  @context_annotations
end

#entity_annotationsObject

Returns the value of attribute entity_annotations.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def entity_annotations
  @entity_annotations
end

#expansionsObject

Returns the value of attribute expansions.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def expansions
  @expansions
end

#fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def fields
  @fields
end

#metaObject

Returns the value of attribute meta.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def meta
  @meta
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def options
  @options
end

#original_responseObject

Returns the value of attribute original_response.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def original_response
  @original_response
end

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def response
  @response
end

#tweetsObject

Returns the value of attribute tweets.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def tweets
  @tweets
end

#twitter_requestObject

Returns the value of attribute twitter_request.



10
11
12
# File 'lib/tweetkit/response.rb', line 10

def twitter_request
  @twitter_request
end

Instance Method Details

#each(*args, &block) ⇒ Object



61
62
63
# File 'lib/tweetkit/response.rb', line 61

def each(*args, &block)
  tweets.each(*args, &block)
end

#extract_and_save_expansionsObject



48
49
50
# File 'lib/tweetkit/response.rb', line 48

def extract_and_save_expansions
  @expansions = Expansions.new(@response['includes'])
end

#extract_and_save_metaObject



44
45
46
# File 'lib/tweetkit/response.rb', line 44

def extract_and_save_meta
  @meta = Meta.new(@response['meta'])
end

#extract_and_save_options(**options) ⇒ Object



52
53
54
# File 'lib/tweetkit/response.rb', line 52

def extract_and_save_options(**options)
  @options = options
end

#extract_and_save_requestObject



56
57
58
59
# File 'lib/tweetkit/response.rb', line 56

def extract_and_save_request
  @connection = @options[:connection]
  @twitter_request = @options[:twitter_request]
end

#extract_and_save_tweetsObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tweetkit/response.rb', line 32

def extract_and_save_tweets
  if (data = @response['data'])
    if data.is_a?(Array)
      @tweets = @response['data'].collect { |tweet| Tweet.new(tweet) }
    else
      @tweets = [Tweet.new(@response['data'])]
    end
  else
    @tweets = nil
  end
end

#lastObject



65
66
67
# File 'lib/tweetkit/response.rb', line 65

def last
  tweets.last
end

#next_pageObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tweetkit/response.rb', line 73

def next_page
  connection.params.merge!({ next_token: meta.next_token })
  response = connection.get(twitter_request[:previous_url])
  parse! response,
         connection: connection,
         twitter_request: {
           previous_url: twitter_request[:previous_url],
           previous_query: twitter_request[:previous_query]
         }
  self
end

#parse!(response, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/tweetkit/response.rb', line 16

def parse!(response, **options)
  parse_response response
  extract_and_save_tweets
  return unless @tweets

  extract_and_save_meta
  extract_and_save_expansions
  extract_and_save_options(**options)
  extract_and_save_request
end

#parse_response(response) ⇒ Object



27
28
29
30
# File 'lib/tweetkit/response.rb', line 27

def parse_response(response)
  @original_response = response.body
  @response = JSON.parse(@original_response)
end

#prev_pageObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tweetkit/response.rb', line 85

def prev_page
  connection.params.merge!({ previous: meta.previous_token })
  response = connection.get(twitter_request[:previous_url])
  parse! response,
         connection: connection,
         twitter_request: {
           previous_url: twitter_request[:previous_url],
           previous_query: twitter_request[:previous_query]
         }
  self
end

#tweetObject



69
70
71
# File 'lib/tweetkit/response.rb', line 69

def tweet
  tweets.first
end