Module: LittleBird::Request

Included in:
Client
Defined in:
lib/client.rb,
lib/request.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/request.rb', line 98

def method_missing(method, *args)
  if endpoints.include?(method)
    interpret(
      request(
        method, args[0]||{}
      ), response_types_by_endpoint[method]
    )
  else
    super
  end
end

Instance Method Details

#base_urlObject



3
4
5
# File 'lib/request.rb', line 3

def base_url
  "http://#{@base_url}/#{@version}/"
end

#endpoint_http_methodsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/request.rb', line 36

def endpoint_http_methods
  {
    v1:
      {
        compare_twitter_screen_name: :get,
        content_top: :get,
        content_recent: :get,
        graph_add: :post,
        graph_analyze: :get,
        graph_clear_attribute: :post,
        graph_community: :get,
        graph_create: :post,
        graph_download: :get,
        graph_drop: :post,
        graph_info: :get,
        graph_metric: :post,
        graph_list: :post,
        graph_set_attribute: :post,
        graph_set_recursive_attribute: :post,
        identity_lookup: :get,
        influencer_discover: :get,
        influencer_lookup: :get,
        misc_screen_name_lookup: :post,
        info_topology: :get,
        topic_search: :get
      }
  }[@version]
end

#endpointsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/request.rb', line 7

def endpoints
  {
    v1:
      {
        compare_twitter_screen_name: "compare/twitter_screen_name",
        content_top: "content/top",
        content_recent: "content/recent",
        graph_add: "graph/add",
        graph_analyze: "graph/analyze",
        graph_clear_attribute: "graph/clear_attribute",
        graph_community: "graph/community",
        graph_create: "graph/create",
        graph_download: "graph/download",
        graph_drop: "graph/drop",
        graph_info: "graph/info",
        graph_metric: "graph/metric",
        graph_list: "graph/list",
        graph_set_attribute: "graph/set_attribute",
        graph_set_recursive_attribute: "graph/set_recursive_attribute",
        identity_lookup: "identity/lookup",
        influencer_discover: "influencer/discover",
        influencer_lookup: "influencer/lookup",
        misc_screen_name_lookup: "misc/screen_name_lookup",
        info_topology: "info/topology",
        topic_search: "topic/search"
      }
  }[@version]
end

#extensionObject



94
95
96
# File 'lib/request.rb', line 94

def extension
  ".json"
end

#parse(response) ⇒ Object



114
115
116
# File 'lib/request.rb', line 114

def parse(response)
  JSON.parse(response)
end

#request(endpoint, args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/request.rb', line 118

def request(endpoint, args)
  puts args.inspect
  begin
    parse(
      RestClient::Request.execute(
        method: endpoint_http_methods[endpoint],
        url: url_for(endpoint),
        payload: args.merge(api_key: @api_key),
        timeout: 60000
      )
    )
  rescue => e
    parse(e.response)
  end
end

#response_types_by_endpointObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/request.rb', line 65

def response_types_by_endpoint
  {
    v1:
      {
        compare_twitter_screen_name: CompareResult,
        content_top: [Tweet],
        content_recent: [Tweet],
        graph_add: Graph,
        graph_analyze: Graph,
        graph_clear_attribute: GraphAttribute,
        graph_community: [Community],
        graph_create: Graph,
        graph_download: GEXF,
        graph_drop: Graph,
        graph_info: Graph,
        graph_metric: [UserMetric],
        graph_list: [Graph],
        graph_set_attribute: GraphAttribute,
        graph_set_recursive_attribute: GraphAttribute,
        identity_lookup: User,
        influencer_discover: [User],
        influencer_lookup: [Topic],
        misc_screen_name_lookup: [ScreenNameResolution],
        info_topology: APIStructure,
        topic_search: [DiscoverURL]
      }
  }[@version]
end

#url_for(endpoint) ⇒ Object



110
111
112
# File 'lib/request.rb', line 110

def url_for(endpoint)
  base_url+endpoints[endpoint]+extension
end